c++ - pipe can not read/write all the chars -
i trying send array of char 1 process using pipe, characters passing not of them! part of beginning. code:
int p1[2], p2[2]; int main() { pipe(p1); int f1= fork(); if(f1 == 0) { char ar[100]; int n = 38; for(int i=0;i<n;i++) { ar[i] = 'f'; } close(p1[0]); //close read write(p1[1],ar,n+1); } else if (f1 > 0) { wait(null); int f2 = fork(); if(f2 == 0) { char arr2[100]; close(p1[1]); //close write int m = read(p1[0],arr2,strlen(arr2)); cout << arr2 << " " << m << endl; } else if (f2 > 0) {wait(null);} } return 0; }
you invoke std::strlen()
on uninitialized char
array, mistake. std::strlen()
looks first occurence of null byte in array , returns position. array uninitialized, making first occurence of null byte undefined.
besides, should check return values of library functions (pipe()
, read()
, write()
, fork()
, etc.).
Comments
Post a Comment