Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- main()
- {
- int k, status;
- int num;
- int num1;
- int num2;
- int num3;
- int num4;
- int char;
- printf("I'm the original process. PID=%d, PPID=%d.\n", getpid(), getppid());
- num=fork();
- if ( num !=0 )
- {
- waitpid (num2, &status, WUNTRACED);
- printf("I'm the parent (P0).PID=%d, PPID=%d.\n", getpid(), getppid());
- printf("I have a child (P1) with PID %d.\n", num);
- num1=fork();
- if ( num1 !=0 ) {
- printf("I'm the parent (P1).PID=%d, PPID=%d.\n", getpid(), getppid());
- }
- else {
- printf("I'm the new child (P2). PID=%d, PPID=%d.\n", getpid(), getppid());
- num2=fork();
- if ( num2 !=0 )
- k=wait(&status);
- {
- printf("I'm the parent (P2).PID=%d, PPID=%d.\n", getpid(), getppid());
- printf("I'm the first child (P3) with PID %d.\n", num2);
- num3=fork();
- if ( num3 !=0 ) {
- k=wait(&status);
- printf("I'm the parent (P2).PID=%d, PPID=%d.\n", getpid(), getppid()) ;
- }
- else {
- printf("I'm the second child (P4). PID=%d, PPID=%d.\n", getpid(), getppid());
- num4=fork();
- if ( num4 !=0 ) {
- printf("I'm the parent (P2).PID=%d, PPID=%d.\n", getpid(), getppid());
- }
- else {
- printf("I'm the third child (P5). PID=%d, PPID=%d.\n", getpid(), getppid());
- }
- }
- }
- }
- }
- else
- {
- printf("I'm the child. PID=%d, PPID=%d.\n", getpid(), getppid());
- }
- printf("PID %d terminates.\n", getpid());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement