0% found this document useful (0 votes)
4 views

Chapter 2 - Processes

The document outlines key concepts related to process management in operating systems, including definitions, states, control blocks, scheduling, and inter-process communication. It explains the differences between processes and threads, multitasking, and the importance of context switching. Additionally, it covers process creation and termination, along with examples such as the architecture of the Chrome browser and the use of system calls in Unix and Windows for process management.

Uploaded by

Jawad Chahine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 2 - Processes

The document outlines key concepts related to process management in operating systems, including definitions, states, control blocks, scheduling, and inter-process communication. It explains the differences between processes and threads, multitasking, and the importance of context switching. Additionally, it covers process creation and termination, along with examples such as the architecture of the Chrome browser and the use of system calls in Unix and Windows for process management.

Uploaded by

Jawad Chahine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

𝐸𝒹𝓊𝒸𝒶𝓉𝒾𝑜𝓃 𝒾𝓈 𝓉𝒽𝑒 𝓂𝑜𝓈𝓉 𝓅𝑜𝓌𝑒𝓇𝒻𝓊ℓ 𝓌𝑒𝒶𝓅𝑜𝓃 𝓌𝒽𝒾𝒸𝒽 𝓎𝑜𝓊 𝒸𝒶𝓃 𝓊𝓈𝑒

𝓉𝑜 𝒸𝒽𝒶𝓃𝑔𝑒 𝓉𝒽𝑒 𝓌𝑜𝓇ℓ𝒹, 𝒩𝑒ℓ𝓈𝑜𝓃 𝑀𝒶𝓃𝒹𝑒ℓ𝒶

Processes

14 February 2025 1
Outline

 Process Definition and Management.


 Process States Model: 5 States Model.
 Process Control Block: PCB.
 Process Scheduling.
 Context Switch.
 Operation on Processes: Creation, Termination, Inter-process Communication.

14 February 2025 2 Process


Learning Outcomes

 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.

14 February 2025 3 Process


Process Definition and Management

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

14 February 2025 4 Process


Process Definition and Management

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

14 February 2025 5 Thread Process


Process Definition and Management

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.

 OS achieves multitasking by executing many processes concurrently. It is similar to


parallel computing, but permits to time-share the same hardware to many processes.

14 February 2025 6 Process


Process Definition and Management

Example Multi-Process Architecture: Chrome Browser


Google Chrome Browser is a multi-process
application, with 3 types of processes.
Process Task
process manages the user interface and
performs network I/O. A new browser process
Browser
is created when Chrome is started. Only one
browser process is created.
processes render web pages, parse HTML, run
Renderer Javascript code, etc. A new renderer created
for each website opened.
A plug-in process is created for each type of
plug-in (such as Adobe Flash Player) in use. Adobe Flash Player (known in IE, Firefox, and Google
Plug-in processes contain the code for the plug- Chrome as Shockwave Flash) is computer software for
Plug-in in as well as additional code that enables the content created on the Adobe Flash platform. Flash Player
plug-in to communicate with associated is capable of viewing multimedia contents, executing rich
renderer processes and the browser process. Internet applications, and streaming audio and video.
14 February 2025 7 Process
Process Definition and Management

Example Multi-Process Architecture: Chrome Browser

14 February 2025 8 Process


Process State

Introduction and basic definitions

Process Execute Change State

The State of a process is defined by the current activity of that process

When a process is executing, it changes from a State to another

The State of a process can be easily understood by asking the following question:
What is the process is currently doing?

14 February 2025 9 Process


Process State

Five Process States

New The process is being created.

Running Instructions are being executed.

The process is waiting for some events to occur.


Waiting
(Such as I/O completion or reception of a signal)

Ready The process is waiting to be assigned to a processor.

Terminated The process has finished execution.

14 February 2025 10 Process


Process State

Diagram of Process State

New Terminated

Interrupt
Admitted
Exit

Ready Running

Scheduler dispatch

Waiting
I/O or Event Completion I/O or Event Wait

14 February 2025 11 Process


PCB: Process Control Block

 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

14 February 2025 12 Process


PCB: Process Control Block

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 */

14 February 2025 13 Process


Process Scheduling

Basic Concepts
The objective of multiprogramming is to have process running at
First Concept
all times, to maximize CPU utilization.

The objective of time sharing is to switch the CPU among


Second Concept processes so frequently that users can interact with each program
while it is running.

To meet these two objectives, the process scheduler selects an available


process (possibly from a set of several available processes) for program
execution on the CPU.

14 February 2025 14 Process


Process Scheduling

Single Processor System


 For a single processor system, there will never be more than one running process.
 If there are more processes, the rest will have to wait until the CPU is free and can be
rescheduled.

Which process will be executed?


What will happen if a process
with a higher priority came?
Will the chosen process take the
total time to execute?

14 February 2025 15 Process


Process Scheduling

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

14 February 2025 16 Process


Process Scheduling

Scheduling Queues

14 February 2025 17 Process


Process Scheduling

Scheduler: Long Term and Short Term Schedulers


 Often, in a batch system, more processes are submitted than can be executed immediately.
