Lecture3 Processes
Lecture3 Processes
Lecture 3
Processes
(chapter 3)
Dr. İbrahim Körpeoğlu
http://www.cs.bilkent.edu.tr/~korpe
• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 7th & 8th editions,
Wiley.
REFERENCES
• Operating System Concepts, 7th and 8th editions, Silberschatz et al.
Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, 2009.
• Process Concept
• Process Scheduling
• Operations on Processes
• Interprocess Communication
• Examples of IPC Systems
• Communication in Client-Server Systems
Stack segment
(holds the called function parameters,
local variables)
Data segment
(includes global
variables, arrays, etc., you use)
Text segment
(code segment)
A process needs this to
(instructions are here)
be in memory
(address space; memory image)
CPU
registers
(Physical)
(Physical)
PSW Main
Main
PC Memory
Memory
(RAM)
(RAM)
IR
CPU state
of the process
(CPU context) process address space
Process processes
Three program counters
A
Process C
B Process
Process Process B
A C
B
Process A
C
time
Conceptual model
what is of three different one process
happening processes executing at
physically a time
Process management
Memory management
Registers
Pointer to text segment info
Program Counter (PC)
Pointer to data segment info
Program status word (PSW)
Pointer to stack segment info
Stack pointer
Process state
File management
Priority
Root directory
Scheduling parameters
Working directory
Process ID
File descriptors
Parent Process
User ID
Time when process started
Group ID
CPU time used
Children’s CPU time ……more
address space
stack stack stack stack
process
data data data data
text text text text
Kernel mains a PCB for each process. They can be linked together in various queues.
CS342 Operating Systems 14 İbrahim Körpeoğlu, Bilkent University
Process Representation in Linux
struct task_struct {
long state; /* state of the process */
….
pid_t pid; /* identifier of the process */
…
unisgned int time_slice; /* scheduling info */
…
struct files_struct *files; /* info about open files */
….
struct mm_struct *mm; /* info about the address space of this process */
…
}
Short-term
scheduler
CPU
ready queue
job queue
CS342 Operating Systems 19 İbrahim Körpeoğlu, Bilkent University
Addition of Medium Term Scheduling
CPU Scheduler
ready queue
I/O queue
• 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
• Time dependent on hardware support
• UNIX examples
– fork system call creates new process
– exec system call used after a fork to replace the process’ memory
space with a new program
Process-Parent Process-Child
stack n y stack n 0
PC
data data
…. ….
text n=fork(); text n=fork();
If (n == 0) If (n == 0)
.. ..
else if (n>0) else if (n>0)
CPU ... ...
PC PC
x pid y pid
PCB-Parent PCB-Child
sys_fork()
Kernel {….}
RAM
CS342 Operating Systems 28 İbrahim Körpeoğlu, Bilkent University
Execution Trace: fork() with execlp()
Process-Parent Process-Child
stack n y stack n 0
PC
data data
…. ….
text n=fork(); text n=fork();
If (n == 0) If (n == 0)
new code
…exec() …exec()
else if (n>0) else if (n>0)
CPU ... ...
PC PC
x pid y pid
PCB-Parent PCB-Child
sys_fork() sys_execve()
Kernel {….} {….}
RAM
CS342 Operating Systems 29 İbrahim Körpeoğlu, Bilkent University
Family of exec() Functions in Unix
Program A Program B
… …
Your Programs execlp(…); execv(…); …..
… …
user
execl(...) execlp(...) execle(...) execv(...) execvp(...) execve(...) mode
C Library {…} {…} {…} {…} {…} {…}
sys_execve(…)
{ kernel
Kernel … mode
}
Parent Child
fork();
….
….
….
….
….
x = wait ();
exit (code);
1) Shared Memory
2) Message Passing
Buffer
Producer Produced Items Consumer
Process Process
item buffer[BUFFER_SIZE];
int in = 0; // next free position
int out = 0; // first full position
item buffer[BUFFER_SIZE]
Producer Consumer
int out;
int in;
Shared Memory
Buffer Full
in out
((in+1) % BUFFER_SIZE == out) : considered full buffer
Buffer Empty
in out
M
Process A
M
Process B
M
Kernel
• Naming
– Direct
– Indirect
• Synchronization
– Blocking send/receive
– Non-blocking send/receive
• Buffering
– Zero capacity
– Bounded capacity
– Unbounded capacity
• Operations
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Process Process
send() receive()
Mailbox
{.. {…
{ }
Kernel
CS342 Operating Systems 50 İbrahim Körpeoğlu, Bilkent University
Indirect Communication
• Mailbox sharing
– P1, P2, and P3 share mailbox A
– P1, sends; P2 and P3 receive
– Who gets the message?
• Solutions
– Allow a link to be associated with at most two processes
– Allow only one process at a time to execute a receive operation
– Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.
– 1) System V API
• System V is one of the earlier Unix versions that introduced shared
memory
– 2) POSIX API
• POSIX (Portable Operating System Interface) is the standard API for
Unix like systems.
• Sockets
• Remote Procedure Calls
• Remote Method Invocation (Java)