Hello guys.
Can I write structure data to child pipe?
Usual examples are about writing char* or int type.
I tried to write structure. But it does not look work.
Will you check my code?
-----------------------------------------------------------------------

struct work_list {
	int heavy_rate; //0 - 5 level
	int work_owner; //thread id
} ; 

int main(void)
{
int     fd[2], nbytes, icount;
pid_t   childpid;

work_list work_ll[5] = {
	{3, 5}, {3, 3}, {3, 4}, {4, 1}, {4, 3}};

work_list *readbuffer;
pipe(fd);
    
if((childpid = fork()) == -1)
   {
	perror("fork");
   }

if(childpid != 0)
   {
	close(fd[0]);
	write(fd[1], work_ll, (sizeof(work_ll)+1));
   }
else
   {
	close(fd[1]);
	nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
	cout << "Received string: " << readbuffer[0].dirty_rate << " byte " << nbytes << endl;
   }
}

where did you call popen() to open the pipe?

[edit]Oh never mind. I checked the *nix man page and it appears you did it the same way as in the example. Since I don't use *nix very often I have no clue what is wrong. [/edit]

Edited 6 Years Ago by Ancient Dragon: n/a

Writing (line 25) is almost OK (I think that a "+1" was added in an act of desperation, it is not needed there).
Reading (line 30) is a problem. First, a readbuffer is not initialized, so the segfault is imminent. Second, you only reading 4 bytes of the message. Fix those and we can proceed further.

To answer your original question, yes you can. You just need to be careful.

Comments
Nice
This article has been dead for over six months. Start a new discussion instead.