jehanne/qa/lib/newlib/010-fork.c

49 lines
767 B
C
Raw Normal View History

2017-04-22 00:46:55 +02:00
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
int counter = 0;
int status;
printf("parent: pid = %d; ppid = %d\n", getpid(), getppid());
pid_t pid = fork();
if (pid == 0)
{
// child process
printf("child: pid = %d; ppid = %d\n", getpid(), getppid());
int i = 0;
for (; i < 5; ++i)
{
printf("child: counter=%d\n", ++counter);
}
}
else if (pid > 0)
{
// parent process
int j = 0;
for (; j < 5; ++j)
{
printf("parent: counter=%d\n", ++counter);
}
wait(&status);
if(status != 0){
printf("parent: child exited with status %d\n", status);
return 2;
}
2017-04-22 00:46:55 +02:00
}
else
{
// fork failed
printf("parent: fork() failed!\n");
return 1;
}
return 0;
}