You are on page 1of 26

Operating Systems

Lecture 3 - Introduction to Process

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

Program is passive entity stored on disk (executable file),


process is active
◦ Program becomes process when executable file loaded into memory
◦ Execution of program starts via GUI mouse clicks, command line
entry of its name, etc.

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);
}

• To compile it we type: gcc filename.c


• Note that a file called a.out will be created in the same directory
• To run the executable a.out we have to type ./a.out
• ./ is used in Linux and Unix to execute a compiled program in the current
directory
5
Process Concept (Cont.)
One program can be several processes
◦ Consider multiple users executing the same program
◦ e.g. when we execute a.out many times → many processes a.out will be
running

A running system consists of multiple processes


◦ OS processes
◦ Processes started by the OS to do “system things”, e.g. winlogon.exe and explorer.exe under win
7 (browse them in task manager)
◦ Not everything is in the kernel
◦ User processes
◦ Executing user code, with the possibility of executing kernel code by going to kernel mode
(system calls)
◦ In our example a.out is a user program, where printf launches a system call to display
something on the screen

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

Process Address Space


◦ Stack contains:
◦ Temporary data (function parameters, return addresses, local variables)
◦ Heap (dynamic memory space)
◦ e.g when allocating dynamic arrays…
◦ Data contains:
◦ Global variables (e.g. int x, float y)
◦ Text:
◦ Code in binary (the program)
◦ e.g. the instructions in binary

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

Only one process can be running on any processor at any instant,


although many processes may be ready and waiting.

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

Upon creation of a child, the parent could continue


execution, or wait for the child’s completion

The child could be a clone of the parent (i.e., have a copy of


the address space), or have a new program loaded into it

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

What are UID, PID,


PPID, TTY, TIME and
CMD?

15
Process System Calls

Write an infinite loop program and try these commands!

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()
{

// make two process which run same


// program after this instruction
fork();

printf("Hello world!\n"); Output:


return 0; Hello world!
Hello world!
}

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");

// parent process because return value non-zero.


else
printf("Hello from Parent!\n");
} 1. Hello from Child!
int main() Hello from Parent!
{ (or)
forkexample(); 2. Hello from Parent!
return 0; Hello from Child!
}
21
Process System Calls
Fork()
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

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

The kill kills


parent and child
(not always true
depending on
O/S)

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?

Original and the 2nd fork of original and


the cloned and the fork in the clone.
Total of 4 ( original and 3 new)

26

You might also like