Lect 2
Lect 2
Chapter 2: Processes
Process Concept
Process Scheduling
Context Switching
Operations on Processes
Inter-process Communication
2
Process Concept
A computer program is a collection of instructions
performing a specific task.
Process – sometimes called a task/job is, informally, a
program in execution
process execution is done in sequential fashion
Components of a process
The program code, also called text section
Current activity including
program counter,
processor registers
4
Process Concept (Cont.)
―Process‖ is not the same as ―program‖
Execution of program started via GUI mouse clicks, command line entry
of its name, by execution of another program etc
6
Cont’d …
new →ready
Admitted to ready queue;
can now be considered by CPU scheduler
ready →running
CPU scheduler chooses that process to execute next, according to
some scheduling algorithm
running →ready
Process has used up its current time slice or
Interrupt has been detected
running →waiting
Process is waiting for some event to occur (for I/O operation to
complete, etc.)
Waiting → ready
Whatever event the process was waiting on has occurred
7
Process Control Block (PCB)
8
Process Scheduling
The Objective of multiprogramming is to have some process
running at all times (to maximize CPU utilization.)
Note:-
Processes migrate among the various queues till it finishes
execution
10
Ready and Wait Queues
11
Representation of Process Scheduling
Queuing diagram is a common representation of process scheduling
Rectangles represents queues (ready and device queues),
Circles represents resources that serve queues,
Arrows indicates flows of process in the system
12
Context Switch
The process of changing the CPU hardware state from one process to
another is called a context switch
This can happen 100 or 1000 times a second!
When CPU switches to another process,
The system must save the state of the old process and
Load the saved state for the new process via a context switch
Context of a process represented in the PCB
Context-switch time is overhead;
the system does no useful work while switching
The more complex the OS and the PCB the longer the context
switch
Time for context switch is dependent on hardware support
Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
context switch time will be minimal
13
CPU Switch From Process to Process
14
Operations on Processes
System must provide mechanisms for:
Process creation,
Process termination,
Inter-process communication and so on as detailed next
15
Process Creation
Processes are created and deleted dynamically
The system creates the first process (init in Linux)
The first process then creates other processes such that:
the creator is called the parent process
the created is called the child process
the parent/child relationships can be expressed by a process tree
16
A Tree of Processes in Linux
init
pid = 1
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
17
Cont...
Reasons for process creation
User logs on
User starts a program
OS creates process to provide a service
(e.g., printer daemon to manage printer)
Program starts another process (e.g., Microsoft word calls Microsoft
excel to edit numerical data)
18
Process Creation
Two ways to create a process
Build a new empty process from scratch
Example: Windows using createProcess() system call
19
Process Creation: Windows
Option 1: New process from scratch
Steps
Create and initialize the process control block (PCB) in the
kernel
Create and initialize a new address space
Load the program into the address space
Copy arguments into memory in the address space
Initialize the hardware context to start execution at ``start'’
Places the PCB on the ready queue
Advantages:
No wasted work
Disadvantages: Difficult to setup process correctly and to express
all possible options
Process permissions, where to write I/O, environment variables
Example: WindowsNT has to call with 10 arguments
20
Creating a Separate Process via Windows API
21
Process Creation
Option 2: Clone existing process and change (UNIX)
Some unix system calls
fork() – system call to create a new process
No arguments!
fork() returns PID of the child to parent, and 0 to the child
exec() – system call used after a fork() to replace the process’ memory
space with a new program
wait() – system call to wait for a process to finish
22
Process Creation (Cont.)
Resource sharing options (parent/child )
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options(parent/child )
Parent and children execute concurrently
Parent waits until children terminate
Address space- a separate address space created for the child.
Child duplicate of parent (sharing the same program and data
segments in memory )
Child has a program loaded into it (all new code and data segments)
23
UNIX Process Management
24
C Program Forking Separate Process
25
Question: What does this code print?
Parent could print first, or child could print first – you don’t know!
26
Question: What does this code print?
27
Process Termination
29
Cont…
Parent may terminate the execution of children processes
using the abort() system call.
Some reasons for doing so:
Child has exceeded allocated resources
Task assigned to child is no longer required
The parent is exiting and the operating systems does
not allow a child to continue if its parent terminates
Arithmetic error, or data misuse (e.g.,wrong type)
Invalid instruction execution
Insufficient memory available, or memory bounds
violation
I/O failure
30
Inter-process communication
32
Interprocess Communication
Processes within a system may be independent or cooperating
Cooperating process can affect or be affected by other processes,
including sharing data
Independent process cannot affect or be affected by the execution of
another process
35
Bounded-Buffer – Shared-Memory Solution
Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
36
Bounded-Buffer – Producer
item next_produced;
while (true) {
/* produce an item in next produced */
nextProduced = makeNewItem( . . . );
37
Bounded Buffer – Consumer
item next_consumed;
while (true) {
/* Wait for an item to become available */
while (in == out)
; /* do nothing */
38
Interprocess Communication – Shared Memory
Advantage:
Fast and easy to share data
Disadvantage:
Must synchronize data accesses; error prone
39
Interprocess Communication – Message Passing
Mechanism for processes to communicate and to synchronize their actions
Message system – processes communicate with each other without
resorting to shared variables
Explicitly pass data between sender (src) + receiver (destination)
40
Message Passing (Cont.)
41
Assignmen-1
Make group having 5-6 members
Pick 1 case/topic from the list below and prepare maximum
of 5 pages report and 15 minuets presentaiton
1. Shared memory implementation in Linux (Nathnael)
2. Remote Procedure call/Remote method Invoacaton (Tilahun)
3. Pipes (Abraham)
4. Socket (Tamagn)
Submission
Summary of the topic not morethan 5 pages and Demo code
Deadline:
September 10, 2023
Evaluation
Presentaiton and the report
42
Summary
43
IPC - Summary
44
Additional readings
Operating system concepts, chapter 3
Modern Operating systems, chapter 2 of section 2.1
45
End of Chapter 2