View Full Version : need a bit of help with c (forks)
so i'm writing this small program where i fork the main program, the i fork the child... so now i essentially have a parent, and two children...
my question is how can i pass the values of the pid's of the children to the parent ?
CrunchyB
06-19-2004, 09:14 PM
$man fork
On success, the PID of the child process is returned in the parent's thread of execution
actullay... i'm not passing the pid of the child...i'm passing a random number calculated based on the pid of the child
Shompola
06-19-2004, 09:27 PM
Is this an operative system course?
software tools and system programming
Shompola
06-19-2004, 09:29 PM
I see I had a similiar course a few years back. If I remember we had references to the children via the parent with pointers via the datatype.
it has somethign to do with WEXITSTATUS... i can't exactly figure it out :s
CrunchyB
06-19-2004, 09:54 PM
Beej's programming guides helped me through most IPC assignments :)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pfds[2];
char buf[30];
pipe(pfds);
if (!fork()) {
printf(" CHILD: writing to the pipe\n");
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n");
exit(0);
} else {
printf("PARENT: reading from pipe\n");
read(pfds[0], buf, 5);
printf("PARENT: read \"%s\"\n", buf);
//wait(NULL); (dunno what's up with this >CrunchyB)
}
}
vBulletin v3.0.7, Copyright ©2000-2005, Jelsoft Enterprises Ltd.