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

03 Processes

The document discusses processes in operating systems, defining a process as a program in execution. It describes how operating systems manage multiple concurrent processes using techniques like process scheduling, which selects ready processes and assigns them to the CPU for execution. Process management involves maintaining process state and context in data structures called process control blocks.

Uploaded by

Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

03 Processes

The document discusses processes in operating systems, defining a process as a program in execution. It describes how operating systems manage multiple concurrent processes using techniques like process scheduling, which selects ready processes and assigns them to the CPU for execution. Process management involves maintaining process state and context in data structures called process control blocks.

Uploaded by

Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 72

Operating Systems

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

• Process: a program in execution; process execution must progress in


sequential fashion
• A process includes:
• text – code – section (program counter – PC)
• stack section (stack pointer)
• data section

• set of open files currently used


• set of I/O devices currently used

• An operating system executes a variety of programs:


• Batch systems: jobs
• Time-shared systems: user programs or tasks
– We will use the terms job and process almost interchangeably

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

– To do process switch, we have to save the state/context (register values)


of the CPU which belongs to the stopped program, so that later the
stopped program can be re-started again as if nothing has happened.

5
Process: program in execution

CPU
registers

(Physical)
PSW Main
PC Memory
(RAM)
IR

CPU state
of the process
(CPU context) process address space

(currently used portion of the address space


must be in memory)
6
Multiple Processes
one program counter

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)

Storage for dynamically created


variables

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

• A process can only access its address space

• Each process has its own address space

• Kernel can access everything

9
Process State

• As a process executes, it changes state


– new: The process is being created
– running: Instructions are being executed
– waiting: The process is waiting for some event to occur
– ready: The process is waiting to be assigned to a processor
– terminated: The process has finished execution

In a single-CPU system, only one process may be in running state; many


processes may be in ready and waiting states.

10
Diagram of Process State

11
Process Control Block

Information associated with each process


• Process state (ready, running, waiting, etc)
• Program counter (PC)
• CPU registers
• CPU scheduling information
– Priority of the process, etc.
• Memory-management information
– text/data/stack section pointers, sizes, etc.
– pointer to page table, etc.
• Accounting information
– CPU usage, clock time so far, …
• I/O status information
– List of I/O devices allocated to the process, a list of open files, etc.

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

a PCB of a process may contain this information

13
PCBs

Process 1 Process 2 Process 3 Process N

address space
stack stack stack stack

process
data data data data
text text text text

PCB PCB PCB PCB


1 2 3 N
Kernel Memory

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

In Linux kernel source tree, the file include/linux/sched.h contains


the definition of the structure task_struct, which is the PCB for a process.

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

• Use ps command to see the currently started processes in the system


• Use ps aux to get more detailed information
• See the manual page of the ps to get help about the ps:
– Type: man ps
• The man command gives info about a command, program, library
function, or system call.

• 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

• In a multiprogramming or time-sharing system, there may be multiple


processes ready to execute.

• We need to select one them and give the CPU to that.


– This is scheduling (decision).
– There are various criteria that can be used in the scheduling
decision.

• The scheduling mechanism (dispatcher) than assigns the selected


process to the CPU and starts execution of it.

Select Dispatch
(Scheduling Algorithm) (mechanism)

19
Scheduling

• Ready queue is one of the many queues Process/CPU scheduling


that a process may be added
Device
– CPU scheduling schedules from ready
queue
queue. CPU Device
• Other queues possible:
– Job queue – set of all processes started Ready Device
in the system waiting for memory queue queue
– one process from there Device
– Device queues – set of processes
waiting for an I/O device Memory
• A process will wait in such a queue
until I/O is finished or until the waited
event happens

• Processes migrate among the various Job queue


queues

20
Ready Queue and Various I/O Device
Queues

21
Representation of Process Scheduling

CPU Scheduler

ready queue

I/O queue

22
Schedulers

• Long-term scheduler (or job scheduler) – selects which processes


should be brought into the ready queue
• Short-term scheduler (or CPU scheduler) – selects which process
should be executed next and allocates CPU

Short-term
scheduler
CPU
ready queue

Long-term Main Memory


scheduler

job queue
23
Schedulers

• Short-term scheduler is invoked very frequently (milliseconds) 


(must be fast)
• Long-term scheduler is invoked very infrequently (seconds, minutes)
 (may be slow)
