You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
927 B

#include <stdio.h>
int main()
{
int child, grandson;
char *sargs[] = { "/bin/echo", "Son ", "Hello", "World!", NULL};
char *gargs[] = { "/bin/echo", "Grandson ", "Hello", "World!", NULL};
if (!(child = fork()))
{
/* child */
printf("child:s1: pid %d: %d is my father\n", getpid(), getppid());
if(!(grandson=fork()))
{
printf("grandson: pid %d: %d is my father\n", getpid(), getppid());
execve("/bin/echo", gargs, NULL);
printf("pid %d: I am back, something is wrong!\n", getpid());
}else{
printf("child:s2: pid %d: %d is my father\n", getpid(), getppid());
wait4(grandson, NULL, 0, NULL);
execve("/bin/echo", sargs, NULL);
printf("pid %d: I am back, something is wrong!\n", getpid());
}
}
else
{
int myself = getpid();
printf("parent: pid %d: %d is my son\n", myself, child);
wait4(child, NULL, 0, NULL);
printf("pid %d: done\n", myself);
}
return 0;
}