Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Operating Systems - Ch3 - Mod - Reem

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

CSCI315

Operating Systems- Processes


Dr. Reem Ibrahim

Operating System Concepts, Avi Silberschatz, Peter Baer Galvin, Greg Gagne,
John Wiley & Sons, ISBN 978-1-118-06333-0 , 9th Ed., 2012.
Processes
 Process Concept
 Process Scheduling
 Operations on Processes
 Interprocess Communication
 Examples of IPC Systems

2 Operating System Dr. Reem Ibrahim


Process Concept
 An operating system executes a variety of programs:
 Batch system – jobs
 Time-shared systems – user programs or tasks
 Process – a program in execution; process execution must
progress in sequential fashion.
 Program is passive entity stored on disk (executable file),
process is active
 Program becomes process when executable file loaded into memory
 Execution of program started via GUI mouse clicks, command
line entry of its name, etc
 One program can be several processes
 Consider multiple users executing the same program

3 Operating System Dr. Reem Ibrahim


Process Concept
 A process generally also includes:
Max
 The program code, also called text section
Data section
containing global
variables

Stack containing
temporary data
Function parameters,
return addresses, 0
local variables
Process in Memory

Heap containing
memory dynamically
allocated during run
time
4 Operating System Dr. Reem Ibrahim
Process Concept
 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 Diagram of Process State
to be assigned to a processor
terminated: The process has
finished execution
5 Operating System Dr. Reem Ibrahim
Process Concept
 Process Control Block (PCB)
 Each process is represented in the operating system by a
PCB—also called a task control block.
 It contains many pieces of information associated with a
specific process, including these:
 Process state – running, waiting, etc
 Program counter – location of instruction to next
execute
 CPU registers – contents of all process-centric registers
 CPU scheduling information- priorities, scheduling
queue pointers
 Memory-management information – memory
allocated to the process
 Accounting information – CPU used, clock time
elapsed since start, time limits
 I/O status information – I/O devices allocated to
process, list of open files

6 Operating System Dr. Reem Ibrahim


Process Concept
 CPU Switch From Process to Process

7 Operating System Dr. Reem Ibrahim


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
 The more complex the OS and the PCB  the longer the context switch
 Time dependent on hardware support
 Some hardware provides multiple sets of registers per CPU  multiple
contexts loaded at once

8 Operating System Dr. Reem Ibrahim


Process Scheduling
 Maximize CPU use, quickly switch processes onto CPU for time sharing
 Process scheduler selects among available processes for next
execution on CPU
 Maintains scheduling queues of processes
 Job queue – set of all processes in the system
 Ready queue – set of all processes residing in main memory, ready and
waiting to execute
 Device queues – set of processes waiting for an I/O device

9 Operating System Dr. Reem Ibrahim


Process Scheduling
 A common representation of process
scheduling is a queuing diagram,
 Each rectangular box represents a
queue. Two types of queues are
present: the ready queue and a set of
device queues.
 The circles represent the resources
that serve the queues,
 arrows indicate the flow of processes
 A new process is put in the ready
queue. It waits there until it is selected
for execution, or dispatched.
 one of several events could occur:
 The process could issue an I/O request and then be placed in an I/O queue.
 The process could create a new child process and wait for the child’s termination.
 The process could be removed forcibly from the CPU, as a result of an interrupt, and be
put back in the ready queue
10 Operating System Dr. Reem Ibrahim
Process Scheduling
 Schedulers
 Short-term scheduler (or CPU scheduler) – selects which process should be
executed next and allocates CPU
 Short-term scheduler is invoked frequently (milliseconds)  (must be fast)

 Long-term scheduler (or job scheduler) – selects which processes should be


brought into the ready queue
 Long-term scheduler is invoked infrequently (seconds, minutes)  (may be slow)
 The long-term scheduler controls the degree of multiprogramming

 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

11 Operating System Dr. Reem Ibrahim


Process Scheduling
 Schedulers
 Medium-term scheduler can be added if degree of multiple programming needs
to decrease
 Remove process from memory, store on disk, bring back in from disk to continue
execution: swapping

12 Operating System Dr. Reem Ibrahim


Operations on Processes
 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)
 Resource sharing options init
pid = 1

 Parent and children share all resources


 Children share subset of parent’s resources
Parent and child share no resources
login kthreadd sshd
 pid = 8415 pid = 2 pid = 3028

 Execution options
 Parent and children execute concurrently bash khelper pdflush sshd
pid = 8416 pid = 6 pid = 200 pid = 3610
 Parent waits until children terminate
A tree of processes
emacs
on a typical Linux tcsch
ps
pid = 9298 pid = 9204 system pid = 4005

13 Operating System Dr. Reem Ibrahim


Operations on Processes
 Process Creation(UNIX example)
 fork() system call creates new process
 exec() system call used after a fork() to replace the process’ memory space
with a new program

Creating a separate process using the UNIX


fork( ) system call

14 Operating System Dr. Reem Ibrahim


Operations on Processes
 Process Creation
 Windows API example
 CreateProcess() function, which is similar to
fork() in that a parent creates a new child
process.
 The two parameters passed to the
CreateProcess() function are instances of the
STARTUPINFO and PROCESS INFORMATION
structures. STARTUPINFO specifies many
properties of the new process, such as window
size and appearance and handles to standard
input and output files.
The PROCESSINFORMATION structure
contains a handle and the identifiers to the
newly created process and its thread.
 We invoke the ZeroMemory() function to
