03 Processes
03 Processes
Chapter 3
Processes
1
Outline
OUTLINE OBJECTIVES
• Process Concept • To introduce the notion of a process
-- a program in execution, which
• Process Scheduling
forms the basis of all computation
• Operations on Processes
• To describe the various features of
• Inter-process Communication processes, including scheduling,
• Examples of IPC Systems creation and termination, and
• Communication in Client-Server communication
Systems • To describe communication in
client-server systems
2
Process Concept and Process
Management
3
Process Concept
4
Process: program in execution
• If we have a single program running in the system, then the task of OS is easy:
– load the program, start it and program runs in CPU
– (from time to time it calls OS to get some service done)
• But if we want to start several processes, then the running program in CPU
(current process) has to be stopped for a while and other program (process)
has to run in CPU.
– Process management becomes an important issue
5
Process: program in execution
CPU
registers
(Physical)
PSW Main
PC Memory
(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
7
Process in Memory
Stack segment
(holds the called function parameters,
local variables, return values)
Data segment
(includes global
variables, arrays, etc., you use)
Text segment
(code segment)
A process needs this memory
(instructions are here)
content to run
(called address space; memory image)
8
Process Address Space
9
Process State
10
Diagram of Process State
11
Process Control Block
12
Process Control Block (PCB)
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
13
PCBs
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.
14
CPU Switch from Process to Process
15
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 */
…
}
16
Example: Processes in Linux
• The /proc file system in Linux is the kernel interface to users to look to
the kernel state (variables, structures, etc.).
– Many subfolders
– One subfolder per process (name of subfolder == pid of process)
17
Process Queues and Scheduling
18
Process Scheduling
Select Dispatch
(Scheduling Algorithm) (mechanism)
19
Scheduling
20
Ready Queue and Various I/O Device
Queues
21
Representation of Process Scheduling
CPU Scheduler
ready queue
I/O queue
22
Schedulers
Short-term
scheduler
CPU
ready queue
job queue
23
Schedulers
24
Process Behavior
waiting waiting
25
Addition of Medium Term Scheduling
26
Context Switch
• 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
27
Process Creation and Termination
28
Process Creation
29
Process Creation (Cont)
• 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
30
C Program Forking Separate Process in
Linux
int main()
{ Parent
pid_t n; // return value of fork; it is process ID pid=x
n=?
/* fork another process */
n = fork(); before fork() executed
if (n < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
} Parent Child pid=y
else if (n == 0) { /* child process */ pid=x
n=y n=0
execlp("/bin/ls", "ls", NULL);
} after fork() executed
else { /* parent process */
/* parent will wait for the child
to complete */
wait (NULL);
Parent
printf ("Child Complete");
pid=x n=y Child pid=y
exit(0);
}
} after execlp() executed
31
Execution Trace: fork()
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
32
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
33
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
}
34
A tree of processes on a typical Solaris
35
Process Termination
36
Process Termination
Parent Child
fork();
….
….
….
….
….
x = wait ();
exit (code);
38
Cooperating Processes and the need for
Interprocess Communication
• Processes within a system may be independent or cooperating
– Independent process cannot affect or be affected by the
execution of another process
– Cooperating process can affect or be affected by the execution
of another process
Application
40
Communication Models
41
Shared Memory IPC Mechanism
42
Shared Memory IPC Mechanism
Buffer
Producer Produced Items Consumer
Process Process
item buffer[BUFFER_SIZE];
int in = 0; // next free position
int out = 0; // first full position
44
Buffer State in Shared Memory
item buffer[BUFFER_SIZE]
Producer Consumer
int out;
int in;
Shared Memory
45
Buffer State in Shared Memory
Buffer Full
in out
((in+1) % BUFFER_SIZE == out) : considered full buffer
Buffer Empty
in out
48
Implementation in a system
49
Naming: Identifying the receiver
send() receive()
Mailbox (mqid)
{.. {…
{ }
Kernel
50
• Synchronization (how does the sender behave if can not send
message immediately)
– Blocking send/receive
– Non-blocking send/receive
• Buffering
– Zero capacity
– Bounded capacity
– Unbounded capacity
51
Synchronization
52
Buffering
53
Synchronization
Sender Receiver
Kernel
Buffer
– 2) POSIX API
– POSIX (Portable Operating System Interface) is the standard API
for Unix like systems.
• shm_open, mmap, shm_unlink
55
Examples of IPC Systems –
POSIX Shared Memory API (derived from SV API)
– When done a process can detach the shared memory from its
address space
shmdt(ptr);
56
Examples of IPC Systems –
another POSIX Shared Memory API
• The following functions are defined to create and manage shared
memory in POSIX API
• shm_open():
– create or open a shared memory region/segment (also
called shared memory object)
• shm_unlink():
– remove the shared memory object
• ftruncate():
– set the size of shared memory region
• mmap():
– map the shared memory into the address space of the
process. With this a process gets a pointer to the shared
memory region and can use that pointer to access the
shared memory.
57
Examples of IPC Systems - Mach
58
Examples of IPC Systems – Windows XP
59
Local Procedure Calls in Windows XP
60
Other IPC methods:
pipes
• Piped and Named-Pipes (FIFOs)
• In Unix/Linux:
– A pipe enables one-way communication
between a parent and child
– It is easy to use.
– When process terminates, pipe is removed
automatically
– pipe() system call
P C
pipe
61
Other IPC methods:
named-pipes (FIFOs)
– A named-pipe is called FIFO.
– It has a name
– When processes terminate, it is not removed automatically
– No need for parent-child relationship
– birectional
– Any two process can create and use named pipes.
P1 P2
a_filename
named pipe
62
Communication Through Network:
Client-Server Communication
63
Communications in Client-Server Systems
• Sockets
• Remote Procedure Calls
• Remote Method Invocation (Java)
64
Sockets
65
Socket Communication
66
Remote Procedure Calls
67
Execution of RPC
68
Remote Method Invocation
69
Marshalling Parameters
70
References
71
Additional Study Material
72