Download Fork() system call - IT325

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Library (computing) wikipedia , lookup

Unix security wikipedia , lookup

Burroughs MCP wikipedia , lookup

DNIX wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
King Saud University
College of Computer & Information Sciences
Information Technology Department
IT325: Operating Systems
Assignment 3
First Semester 1434/1435H
Name:
ID:
Section:
Serial:
References:
1) Your text book, Lecture notes, Tutorial slides and activity sheet.
2) Programming in C.
3) "C Programming Language" - 2nd Edition- by Kernighan andRitchie.
4) Extracted from http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/fork/fork.html
Assignment Objectives:
1. Gaining a better understanding of the fork(), exec() and wait() system calls.
2. Getting familiar with the UNIX/Linux operating system and C programming
Hand-in:
Submit printed copies of the following:
 The program code
 The output of the program
Part one
UNIX process creation
In this tutorial, you’ll learn how a process can create a new process in the UNIX operating
system and learn about some system calls related to creating, executing and communicating
with a child process. All the program files referred to in this tutorial can be found on the
assignments page on the course blog (i.e., http://it325blog.wordpress.com/assignments/).
Getting started with system calls
Let’s first run a simple program with a few simple system calls. Look at the program getinfo1.c.
Compile and execute it. Answer the following questions:




What’s the process ID of this process? _______________
What’s the process ID of the parent of this process? ________________
What’s the user ID of the owner of this process? _____________________
How many seconds did the process get suspended for? _ __________________
Fork() system call
If a program contains a call to fork( ), the execution of the program results in the execution of
two processes. One process is created to start executing the program. When the fork( ) system
call is executed, another process is created. The original process is called the parent process
and the second process is called the child process. The child process is an almost exact copy of
the parent process. Both processes continue executing from the point where the fork( ) calls
returns execution to the main program. Since UNIX is a time-shared operating system, the two
processes can execute concurrently.
Look at the program fork_basic.c. Compile and execute it. Answer the following questions



What value did the fork() function return to the child process? ______________
What value did the fork() function return to the parent process? ______________
What’s the process ID of the child process? ________________
Some differences between the child and parent process are:






They have different pids.
In the parent, fork( ) returns the pid of the child process if a child process is created.
In the child, fork( ) always returns 0.
Separate copies of all data, including variables with their current values and the stack.
Separate program counter (PC) indicating where to execute next; originally both have
the same value but they are thereafter separate
After fork, the two processes do not share variables
Implementation of the fork() system call:






In UNIX, fork is a system call that creates a new process control block (PCB)
Copies most information from the current process's PCB into the next free location in
the process table
The parent and child processes will now both be ready to execute.
One can be left/placed in the RUNNING state and the other in the READY state.
Henceforth, both will takes turns in using the processor (along with all other processes).
Which one is chosen to run first, depends on the exact version of the operating system.
Look at the program fork_overlap.c. . Compile and execute it. Answer the following question:

The output of the parent and child processes may overlap. Why do you think this
happens?
Wait() system call



This is a system call that causes the process to wait for a signal (waits until any type of
signal is received from any process).
It is most commonly used in a parent process to wait for the signal that the OS sends to
a parent when its child is terminated
It returns the pid of the process sending the signal
Suppose we want to have the parent wait for a signal from the child process. Look at the
program fork_wait.c. Compile and execute it. Answer the following questions:


What is the child process’s id? _______
What is the return value of the wait system call? __________________
Exec() system call
Suppose we want the new process to do something quite different from the parent process,
namely run a different program. The execl system call loads a new executable into memory and
associates it with the current process. In other words, it changes things so that this process
starts executing executable code from a different file.




In the following program, execl is called to load the file /bin/ls into memory. The
argument vector is set to the words "ls" and "-l".
argv[0] is set to "ls"
argv[1] is set to "-l"
The number of arguments to execl can vary so we end with NULL to show that there are
no more arguments.
Look at the program fork_exec.c. Compile and execute it. Answer the following question:

Does the child process’s id change as a result of the execl() system call? How can you tell
whether it has been changed or not?
Multiple fork() calls
A program can make multiple fork calls to create multiple child processes, each of which may
create their own children if they execute fork() calls, too. To see how this works, do the
following:
Look at the program simple_fork1.c which contains one fork() call. Compile and execute it. How
many processes are created by executing this program? ____________________
Look at the program simple_fork2.c which contains two fork() calls. Compile and execute it.
How many processes are created by executing this program? ___________________
Look at the program simple_fork3.c which contains one fork() call. Compile and execute it. How
many processes are created by executing this program? _____________
Based on the above, how many processes are created by executing a program that contains n
fork() calls? ___________________________
Part two

Write a C program that creates 16 processes using fork() system calls. Have each process print
out its process ID.