You are on page 1of 7

LAB SHEET 6

Process hierarchy

1.
Code:
#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main()

if(fork())

printf("Parent ID : %d\n",getpid());

wait(NULL);

else

printf("C1 ID : %d\n",getpid());

printf("Parent of C1 ID : %d\n",getppid());

return 0;

2.

Code:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>
int main()

if(fork())

if(fork())

wait(NULL);

else

printf("Child C2 ID : %d\n",getpid());

printf("Parent of C2 ID : %d\n",getppid());

return;

printf("Parent ID : %d\n",getpid());

printf("Parent of P ID : %d\n",getppid());

wait(NULL);

else

printf("Child C1 ID : %d\n",getpid());

printf("Parent of C1 ID : %d\n",getppid());

return 0;

}
3.

Code:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main()

if(fork())

printf("Parent ID : %d\n",getpid());

printf("Parent of P ID : %d\n",getppid());

wait(NULL);

} else

printf("Child C1 ID : %d\n",getpid());

printf("Parent of C1 ID : %d\n",getppid());

if(fork())

{ wait(NULL);

else

printf("Child C2 ID : %d\n",getpid());

printf("Parent of C2 ID : %d\n",getppid());

return 0;

}
4.

Code:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main()

if(fork())

wait(NULL);

if(fork())

wait(NULL);

else

printf("Child C2 ID : %d\n",getpid());

printf("Parent of C2 ID : %d\n",getppid());

if(fork())

wait(NULL);

if(fork())

wait(NULL);

else

printf("Child C4 ID : %d\n",getpid());

printf("Parent of C4 ID : %d\n",getppid());

return;

}
}

else

printf("Child C3 ID : %d\n",getpid());

printf("Parent of C3 ID : %d\n",getppid());

return;

printf("Parent ID : %d\n",getpid());

printf("Parent of P ID : %d\n",getppid());

else

printf("Child C1 ID : %d\n",getpid());

printf("Parent of C1 ID : %d\n",getppid());

return 0;

5.

Code:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

int main()
{

if(fork())

printf("Parent ID : %d\n",getpid());

printf("Parent of P ID : %d\n",getppid());

wait(NULL);

if(fork())

wait(NULL);

else

printf("Child C2 ID : %d\n",getpid());

printf("Parent of C2 ID : %d\n",getppid());

if(fork())

wait(NULL);

else

printf("Child C4 ID : %d\n",getpid());

printf("Parent of C4 ID : %d\n",getppid());

return;

if(fork())

wait(NULL);

else

printf("Child C5 ID : %d\n",getpid());
printf("Parent of C5 ID : %d\n",getppid());

return;

return;

if(fork())

wait(NULL);

else

printf("Child C3 ID : %d\n",getpid());

printf("Parent of C3 ID : %d\n",getppid());

return;

else

printf("Child C1 ID : %d\n",getpid());

printf("Parent of C1 ID : %d\n",getppid());

return 0;

You might also like