These processes are spooled to a mass-storage device (typically a disk), where they are kept
for later execution.
 The long-term scheduler, or job scheduler, selects processes from this pool and loads them
into memory for execution.
 The short-term scheduler, or CPU scheduler, selects from among the processes that are ready
to execute and allocates the CPU to one of them.
 The primary distinction between these two schedulers lies in frequency of execution: The
short-term scheduler must select a new process for the CPU frequently while The long-term
scheduler executes much less frequently; minutes may separate the creation of one new
process and the next.
 It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-
bound processes.

14 February 2025 18 Process


Process Scheduling

Scheduler: Medium-Term Scheduler


Swap In Partially executed Swap Out
Swapped-out processes

Ready Queue CPU End

I/O I/O 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.

14 February 2025 19 Process


Context Switch

Analogy from the Physical World

3- Father marks the book


Let us suppose that this
1- Father is Reading a book represents saving the first
Let us suppose that reading the book process data
2- Son falls down while playing.
is a running process.
Let us suppose this is the interrupt

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

14 February 2025 20 Process


Context Switch

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.

14 February 2025 21 Process


Context Switch

A context switch occurs when a CPU core


switches from one process to another. OS must
save the context of the current process and load
the context of the new process.
• Scheduler is a kernel component that takes the
decision to perform a context switch and selects the
next process to dispatch.
• There is another situation where the context must be
saved and restored: Interrupt. When an interrupt
occurs, a CPU core is forced to call an interrupt
handler. The handler must save the context of the
interrupted process, then restore it when it finishes
the work.

14 February 2025 22 Process


Context Switch

Context Switch and Performance

 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.

 Typical speeds are few milliseconds.

14 February 2025 23 Process


Operation on Processes

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

14 February 2025 24 Process


Operation on Processes

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

Netscape emacs csh cat


pid=7785 pid=8108 pid=1400 pid=2536

14 February 2025 25 Process


Operation on Processes

Process Creation

When a process create a new process


Two Possibilities in The parent continues to execute concurrently with its children.
terms of Execution The parent waits until some or all of its children have terminated.
Two Possibilities in The children may have all the resources that the parent is having.
terms of Resource
Sharing The children may have a part of the resources that the parent is having.

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.

14 February 2025 26 Process


Operation on Processes

Process Creation: Creating a Process in Unix

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.

14 February 2025 27 Process


Operation on Processes

Process Creation: Creating a Process via Windows API


The CreateProcess() system call directly specifies
a command to execute in child process.
Child info (i.e. pid) is returned in si & pi
structures.
WaitForSingleObject() parent waits child.

This CreateProcess() function, creates a child


process that loads the application
mspaint.exe.

Windows does not support fork().

14 February 2025 28 Process


Operation on Processes

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.

14 February 2025 29 Process


Operation on Processes

IPC: Inter-Process Communication


Process executing concurrently in the Operating System may be either
independent processes or cooperating processes.

Process Type Explanation


Independent Process They can not affect or be affected by the other processes running in the
system.
Cooperate Process They can affect or be affected by the other processes running in the
system.

IPC occurs with cooperating processes

14 February 2025 30 Process


Operation on Processes

IPC: Inter-Process Communication


Needs for Inter-Process Communication

Information Sharing Computation Speed Up Convenience

14 February 2025 31 Process


Operation on Processes

IPC: Inter-Process Communication


Types of Communication
Type of Communication Explanation
A region of memory that is shared by cooperating process is established.
Shared Memory Processes can then exchange information by reading and writing data to the
shared region.
Communication takes place by means of messages exchanged between the
Message Passing
operating processes.
This method is similar to a shared file, where one process writes and
Pipes
another reads (Unidirectional).
A socket is defined as an endpoint for communication. A pair of processes
communicating over a network employs a pair of sockets one for each
Sockets
process. A socket is identified by an IP address concatenated with a port
number.

14 February 2025 32 Process


Operation on Processes

IPC: Inter-Process Communication


Producer-Consumer Model

Process A Buffer Process B

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.

14 February 2025 33 Process


Operation on Processes

IPC: Inter-Process Communication


Producer-Consumer Model

Input

Output Practical Examples


Print Spooling
I/O

I/O. A device driver Applications print files by


collects input and an copying them into some
application processes it. folder. A spooler process
takes the files sends them
one by one to the printer.

14 February 2025 34 Process


Operation on Processes

IPC: Inter-Process Communication


Shared Memory

Memory Accessible by
Process A
Process Process
A B
Shared Memory between
processes A and B

Memory Accessible by
Bidirectional
Process B

14 February 2025 35 Process


Operation on Processes

IPC: Inter-Process Communication


Shared Memory: Consumer-Producer Model
This method needs to create a memory area accessible by the collaborating processes. It is organized as a
cyclic buffer.

/* Consumer’s main loop */ /* Producer’s main loop */


