CS 410 Operating SystemsLab Assignment #2
Using “fork()” (Creating a Process)
In multitasking OS, processes (running programs) need a way to create new processes to runother programs. In Unix/Linux OS fork is an operation whereby a process creates a copy ofitself. It is usually a system call, implemented in the kernel. Fork is the primary method ofprocess creation on Unix-like operating systems.For a process to start the execution of a different program, it first forks to create a copy of itself.Then, the copy, called the “child process”, calls the exec system call to overlay itself with theother program: it ceases execution of its former program in favor of the other.The fork operation creates a separate address space for the child. The child process has anexact copy of all the memory segments of the parent process.
The fork function is the primitive for creating a process. It is declared in the header file unistd.hFunction: pid_t fork (void)http://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html
A program is an executable file residing in a disk file. An executing instance of a program iscalled a process. In Unix/Linux a program is read into memory and executed by the kernel as aresult of one of the six exec functions.Every Unix/Linux process is guaranteed to have a unique numeric identifier called the processID. The process ID (short: PID) is always a nonnegative integer.Under Unix, the only way (except for some very special processes) to create a new processesis when an existing process calls the fork function. You will find details about fork and wait in themanual pages (Unix/Linux command: “man fork”).
Assignment:Write a simple program that uses the “fork” function. The new process created by fork is calledthe “child” process. The function is called once but returns twice. Both the child and the parentcontinue executing with the instruction that follows the call to fork. Here is a typical example:
/* parent creates a process */pid = fork();/* the return value is a process ID, that ALWAYS needs to be tested */switch (pid) {case -1:/* error: fork was unsuccessful */break;case 0:/* this is the child process */
/* no process ID *//* … do something … */break;default:/* this is the parent process *//* pid=process ID of the child *//* … */}/* both processes continue here */
You can list processes by using the ps command or the top command (to see the manual pagesuse “man ps” or “man top”). Under Unix/Linux a process that has terminated, but whoseparent has not yet waited for it is called a zombie.Adjust the sleep calls for parent and child and use the command ps to answer these questions(add them as comments at the end of your program):
1. What status is being printed when the child terminates before the parent (in another terminaluse: ps ajxf | grep yourProgramName) ?Note: run the command “man ps” for more options.
2. What is the parent process ID (PPID) of a child process whose parent terminates beforethe child?
Note: to get the proper results from the “ps” command you need to use two terminals, in oneyou will run the program, and in another terminal you will execute the “ps” command. This has tobe almost at the same time, the parent or the child (depending on the case) should still berunning when you execute the “ps” command, otherwise you won’t get any information fromthose processes.
To make the parent wait for the child, replace the sleep() bypid = waitpid(pid, &status, 0) in the code for the parent and read about the functionby executing “man waitpid”. The status is an integer.
Name your programs: YourLastName_YourFirstName_Lab02_1.cand YourLastName_YourFirstName_Lab02_2.cYou must include a comment on the top of your programs like the following
/*CS 410 Operating SystemsLastname FirstnameLab 02*/
SubmissionThe submission of your program and report will be through the Western Online. The reportshould include screenshots and a conclusion (properly labeled as that) assessing theimportance of the fork function and process replication. Include a header in your report andname it: YourLastName_YourFirstName_Lab01.pdfCheck the Rubrics for more details about this assignment.
——///