Process Concept n An operating system executes a variety of programs: –Batch system – jobs...
-
date post
21-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Process Concept n An operating system executes a variety of programs: –Batch system – jobs...
Process ConceptProcess Concept
An operating system executes a An operating system executes a variety of programs:variety of programs:– Batch system – jobsBatch system – jobs– Time-shared systems – user programs Time-shared systems – user programs
or tasksor tasks Textbook uses the terms Textbook uses the terms jobjob and and
processprocess almost interchangeably. almost interchangeably.
Process Concept Process Concept (cont)(cont)
Process – a program in execution; Process – a program in execution; process execution must progress process execution must progress in sequential fashion.in sequential fashion.
A process includes:A process includes:– program counter program counter – stackstack– data sectiondata section
Process StateProcess State As a process executes, it changes As a process executes, it changes statestate
– newnew: The process is being created.: The process is being created.– runningrunning: Instructions are being executed.: Instructions are being executed.– waitingwaiting: The process is waiting for some event to : The process is waiting for some event to
occur.occur.– readyready: The process is waiting to be assigned to a : The process is waiting to be assigned to a
process.process.– terminatedterminated: The process has finished execution.: The process has finished execution.
Diagram of Process StateDiagram of Process State
Process Control Block Process Control Block (PCB)(PCB)
Information associated with each process.Information associated with each process. Process stateProcess state Program counterProgram counter CPU registersCPU registers CPU scheduling informationCPU scheduling information Memory-management informationMemory-management information Accounting informationAccounting information I/O status informationI/O status information
Process Control Block Process Control Block (cont)(cont)
CPU Switch From Process CPU Switch From Process to Processto Process
Process Scheduling Process Scheduling QueuesQueues
Job queueJob queue – set of all processes in the system. – set of all processes in the system. Ready queueReady queue – set of all processes residing in main – set of all processes residing in main
memory, ready and waiting to execute.memory, ready and waiting to execute. Device queuesDevice queues – set of processes waiting for an I/O device. – set of processes waiting for an I/O device. Process migration between the various queues.Process migration between the various queues.
Ready Queue And Various I/O Ready Queue And Various I/O Device QueuesDevice Queues
Representation of Process Representation of Process SchedulingScheduling
SchedulersSchedulers Long-term scheduler (or job scheduler)Long-term scheduler (or job scheduler)
– Selects which processes should be brought into the ready queue.Selects which processes should be brought into the ready queue. Short-term scheduler (or CPU scheduler) Short-term scheduler (or CPU scheduler)
– Selects which process should be executed next and allocates CPU.Selects which process should be executed next and allocates CPU.
Addition of Medium Term Addition of Medium Term SchedulingScheduling
Schedulers Schedulers (cont)(cont)
Short-term scheduler is invoked Short-term scheduler is invoked very frequently (milliseconds) very frequently (milliseconds) (must be fast).(must be fast).
Long-term scheduler is invoked Long-term scheduler is invoked very infrequently (seconds, very infrequently (seconds, minutes) minutes) (may be slow). (may be slow).
The long-term scheduler controls The long-term scheduler controls the the degree of multiprogramming.degree of multiprogramming.
Schedulers Schedulers (cont)(cont)
Processes can be described as Processes can be described as either:either:– I/O-I/O-bound processbound process – spends more – spends more
time doing I/O than computations, time doing I/O than computations, many short CPU bursts.many short CPU bursts.
– CPU-CPU-bound processbound process – spends more – spends more time doing computations; few very time doing computations; few very long CPU bursts.long CPU bursts.
Context SwitchContext Switch When CPU switches to another process, the system When CPU switches to another process, the system
must save the state of the old process and load the must save the state of the old process and load the saved state for the new process.saved state for the new process.
Context-switch time is overhead; the system does Context-switch time is overhead; the system does no useful work while switching.no useful work while switching.
Time dependent on hardware support.Time dependent on hardware support.
Process CreationProcess Creation
Parent process creates children processes, Parent process creates children processes, which, in turn create other processes, which, in turn create other processes, forming a tree of processes.forming a tree of processes.
Resource sharingResource sharing– Parent and children share all resources.Parent and children share all resources.– Children share subset of parent’s resources.Children share subset of parent’s resources.– Parent and child share no resources.Parent and child share no resources.
ExecutionExecution– Parent and children execute concurrently.Parent and children execute concurrently.– Parent waits until children terminate.Parent waits until children terminate.
Process Creation Process Creation (cont)(cont)
Address spaceAddress space– Child duplicate of parent.Child duplicate of parent.– Child has a program loaded into it.Child has a program loaded into it.
UNIX examplesUNIX examples– forkfork system call creates new process system call creates new process– execveexecve system call used after a system call used after a forkfork
to replace the process’ memory space to replace the process’ memory space with a new program.with a new program.
A Tree of Processes On A A Tree of Processes On A Typical UNIX SystemTypical UNIX System
Process TerminationProcess Termination
Process executes last statement Process executes last statement and asks the operating system to and asks the operating system to delete it (delete it (exitexit).).– Output data from child to parent (via Output data from child to parent (via waitwait).).
– Process’ resources are deallocated by Process’ resources are deallocated by operating system.operating system.
Process Termination Process Termination (cont)(cont)
Parent may terminate execution of Parent may terminate execution of children processes (children processes (abortabort).).– Child has exceeded allocated resources.Child has exceeded allocated resources.– Task assigned to child is no longer Task assigned to child is no longer
required.required.– Parent is exiting.Parent is exiting.
Operating system does not allow child to Operating system does not allow child to continue if its parent terminates.continue if its parent terminates.
Cascading termination.Cascading termination.
Cooperating ProcessesCooperating Processes
IndependentIndependent process cannot affect or be process cannot affect or be affected by the execution of another affected by the execution of another process.process.
CooperatingCooperating process can affect or be process can affect or be affected by the execution of another processaffected by the execution of another process
Advantages of process cooperationAdvantages of process cooperation– Information sharing Information sharing – Computation speed-upComputation speed-up– ModularityModularity– ConvenienceConvenience
Producer-Consumer Producer-Consumer ProblemProblem
Paradigm for cooperating Paradigm for cooperating processes, processes, producerproducer process process produces information that is produces information that is consumed by a consumed by a consumerconsumer process. process.– unbounded-bufferunbounded-buffer places no practical places no practical
limit on the size of the buffer.limit on the size of the buffer.– bounded-bufferbounded-buffer assumes that there is assumes that there is
a fixed buffer size.a fixed buffer size.
Bounded-Buffer – Shared-Bounded-Buffer – Shared-Memory SolutionMemory Solution
Shared dataShared datavarvar nn;;
typetype itemitem = … ; = … ;
varvar bufferbuffer. . arrayarray [0.. [0..nn–1] –1] ofof itemitem;;
in, out:in, out: 0.. 0..nn–1;–1;
Producer processProducer process repeatrepeat
produce an item in produce an item in nextpnextp
whilewhile ( (inin+1) +1) modmod n n = = outout dodo no-op;no-op;
buffer buffer [[inin] :=] :=nextpnextp;;
in := (inin := (in+1) +1) modmod nn;;
untiluntil falsefalse;;
Bounded-Buffer Bounded-Buffer (cont)(cont)
Consumer processConsumer process repeatrepeat
whilewhile inin = = outout dodo no-opno-op;;
nextcnextc := := bufferbuffer [ [outout];];
outout := ( := (outout+1) +1) modmod nn;;
……
consume the item in nextcconsume the item in nextc
……
untiluntil falsefalse;;
Solution is correct, but buffer can only Solution is correct, but buffer can only contain up to n–1 items.contain up to n–1 items.
ThreadsThreads A A threadthread (or (or lightweight process lightweight process) is a ) is a
basic unit of CPU utilization; it consists of:basic unit of CPU utilization; it consists of:– program counterprogram counter– register set register set – stack spacestack space
A thread shares with its peer threads its:A thread shares with its peer threads its:– code sectioncode section– data sectiondata section– operating-system resourcesoperating-system resources
collectively known as a collectively known as a tasktask..
Threads Threads (cont)(cont)
A traditional or A traditional or heavyweightheavyweight process is process is equal to a task with one threadequal to a task with one thread
In a multiple threaded task, while one In a multiple threaded task, while one server thread is blocked and waiting, a server thread is blocked and waiting, a second thread in the same task can run.second thread in the same task can run.– Cooperation of multiple threads in same job Cooperation of multiple threads in same job
confers higher throughput and improved confers higher throughput and improved performance.performance.
– Applications that require sharing a common Applications that require sharing a common buffer (i.e., producer-consumer) benefit from buffer (i.e., producer-consumer) benefit from thread utilization.thread utilization.
Threads Threads (cont)(cont)
Threads make blocking system calls while Threads make blocking system calls while also achieving parallelism.also achieving parallelism.
Kernel-supported threads (Mach and OS/2).Kernel-supported threads (Mach and OS/2). User-level threads; supported above the User-level threads; supported above the
kernel, via a set of library calls at the user kernel, via a set of library calls at the user level (Project Andrew from CMU).level (Project Andrew from CMU).
Hybrid approach implements both user-level Hybrid approach implements both user-level and kernel-supported threads (Solaris 2).and kernel-supported threads (Solaris 2).
Multiple Threads within a Multiple Threads within a TaskTask
Threads Support in Solaris Threads Support in Solaris 22
Solaris 2 is a version of UNIX Solaris 2 is a version of UNIX with support for threads at the with support for threads at the kernel and user levels, kernel and user levels, symmetric multiprocessing, and symmetric multiprocessing, and
real-time scheduling.real-time scheduling. LWP – intermediate level LWP – intermediate level
between user-level threads and between user-level threads and kernel-level threads.kernel-level threads.
Threads in Solaris 2Threads in Solaris 2 (cont)(cont)
Resource needs of thread types:Resource needs of thread types:– Kernel thread: small data structure and a stack; Kernel thread: small data structure and a stack;
thread switching does not require changing thread switching does not require changing memory access information – relatively fast.memory access information – relatively fast.
– LWP: PCB with register data, accounting and LWP: PCB with register data, accounting and memory information; switching between LWPs is memory information; switching between LWPs is relatively slow.relatively slow.
– User-level thread: only need stack and program User-level thread: only need stack and program counter; no kernel involvement means fast counter; no kernel involvement means fast switching. Kernel only sees the LWPs that switching. Kernel only sees the LWPs that support user-level threads.support user-level threads.
Threads in Solaris 2 Threads in Solaris 2 (cont)(cont)
Interprocess Interprocess Communication (IPC)Communication (IPC)
Mechanism for processes to Mechanism for processes to communicate and to synchronize their communicate and to synchronize their actions.actions.
Message system – processes Message system – processes communicate with each other without communicate with each other without resorting to shared variables.resorting to shared variables.
IPC facility provides two operations:IPC facility provides two operations:– sendsend((messagemessage) – message size fixed or ) – message size fixed or
variable variable – receivereceive((messagemessage))
Interprocess Interprocess Communication Communication (cont)(cont)
If If PP and and QQ wish to communicate, they wish to communicate, they need to:need to:– establish a establish a communicationcommunication linklink between between
themthem– exchange messages via send/receiveexchange messages via send/receive
Implementation of communication link:Implementation of communication link:– physical (e.g., shared memory, hardware physical (e.g., shared memory, hardware
bus)bus)– logical (e.g., logical properties)logical (e.g., logical properties)
Implementation QuestionsImplementation Questions
How are links established?How are links established? Can a link be associated with more than Can a link be associated with more than
two processes?two processes? How many links can there be between How many links can there be between
every pair of communicating processes?every pair of communicating processes? What is the capacity of a link?What is the capacity of a link? Is the size of a message that the link can Is the size of a message that the link can
accommodate fixed or variable?accommodate fixed or variable? Is a link unidirectional or bi-directional?Is a link unidirectional or bi-directional?
Direct CommunicationDirect Communication
Processes must name each other explicitly:Processes must name each other explicitly:– sendsend ( (P, messageP, message) – send a message to P) – send a message to P– receivereceive((Q, messageQ, message) – receive a message from Q) – receive a message from Q
Properties of communication linkProperties of communication link– Links are established automatically.Links are established automatically.– A link is associated with exactly one pair of A link is associated with exactly one pair of
communicating processes.communicating processes.– Between each pair there exists exactly one link.Between each pair there exists exactly one link.– The link may be unidirectional, but is usually bi-The link may be unidirectional, but is usually bi-
directional.directional.
Indirect CommunicationIndirect Communication Messages are directed and received from Messages are directed and received from
mailboxes (also referred to as ports).mailboxes (also referred to as ports).– Each mailbox has a unique id.Each mailbox has a unique id.– Processes can communicate only if they share a Processes can communicate only if they share a
mailbox.mailbox.
Properties of communication linkProperties of communication link– Link established only if processes share a common Link established only if processes share a common
mailboxmailbox– A link may be associated with many processes.A link may be associated with many processes.– Each pair of processes may share several Each pair of processes may share several
communication links.communication links.– Link may be unidirectional or bi-directional.Link may be unidirectional or bi-directional.
Indirect Communication Indirect Communication (cont)(cont)
Operations:Operations:– create a new mailboxcreate a new mailbox– send and receive messages through send and receive messages through
mailboxmailbox– destroy a mailboxdestroy a mailbox
Indirect Communication Indirect Communication (cont)(cont)
Mailbox sharingMailbox sharing– PP11, P, P22,, and and P P33 share mailbox A. share mailbox A.– PP11, sends; , sends; PP22 andand P P33 receive. receive.– Who gets the message?Who gets the message?
SolutionsSolutions– Allow link to be associated with <= 2 processes.Allow link to be associated with <= 2 processes.– Allow only 1 proc at a time to execute receive op.Allow only 1 proc at a time to execute receive op.– Allow the system to select arbitrarily the receiver. Allow the system to select arbitrarily the receiver.
Sender is notified who the receiver was. Sender is notified who the receiver was.
BufferingBuffering
Queue of messages attached to the Queue of messages attached to the link; implemented in one of three ways.link; implemented in one of three ways.1.1. Zero capacity – 0 messagesZero capacity – 0 messages
Sender must wait for receiver Sender must wait for receiver (rendezvous).(rendezvous).
2.2. Bounded capacity – finite length of Bounded capacity – finite length of nn messagesmessagesSender must wait if link full.Sender must wait if link full.
3.3. Unbounded capacity – infinite length Unbounded capacity – infinite length Sender never waits.Sender never waits.
Exception Conditions – Exception Conditions – Error RecoveryError Recovery
Process terminatesProcess terminates Lost messagesLost messages Scrambled MessagesScrambled Messages