typedef struct { . . . } item; item next_consumed; item next_produced;
item buffer[5]; shared variables while(true) { while(true) {
int in = 0, out = 0; while (in == out) /* produce an item... */
{ /* empty, do nothing */ } while((in+1) % 5 == out)
𝒊𝒏 → next free position in the buffer. next_consumed = buffer[out]; { /* full, do nothing */ }
𝒐𝒖𝒕 → first full position in the buffer. out = (out+1) % 5; buffer[in] = next_produced;
/* consume the item... */ in = (in+1) % 5;
} }

In the above example, producer and consumer are managing the shared buffer as a cyclic buffer

14 February 2025 36 Process


Operation on Processes

IPC: Inter-Process Communication


Shared Memory

 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.

 When many processes access shared data concurrently, an important issue is


Synchronization. The code given in the previous slide is not synchronized!

 In this case, the buffer is created and managed by the user processes explicitly, as a circular
buffer data structure, accessible by both processes.

14 February 2025 37 Process


Operation on Processes

IPC: Inter-Process Communication


Message Passing

Direct Link
send(pid, msg), recv(pid, msg)

Process Process
A B
Indirect Link
send(mbox, msg), recv(mbox, msg)

Bidirectional MAILBOX

14 February 2025 38 Process


Operation on Processes

IPC: Inter-Process Communication


Message Passing
Mechanism for processes to communicate and synchronize their actions without
explicit shared variables, using primitives send and receive.

Link Type Explanation


Direct link the other process explicitly specified: send(pid, msg) , recv(pid, msg)
messages are exchanged via mailbox. Processes don’t need to know each
Indirect link other in advance. send(mbox, msg), recv(mbox, msg).

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).

14 February 2025 39 Process


Operation on Processes

IPC: Inter-Process Communication


Message Passing: Consumer-Producer Model

/* Consumer’s main loop */ /* Producer’s main loop */


Message next_consumed; Message next_produced;
while(true) { while(true) {
recv(producer, next_consumed) /* produce an item... */
/* consume the item... */ send(consumer, next_produced)
} }

14 February 2025 40 Process


Operation on Processes

IPC: Inter-Process Communication


Characteristics of Shared Memory and Message Passing

Message
Shared
Passing
Memory

- Typically implemented using system calls and thus


require the more time-consuming task of kernel
System calls are required only to establish intervention.
shared memory regions. Once shared memory - Useful for exchanging smaller amounts of data.
is established, all accesses are treated as routine - Easier to implement in a distributed system than
memory accesses, and no assistance from the Shared Memory.
kernel is required. -With several processing cores, message passing
provides better performance than shared memory.

14 February 2025 41 Process


Operation on Processes

IPC: Inter-Process Communication


Pipes
 A pipe is similar to having an intermediate file, where one process writes and another
process reads. However, the pipe exists in main memory, not on disk, so it is faster.
 The system calls to read and write to/from a pipe are the same used for accessing files ie.
READ(fd, …), WRITE(fd, …).

There are two type of pipes:


 Ordinary pipe in UNIX (Anonymous pipes in Windows) is created by a parent to
communicate with a child process.
 Named pipe (aka FIFO) accessible by any process.
In both cases Pipe is Unidirectional, but we can create two pipes, one in each direction.

14 February 2025 42 Process


Operation on Processes

IPC: Inter-Process Communication


Pipes: Parent and Child processes Communication

14 February 2025 43 Process


Operation on Processes

IPC: Inter-Process Communication


Pipes: Parent and Child processes Communication

 Descriptor fd[0] is for reading


and fd[1] is for writing.
 Whatever is written into fd[1]
can be read from fd[0].

fd[0] fd[1]

Reading Writing

Part of a C Code in UNIX

14 February 2025 44 Process


Operation on Processes

IPC: Inter-Process Communication


Using Pipe in Command Line Interface (CLI)
CLI users can “redirect” the input (output) of a program from (to) a file. They can also create
a “pipe” between two programs.

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:

ifconfig > conf.txt ifconfig | grep “192”

The output of ifconfig command is sent to conf.txt The output of ifconfig command is executed by grep “192”

14 February 2025 45 Process


Learning Outcomes

 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.

14 February 2025 46 Process


Process
14 February 2025 47
Exercise 1 (1/2)
• What is the output of the following program?

#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)

Output : Parent Process /


Hello world!
Hello world! Current Process
Hello world!

fork()

Hello world! Child Process

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?

Current Hello 7888


#include <stdio.h> Process Coucou 7888
#include <sys/types.h>
#include <unistd.h>
int main()
{ Child Child
Coucou 7889 Coucou 7891
printf("Hello!\n", getpid()); Process 1 Process 2
fork();
fork();
printf(“Coucou!\n", getpid()); Child
Coucou 7890
return 0; Process 3
}
54
Exercise 6
• What is the output of the following program?

Current Hello 7888


#include <stdio.h> Coucou 7888
Process
#include <sys/types.h>
#include <unistd.h>
int main()
{ pid_t p1 = fork(); Child
if (p1==0) { fork(); Process 1
exit(); }
printf(“Hello!\n", getpid());
printf(“Coucou!\n", getpid()); Child
} Process 2

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy