Chapter 2 - Processes
Chapter 2 - Processes
Processes
14 February 2025 1
Outline
Define a Process.
Differentiate between a process and a thread.
Know what is multitasking.
Define a process state.
Differentiate between the 5 states of a process and draw a state process diagram.
Know the importance and the architecture of PCB.
Know the scheduling concept and draw a processes scheduling diagram.
Know the difference between long-term, short-term and mid-term schedulers.
Understand the concept and the importance of context switch.
Know the different process operations: Creation, Termination and Inter-Process
Communication.
Understand the Producer-Consumer model.
Program Written in
Definition
High level Language Operating System
Transform Load
C/C++/C#
/Java/etc
Computer Program
Compiler Readable Code Memory Execution
By Machine
Allocate
Resources
Process Vs Thread
Process Thread
A process can be taught as a program A unit of execution within a process. A
in execution. process can have one thread or more.
Process
Thread
Thread
Thread
Thread
Thread
Multitasking
During its lifetime, a process does a lot of I/O activity. This is especially true for I/O
bound processes, but also applies to CPU bound processes.
I/O is very slow compared to CPU. While a process waits for completion of some I/O
activity, it is “idle”. Other processes can use the CPU during this time.
The State of a process can be easily understood by asking the following question:
What is the process is currently doing?
New Terminated
Interrupt
Admitted
Exit
Ready Running
Scheduler dispatch
Waiting
I/O or Event Completion I/O or Event Wait
Each process is presented in the Operating System by a Process Control Block (PCB) also called Task Control Block.
PCB is used to represent a particular process in the Operating System.
Process ID Unique ID of a particular process.
Process State Particular State in which a process at that particular moment (New, Waiting, ….)
The address of the next instruction that has to be executed by a particular process.
Process State Program Counter For example if we have a line of code, the PC tells us the address of the next line in a
particular moment.
Process Number
CPU Registers Tells us the registers used by a particular process (index registers, stack pointer, etc)
Program Counter
CPU Scheduling
Registers Has the priority of the processes, pointer to the scheduling queue, etc
Information
Memory Limits Memory
Represents the memory used by a particular process, different aspects of the memory
List of open files Management
related to a certain process
Information
……..
Accounting Represents the resources being used by the particular process such as CPU, Time,
PCB Information Memory
I/O status
Represents the I/O devices assigned to a particular process
Information
PCB in Linux
Linux maintains a doubly-linked list of the processes by the C structure task_struct.
pid_t pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
…
Basic Concepts
The objective of multiprogramming is to have process running at
First Concept
all times, to maximize CPU utilization.
Scheduling Queues
As processes enter the system, they are put into a job queue, which
Job Queue
consists of all the processes in the system.
The processes that are residing in the main memory and are ready
Ready Queue
and waiting to be executed are kept on a list called the ready queue.
Processes CPU
Scheduling Queues
Medium-term scheduling is a part of swapping. It removes the processes from the memory. It reduces the
degree of multiprogramming. The medium-term scheduler is in-charge of handling the swapped out-processes.
A running process may become suspended if it makes an I/O request.
4- Father takes his son to the hospital 5- After treating his son at the hospital, father
Let us suppose that taking the son to returns to read the book from the page he
the hospital is a second process with marked.
a higher priority than the first The first running process is running again
Concept of Context
Interrupts cause the operating system to change a CPU from its current task and to run
a kernel routine.
When an interrupt occurs, the system needs to save the current context of the process
currently running on the CPU so that it can restore that context when its processing is
done, essentially suspending the process and then resuming it.
The context is represented in the PCB (Process Control Block) of the process.
Context Switch time is pure overhead, because the system does no useful work while
switching.
Its speed varies from machine to machine, depending on the memory speed, the number
of registers that must be copied and the existence of special instructions.
Process Creation
A process may create several new processes, via a create process-system call, during the
course of execution.
The creating process is called the parent process, and the new processes are called the
children of that process.
Each of these processes may in turn create other processes, forming a tree of processes.
We recall that each process has a unique pid (process identifier) usually an integer.
Parent_Process
pid
Child_Process
pid
Sched
pid=0
init
pid=1
F sf lush
pageout pid=3
inetd
dtlogin pid=2
pid=140
pid=251
A tree processes on a
telnetdaemon
pid=7776 Xsession typical Solaris System
pid=294
Csh ls
pid=7778 std_shel
pid=340 pid=2123
Process Creation
Two Possibilities in The child is a duplicate of the parent process (it has the same and data as the parent).
terms of Address
Space The child has a new program loaded in it.
UNIX programs use the following system calls 𝑷𝑰𝑫 > 𝟎 𝑷𝑰𝑫 = 𝟎
fork() creates new process as an image of the
parent.
exec() used after fork() to replace the process
image with a new program.
exit() terminates the process with an exit status
wait() used by parent to wait for a child to
terminate.
Process Termination
A process terminates by invoking exit() system call.
The status is caught by parent via the function wait() as illustrated in the following
piece of code:
C code int status; C code
int pid = wait(&status);
When process terminates, its resources are deallocated by the OS. However, its PCB remains
in memory until its parent calls wait. Until then, it’s a zombie process.
A parent might decide to terminate a child process (For example parent wants to exit..)
Kernel can decide to “kill” a process under severe circumstances.
Other reason to terminate a process are errors such as division by zero, access violation,
violation of some rules, etc.
User (process owner) might want to abort the process.
Producer Consumer
Producer and consumer run concurrently. Buffering is needed because at some moments
items might arrive faster than they are consumed. A buffer (aka queue) has limited size, so the
problem is also named the bounded-buffer problem.
Input
Memory Accessible by
Process A
Process Process
A B
Shared Memory between
processes A and B
Memory Accessible by
Bidirectional
Process B
In the above example, producer and consumer are managing the shared buffer as a cyclic buffer
Processes have separate address spaces, OS provides special ways to allocate shared memory
(POSIX Shared Memory: i.e. mmap) for different processes.
The shared memory paradigm works only for IPC on the same host. It is not possible
between processes across network.
In this case, the buffer is created and managed by the user processes explicitly, as a circular
buffer data structure, accessible by both processes.
Direct Link
send(pid, msg), recv(pid, msg)
Process Process
A B
Indirect Link
send(mbox, msg), recv(mbox, msg)
Bidirectional MAILBOX
send() and recv() calls can be blocking (aka synchronous) or non-blocking. Some apps
use both blocking, as a synchronization method called rendez-vous (i.e send and receive
both use blocking mode).
Message
Shared
Passing
Memory
fd[0] fd[1]
Reading Writing
Using the redirector “>”, you can redirect the The pipe symbol “ | ” permits to create a pipe between two
output of a program or a command into a programs, so that the output of the first becomes the input of
specified file. Example: the second. Example:
The output of ifconfig command is sent to conf.txt The output of ifconfig command is executed by grep “192”
Define a Process.
Differentiate between a process and a thread.
Know what is multitasking.
Define a process state.
Differentiate between the 5 states of a process and draw a state process diagram.
Know the importance and the architecture of PCB.
Know the scheduling concept and draw a processes scheduling diagram.
Know the difference between long-term, short-term and mid-term schedulers.
Understand the concept and the importance of context switch.
Know the different process operations: Creation, Termination and Inter-Process
Communication.
Understand the Producer-Consumer model.
#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");
return 0;
}
48
Exercise 1 (2/2)
fork()
49
Exercise 2 (1/2)
• What is the output of the following program?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("Hello!\n", getpid());
fork();
printf(“Coucou!\n", getpid());
return 0;
}
50
Exercise 2 (2/2)
Output 1:
Parent Process /
Hello 7888 Current Process
Coucou 7888
Hello 7888
Coucou 7889 Coucou 7888
Output 2:
fork()
Hello 7888
Coucou 7889
Coucou 7888
Coucou 7889
N.B: The display differs depending on the
process manager. Depending on the Scheduling Child Process
policy.
51
Exercise 3
• Specify the number of child processes created by the following program:
Current
#include <stdio.h>
Process
#include <sys/types.h>
int main(void)
{
if (fork()==0; Child
{ Process 1
via 1st fork()
fork();
}
Child
Number of child processes created = 2 Process 2
via 2nd fork()
Total number of processes = 3
52
Exercise 4
• Specify the number of child processes created by the following program:
Current
#include <stdio.h>
Process
#include <sys/types.h>
int main(void)
{
if (fork()>0; Child Child
{ Process 2 Process 1
via 2nd fork() via 1st fork()
fork();
}
53
Exercise 5
• What is the output of the following program?
55
Exercise 7
• Write a program in C displaying the processes according to the diagram
below. Each process should display its ID and that of its Parent.
Current
#include <stdio.h>
Process
#include <sys/types.h>
#include <unistd.h>
int main() { Child
printf(“Process ID %d and Parent ID%d\n“, getpid(), getppid()); Process 1
if (fork() == 0)
{ fork();
fork(); Child Child
printf(“Process ID %d and Parent ID %d\n“, getpid(), getppid()); Process 2 Process 3
}
return 0;
} Child
Process 4
56