Department of IT & CS
Course Instructor: ____________________ Dated: ________________
Semester: __________________________ Section: ________________
COMP-262L: Operating Systems
Lab 09: Process Creatiion
Utilities: getpid(), getppid(), fork(), wait(), exit(), sleep()
CLO1 CLO2 CLO3
Name Reg. No. Lab Tasks Report Viva Total
Marks Marks Marks Marks
20 5 5 30
Objectives:
Process creation
Process wait, sleep, zombie process
COMP-262L: Operating Systems Page 1
Write and explain the commands used to perform each task. Also attach the output response.
Lab Task 1: [3 Marks]
1. Write a short detail about “uistd.h” and “sys/types.h” header files.
2. What is the difference between getpid() and getppid()?
3. Write this program and compile using built-in GNU C compiler.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hello World!\n");
fork( );
printf("I am after forking\n");
printf("\tI am process %d.\n", getpid( ));
}
4. Write and execute this program to see which process is executed first
#include <stdio.h>
#include <unistd.h> /* contains fork prototype */
int main(void)
{
int pid;
printf("Hello World!\n");
printf("I am the parent process and pid is : %d .\n",getpid());
printf("Here i am before use of forking\n");
pid = fork();
printf("Here I am just after forking\n");
if (pid == 0)
printf("I am the child process and pid is :%d.\n",getpid());
else
printf("I am the parent process and pid is: %d .\n",getpid());
}
Lab Task 2: [3 Marks]
1. What is orphan process and what is zombie process?
2. Print something and Check id of the parent process
3. Create a child process and print child process id in parent process
COMP-262L: Operating Systems Page 2
4. Create a child process and print child process id in child process
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
int forkresult;
printf("%d: I am the parent. Remember my number!\n", getpid());
printf("%d: I am now going to fork ... \n", getpid());
forkresult = fork();
if (forkresult != 0)
{ /* the parent will execute this code */
printf("%d: My child's pid is %d\n", getpid(), forkresult);
else /* forkresult == 0 */
{ /* the child will execute this code */
printf("%d: Hi! I am the child.\n", getpid());
printf("%d: like father like son. \n", getpid());
return 0;
Lab Task 3: [6 Marks]
1. Create a process and make it an orphan.
Steps to create an orphan process:
COMP-262L: Operating Systems Page 3
1. print something and get its pid and ppid
2. create a child process
3. Now as a parent process print parent id and id of child process
4. Make child sleep for 5 seconds
5. Now while child is sleeping parent will terminate. Print parent id of child to make
sure it is orphaned (PPID has been changed)
#include <stdio.h>
main()
{
int pid ;
printf("I'am the original process with PID %d and PPID %d.\n",
getpid(), getppid()) ;
pid = fork ( ) ; /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero,so I must be the parent*/
{
printf("I'am the parent with PID %d and PPID %d.\n",
getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ;
}
else /* pid is zero, so I must be the child */
{
sleep(4); /* make sure that the parent terminates first */
printf("I'm the child with PID %d and PPID %d.\n",
getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
}
COMP-262L: Operating Systems Page 4
Lab Task 4: [6 Marks]
Create a process and make it a Zombie.
Steps to create a zombie process:
1. Execute fork to create a child
2. In parent process (using if statement) Create an infinite loop so that it never terminates
and never executes wait ()
3. Make parent sleep for 100 sec
4.Terminate child process exit using exit.
Now this child is a zombie because no parent is waiting for him.
#include <stdio.h>
main ( )
{
int pid ;
pid = fork(); /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero, so I must be the parent */
{
while (1) /* Never terminate and never execute a wait ( ) */
sleep (100) ; /* stop executing for 100 seconds */
}
else /* pid is zero, so I must be the child */
{
exit (42) ; /* exit with any number */
}
}
COMP-262L: Operating Systems Page 5
Lab Task 5: [2 Marks]
Write a C/C++ program in which a parent process creates a child process using a fork()
system call. The child process takes your age as input and parent process prints the age.
COMP-262L: Operating Systems Page 6