Lab#3 (CS500 OSD, Spring, 2016)

Due Date/Time:

Due Date/Time:

Before class, Tuesday June14, 2016 (Tuesday session)

Before class, ThursdayJune 16, 2016 (Thursday session)

Before class, Friday June 17, 2016 (Friday session)

Before class, Saturday June 18, 2016 (Saturday session)

Before class, Sunday June 19, 2016 (Sunday session)

Turn in your lab results with screen shots to your graders, including the answers to the question in red.

Each group assigned by graders just need to submit a report.

Note: You cannot earn this extra credit without finishing your individual case study. Submission after the deadline gets only partial credit.

Group number: ______

Student ID:______Name: ______

Student ID:______Name: ______

Student ID:______Name: ______

Student ID:______Name: ______

1

Summer 2015Silicon Valley University

Lab Description

A process can be a user or system program in execution. A process can create several new processes (child processes) through a system call from the executing process. The creating process is called the parent process and the new processes are called the children of the process. The process will run concurrently with its child processes. In the lab, you will have a program create a child process and display the Process Identifier (PID) of the child and parent process.

Lab Procedure

1)Use vi editor to enter the C program below to file lab3.c.

#include <sys/types.h>

#include <stdio.h>

#include <unistd.h>

int main( void )

{

pid_t pid, pid1, pid2;

pid = fork();

if (pid < 0) {

fprintf(stderr, "Fork Failed");

return 1;

}

else if (pid == 0) {

/* Child process */

pid1 = getpid();

printf("child: pid = %d\n", pid); /* A */

printf("child: pid1 = %d\n", pid1); /* B */

}

else {

/* Parent process */

pid1 = getpid();

printf("parent: pid = %d\n", pid); /* C */

printf("parent: pid1 = %d\n", pid1); /* D */

wait( NULL );

}

return 0;

}

2)Compile the C program:

gcc lab3.c -o lab3

3)Execute the image.Yourprogram output should like the following:

( whoami ); ./lab3

parent: pid = A // A is a number like 18092

parent: pid1 = B// number

child: pid = C// number

child: pid1 = D// number

Q#1: What is the meaning of the parent:pid? That is, the value of A is the PID of

a)Child process

b)Parent processAnswer (1pt): ______

Q#2:What is the meaning of parent:pid1? That is, the value of B is the PID of

a)Child process

b)Parent process Answer (1pt): ______

Q#3:Note that the value of child:pid, C, is 0. What does it mean?

a)Indicates the executing process is a child process, and 0 is not any process ID.

b)Indicates the executing process is a parent process, and 0 is not any process ID.

c)Indicates the executing process is a child process, 0 is child process ID.

d)Indicates the executing process is a parent process, 0 is parent process ID.

Answer (1pt): ______

Q#4:What is the meaning of child:pid1? That is, the value of D is the PID of

a)Child process

b)Parent processAnswer (1pt): ______

Q#5:The values of A and D are “always the same?

a)True

b)FalseAnswer (1pt): ______

Q#6:Take a snapshot of your screen to show your works, including your user name.

Answer (3pt): ______

Lab Procedure II.

This procedure is another method to produce Zombie processes with the fork() command. Zombie is a terminated process but not reaped by its parent. When we say not reaped by its parent, wemeans "parent is yet to collect the exit status from child". Child is completed its execution ready with theexit status and waiting for parent to ask for it.

1)Use vi editor to enter the C program below to file zombie.c

#include <unistd.h>

#include <stdio.h>

#include <sys/types.h>

int main(void)

{

pid_t pid;

pid = fork();

if (pid < 0) {

fprintf(stderr,"Unable to create child process\n");

return -1;

} else if (pid == 0) { /* Just have the child exit * and turn into a zombie. */

return 0;

} else { /** * Have the parent sleep for 10 seconds */

sleep(100);

printf("Parent exiting\n");

return 0;

}

}

2)Compile the C program:

gcc zombie.c –o zombie

3)Run the program in background:

./zombie &

4)Execute the command:

whoami; ps S;

The output should look similar to:

PID TT STAT TIME COMMAND

575 s000 S 0:00.23 -bash

10424 s000 S 0:00.00 ./zombie

10425 s000 Z 0:00.00 (zombie)

Q#7: Take a snapshot of your screen to show your works, including your user name.

Answer (3pt): ______

1

Summer 2015Silicon Valley University