pass child'PID to the perant

  • clunix
  • Born
  • Born
  • clunix
  • Posts: 2

Post 3+ Months Ago

hi my friends
I am new
how are you?
I have good quiestion

I hve made a program by using fork() system call
I want from chlid to calculate something and I want the parent itself print the result.
I think I have to pass the child' PID to parent but I did't how can i do this in program
if some boday can help me How can I pass this in the program
thank you my friends
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

fork's return value (in the parent) is the child's pid. If you want the child to communicate to the parent, you could make a pipe before you fork and then send information over that.
  • clunix
  • Born
  • Born
  • clunix
  • Posts: 2

Post 3+ Months Ago

thank you Sam Hughes
but please explain it to me how can I use pipe in the program(give me small example) so I can understand,
because this way is new for me.
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

Code: [ Select ]
int file_descriptors[2];
int errcode = pipe(file_descriptors);
  1. int file_descriptors[2];
  2. int errcode = pipe(file_descriptors);


Now file_descriptors[0] contains the file descriptor number for reading from the pipe, file_descriptors[1] containse the number for writing. Then after you fork, both processes inherit the file descriptor table. So the child process can write to the pipe (with write and the parent process can read from the pipe (with read). Or vice versa -- go one way or the other. I assume you know how read and write work; if not, read the man pages.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 50 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.