• The long-term scheduler controls the degree of multiprogramming
– i.e. number of processes in memory
– Can also control kind of processes in memory!

• What kind of processes will be in memory?


– A good mix of IO bound and CPU bound processes

24
Process Behavior

• Processes can be described as either:


– I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
– CPU-bound process – spends more time doing computations; few
very long CPU bursts

• CPU burst: the execution of the program in CPU between two


I/O requests (i.e. time period during which the process wants to

continuously run in the CPU without making I/O)


– We may have a short or long CPU burst.
I/O bound CPU bound

waiting waiting
25
Addition of Medium Term Scheduling

Medium term Medium term


scheduler scheduler
Short term
Scheduler
(CPU Scheduler)

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

• 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

27
Process Creation and Termination

28
Process Creation

• Parent process create children processes, which, in turn create other


processes, forming a tree of processes
• Generally, process identified and managed via a process identifier
(pid)
Process

• Resource sharing alternatives:


– Parent and children share all resources
Process Process
– Children share subset of parent’s resources
– Parent and child share no resources
• Execution alternatives:
– Parent and children execute concurrently Process Process Process

– Parent waits until children terminate

29
Process Creation (Cont)

• Child’s address space?


Parent Child
1)
Child has a new address space. AS AS
Child’s address space can contain:
– 1) the copy of the parent (at creation)
– 2) has a new program loaded into it
Parent Child
2)
AS AS

• 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

the shell that


a remote user is using

your local shell

your started programs

35
Process Termination

• Process executes last statement and asks the operating system to


delete it (can use exit system call)
– Output data from child to parent (via wait)
– Process’ resources are deallocated by operating system

• Parent may terminate execution of children processes (abort)


– Child has exceeded allocated resources
– Task assigned to child is no longer required
– If parent is exiting
• Some operating systems do not allow child to continue if its
parent terminates
– All children terminated - cascading termination

36
Process Termination

Parent Child

fork();
….
….
….
….
….
x = wait ();
exit (code);

PCB of parent PCB of child


sys_wait() sys_exit(..)
{ {
…return(..) …
Kernel } }
37
Inter-process Communication (IPC)

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

• Reasons for process cooperation


– Information sharing
Process Process Process
– Computation speed-up
– Modularity (application will
be divided into modules/sub-tasks)
– Convenience (may be better to cooperating process
work with multiple processes)
The overall application is designed
to consist of cooperating processes
39
IPC Mechanisms

• Cooperating processes require a facility/mechanism for inter-process


communication (IPC)

• There are two basic IPC models provided by most systems:

1) Shared memory model


processes use a shared memory to exchange data

2) Message passing model


processes send messages to each other through the kernel

40
Communication Models

message passing approach shared memory approach

41
Shared Memory IPC Mechanism

• A region of shared memory is


established between (among) two or
more processes. Process A
– via the help of the operating system
kernel (i.e. system calls).
shared region

• Processes can read and write shared


memory region (segment) directly as
ordinary memory access (pointer Process B
access)
– During this time, kernel is not
involved.
– Hence it is fast
Kernel

42
Shared Memory IPC Mechanism

• To illustrate use of an IPC mechanism, a general model problem, called


producer-consumer problem, can be used. A lot of problems look like this.

– We have a producer, a consumer, and data is sent from producer to


consumer.
• unbounded-buffer places no practical limit on the size of the buffer
• bounded-buffer assumes that there is a fixed buffer size

Buffer
Producer Produced Items Consumer
Process Process

We can solve this problem via shared memory IPC mechanism


43
Bounded-Buffer – Shared-Memory
Solution
• Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;

item buffer[BUFFER_SIZE];
int in = 0; // next free position
int out = 0; // first full position

Solution is correct, but can only use BUFFER_SIZE-1 elements

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

in == out : empty buffer