allocate memory for each of these structures
before proceeding with CreateProcess().
15 Operating System Dr. Reem Ibrahim
Operations on Processes
 Process Termination
 Process executes last statement and then asks the operating system to delete it
using the exit() system call.
 Returns status data from child to parent (via wait())

 Process’ resources are deallocated by operating system


 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.
 The parent process may wait for termination of a child process by using the
wait()system call. The call returns status information and the pid of the terminated
process
pid = wait(&status);

16 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Processes within a system may be independent or cooperating
 Cooperating process can affect or be affected by other processes,
including sharing data
 Reasons for cooperating processes:
 Information sharing: Since several users may be interested in the same
piece of information.
 Computation speedup: can be achieved only if the computer has multiple
processing cores.
 Modularity: dividing the system functions into separate processes or threads.
 Convenience: individual user may work on many tasks at the same time.
 Cooperating processes need interprocess communication (IPC)
 Two models of IPC
 Shared memory
 Message passing

17 Operating System Dr. Reem Ibrahim


Interprocess Communication
(a) Message passing. (b) shared memory.

18 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Shared Memory
 Cooperating process can affect or be affected by the execution of
another process.
 Producer-Consumer Problem : Paradigm for cooperating
processes, producer process produces information that is
consumed by a consumer process
 unbounded-buffer places no practical limit on the size of the buffer
 bounded-buffer assumes that there is a fixed buffer size

Bounded-Buffer –
Shared-Memory Solution

19 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Shared Memory
 The shared buffer is implemented as a circular array with two logical
pointers: in and out. The variable in points to the next free position in
the buffer; out points to the first full position in the buffer.
 The buffer is empty when in == out; the buffer is full when ((in + 1)%
BUFFER SIZE) ==out.

The producer process The consumer process using shared memory.


20 Operating System Dr. Reem Ibrahim
Interprocess Communication
 Message-Passing Systems
 Message passing provides a mechanism to allow processes to communicate
and to synchronize their actions without sharing the same address space.

 If processes P and Q wish to communicate, they need to:


 Establish a communication link between them
 Exchange messages via send/receive.

 Implementation of communication link


 Physical (Shared memory , Hardware bus, Network)
 Logical( Direct or indirect, Synchronous or asynchronous,
Automatic or explicit buffering)
 There are several issues related with features like (Naming , Synchronization ,
Buffering )

21 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Direct Communication
 Processes must name each other explicitly:
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process Q
 Properties of communication link
 Links are established automatically
 A link is associated with exactly one pair of communicating processes
 Between each pair there exists exactly one link

22 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Indirect Communication
 Messages are directed and received from mailboxes (also
referred to as ports)
 Each mailbox has a unique id
 Processes can communicate only if they share a mailbox.
 Primitives are defined as:
 send(A, message)—Send a message to mailbox A.
 receive(A, message)—Receive a message from mailbox A.
 Properties of communication link
 Link established only if processes share a common mailbox
 A link may be associated with many processes
 Each pair of processes may share several communication links

23 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Indirect Communication

 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.
24 Operating System Dr. Reem Ibrahim
Interprocess Communication
 Synchronization
 Message passing may be either blocking or non-blocking
 Blocking is considered synchronous
 Blocking send -- the sender is blocked until the message is received
 Blocking receive -- the receiver is blocked until a message is available
 Non-blocking is considered asynchronous
 Non-blocking send -- the sender sends the message and continue
 Non-blocking receive -- the receiver receives:
 A valid message, or
 Null message

Producer-consumer Problem

25 Operating System Dr. Reem Ibrahim


Interprocess Communication
 Buffering
 Queue of messages attached to the link.
 implemented in one of three ways
 1. Zero capacity – The queue has a maximum length of zero; thus, the
link cannot have any messages waiting in it. In this case, the sender must
block until the recipient receives the message

2. Bounded capacity – finite length of n messages. Sender must wait if


link full
3. Unbounded capacity – infinite length .Sender never waits

26 Operating System Dr. Reem Ibrahim


Examples of IPC Systems
 An Example: POSIX (POSIX Shared Memory)
 Process first creates shared memory segment
 It opens a POSIX shared memory object and makes it available to the calling
process via the returned file descriptor.
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
Set the size of the object
ftruncate(shm fd, 4096);
Now the process could write to the shared memory
sprintf(shared memory, "Writing to shared memory");
 Map a POSIX shared memory object to the calling process's virtual address
space. addr specifies the address at which it should be mapped.
void *mmap (void *addr, size_t length,00int prot, int flags, int fd, off_t offset);
 shm_unlink removes the previously created POSIX shared memory object.
int shm_unlink (const char *name(
27 Operating System Dr. Reem Ibrahim
Examples of IPC Systems
 An Example: POSIX
IPC POSIX Producer IPC POSIX Consumer

28 Operating System Dr. Reem Ibrahim


Examples of IPC Systems
 An Example: 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()
 Send and receive are flexible, for example four options if mailbox full:
 Wait indefinitely
 Wait at most n milliseconds
 Return immediately
 Temporarily cache a message

29 Operating System Dr. Reem Ibrahim


Examples of IPC Systems
 An Example: Windows
 Message-passing centric via advanced 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.

30 Operating System Dr. Reem Ibrahim

You might also like