• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

need a bit of help with c (forks)

Status
Not open for further replies.

7imz

Member
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 ?
 

7imz

Member
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

Banned
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.
 

CrunchyB

Member
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)
}
}
 
Status
Not open for further replies.
Top Bottom