46
Bounded-Buffer – Producer and Consumer
Code
while (true) {
Producer
/* Produce an item */
while ( ((in + 1) % BUFFER SIZE) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
Buffer (an array)
in, out integer variable
// remove an item from the buffer
item = buffer[out];
Shared Memory out = (out + 1) % BUFFER SIZE;
return item;
}
47
Message Passing IPC Mechanism

• Another mechanism for processes to communicate and to synchronize their


actions
• With message paasing system processes communicate with each other
without resorting to shared variables

• This IPC facility provides two operations: messages


– send(message) – message size fixed or variable passed
– receive(message) through

• If P and Q wish to communicate, they need to: P Q


– establish a (logical) communication link
between them Logical
– exchange messages via send/receive Communication
Link

48
Implementation in a system

• The messaging passing facility can be • How are links established?


implemented in various ways. – Explicitly by the process? Or
implicitly by the kernel?
• That means the facility may have
different features depending on the • Can a link be associated with more
system than two processes?

• How many links can there be between


every pair of communicating processes?

• What is the capacity of a link?

• Is the size of a message that the link


can accommodate fixed or variable?

• Is a link unidirectional or bi-directional?

49
Naming: Identifying the receiver

• Naming (how do we identify the receiver)


– Direct naming and communication
• Receiver processes is explicitly specified
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from process Q

– Indirect naming and communicaiton


• Messages are directed and received from mailboxes (also
referred to as ports)
– send (mqid, message) Process
Process
– receive (mqid, message)

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

• How does the sender/receiver behave if it can not send/receive the


message immediately
– Depend if Blocking or Non-Blocking communication is used

• Blocking is considered synchronous


– Sender blocks block until receiver or kernel receives
– Receiver blocks until message available
• Non-blocking is considered asynchronous
– Sender sends the message really or tries later, but always returns
immediately
– Receiver receives a valid message or null, but always returns
immediately

52
Buffering

• Exact behavior depends also on the Available Buffer

• Queue of messages attached to the link; implemented in one of three


ways
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits

53
Synchronization

Sender Receiver

Kernel
Buffer

Zero Buffer Some Buffer

Blocking Send Wait until receiver Wait until kernel


receives receives (if buffer
has space no wait) What if there
Blocking Receive Wait until sender has Wait until kernel has
would
a message a message (if buffer be infinite
has space no wait) buffer?
Nonblocking Send Return with receiver Return with kernel
received the received the
message or error message or error

Nonblocking Receive Return with a Return with a


message or none message or none
54
Examples of IPC Systems: Unix/Linux
Shared Memory
• There are two different APIs that provide functions for shared memory
in Unix/Linux operating system

– 1) POSIX System V API


• This POSIX standard API is historically called System V API.
– System V (System Five) is one of the earlier Unix versions that
introduced shared memory
• shmget, shmat, shmdt, …

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

• POSIX + System V Shared Memory API


– Process first creates shared memory segment
segment id = shmget(IPC PRIVATE, size, S IRUSR | S IWUSR);

– Process wanting access to that shared memory must attach to it


ptr = (char *) shmat(id, NULL, 0);

– Now the process could write to the shared memory


sprintf(ptr, "Writing to shared memory");

– 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

• Mach communication is message based


– Even system calls are messages
– Each task gets two mailboxes at creation- Kernel and Notify
– Only three system calls needed for message transfer
msg_send(), msg_receive(), msg_rpc()
– Mailboxes needed for commuication, created via
port_allocate()

58
Examples of IPC Systems – Windows XP

• Message-passing centric via local procedure call (LPC) facility


– Only works between processes on the same system
– Uses ports (like mailboxes) to establish and maintain
communication channels
– Communication works as follows:
• The client opens a handle to the subsystem’s connection port
object
• The client sends a connection request
• The server creates two private communication ports and
returns the handle to one of them to the client
• The client and server use the corresponding port handle to
send messages or callbacks and to listen for replies

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

• A socket is defined as an endpoint for communication


• Concatenation of IP address and port
• The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
• Communication consists between a pair of sockets

65
Socket Communication

66
Remote Procedure Calls

• Remote procedure call (RPC) abstracts procedure calls between


processes on networked systems
• Stubs – client-side proxy for the actual procedure on the server
• The client-side stub locates the server and marshalls the parameters
• The server-side stub receives this message, unpacks the marshalled
parameters, and peforms the procedure on the server

67
Execution of RPC

68
Remote Method Invocation

• Remote Method Invocation (RMI) is a Java mechanism similar to


RPCs
• RMI allows a Java program on one machine to invoke a method on a
remote object

69
Marshalling Parameters

70
References

• 1. Operating System Concepts, 7th and 8th editions, Silberschatz et al.


Wiley.
• 2. Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition,
2009.
• 3. The slides here are adapted/modified from the textbook and its
slides: Operating System Concepts, Silberschatz et al., 7th & 8th
editions, Wiley.

71
Additional Study Material

72

You might also like