Professional Documents
Culture Documents
1
Topics
Process Concepts
Operations on Processes (fork, execlp
…)
Process Scheduling Next lectures
Multithreading
Inter-process Communications
2
Objectives
To introduce the notion of a process -- a program in
execution, which forms the basis of all computation
To describe the various features of processes,
including creation, life cycle and termination of
processes
3
Process Concept
Process – is a program in execution
◦ Process execution must progress in sequential fashion
4
Example of a program/process
The program is filename.c
The process is a.out
#include <stdio.h>
main()
{
int i,sum;
sum=0;
for (i=0; i<10; i++)
if (i < 5)
sum=sum+i;
printf("sum=%d\n",sum);
}
6
Process Address Space
The OS role is to allocate a RAM space for any running process
A process space in RAM includes:
◦ Program counter and processor’s registers defining current activity
min
7
Process Life Cycle
As a process executes, it changes state
◦ new: The process is being created
◦ ready: The process is waiting to run by the CPU
◦ running: Instructions are being executed in CPU
◦ waiting: Process waiting for some event to occur
◦ terminated: The process has finished execution
8
Topics
Process Concepts
Operations on Processes (fork, execlp
…)
Next lectures
Process Scheduling
Multithreading
Inter-process Communications
9
A process may create a new process, in which
case it becomes a parent
We obtain a tree of processes
Each process has a pid
Tap ps –ef for list of running processes
ppid refers to the parent’s pid
10
When a process creates a child, the child may inherit/share
some of the resources of its parent, or may have entirely
new ones
11
Questions
Back to our first example:
Is a.out a child?
If yes, who is its parent?
Where should be the
location of a.out in this tree?
12
1
Process System Calls
starting up new programs
Bottom line: To start a new program, a currently running program
makes a clone of itself and copies the code of the new program (to be
run).
All programs are descendants of the FIRST program run after the
kernel boots. → The init process
13
Process System Calls
A process is a program currently in execution
Commands used: ps, ps –ef, CTRL-C, CTRL-Z, &, bg, fg, kill #
Try all of them on ubuntu:
ps → display current processes, ps –ef → same with more info
14
Process System Calls
15
Process System Calls
16
Process System Calls
Fork()
Fork system call is used for creating a new process, which is called child
process, which runs concurrently with the process that makes the fork() call
(parent process). After a new child process is created, both processes will
execute the next instruction following the fork() system call. A child process
uses the same pc(program counter), same CPU registers, same open files
which use in the parent process.
It takes no parameters and returns an integer value. Below are different
values returned by fork().
Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of
newly created child process.
17
Process System Calls
Fork()
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
18
Process System Calls
Fork()
#include <stdio.h>
#include <sys/types.h>
int main() Output:
{ Hello
hello
fork(); hello
fork(); hello
fork(); hello
hello
printf("hello\n"); hello
return 0; hello
}
19
Process System Calls
Fork()
The number of times ‘hello’ is printed is equal to number of process
created. Total Number of Processes = 2n, where n is number of fork
system calls. So here n = 3, 23 = 8
Let us put some label names for the three lines:
So there are total eight processes (new child processes and one
original process).
If we want to represent the relationship between the processes as a
tree hierarchy it would be the following:
The main process: P0
Processes created by the 1st fork: P1
Processes created by the 2nd fork: P2, P3
Processes created by the 3rd fork: P4, P5, P6, P7
20
Process System Calls
Fork()
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!\n");
void forkexample()
{
int x = 1;
if (fork() == 0)
printf("Child has x = %d\n", ++x); Parent has x = 0
else Child has x = 2
printf("Parent has x = %d\n", --x); Or
}
Child has x = 2
int main()
{
Parent has x = 0
forkexample();
return 0;
}
22
Process System Calls
Fork
• Fork() creates a clone of the currently running program
• The cloned program starts executing at the next line and the forking
Fork returns an integer, here stored in
program also
variable i.
• At the end, we have multiple files running concurrently
The variable “i” is visible in BOTH, the
forking and the forked program.
In the forking (parent), it contains the
PID of the newly created child.
In the forked (child), it is always ZERO!
23
Process System Calls
Fork
24
Process System Calls
Fork
Q: What do you expect the
output to be?
25
Process System Calls
Fork
Q: how many programs are
running?
26