Chapter 3 Processes Operating System Concepts 8 th

  • Slides: 49
Download presentation
Chapter 3: Processes Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne

Chapter 3: Processes Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n Interpocess Communication n Threads Operating System Concepts – 8 th Edition 3. 2 Silberschatz, Galvin and Gagne © 2009

Objectives n To introduce the notion of a process -- a program in execution,

Objectives n To introduce the notion of a process -- a program in execution, which forms the basis of all computation n To describe the various features of processes, including scheduling, creation and termination, and communication n To describe communication in client-server systems Operating System Concepts – 8 th Edition 3. 3 Silberschatz, Galvin and Gagne © 2009

Process Concept n An operating system executes a variety of programs: l l Batch

Process Concept n An operating system executes a variety of programs: l l Batch system – jobs Time-shared systems – user programs or tasks n Textbook uses the terms job and process almost interchangeably n Process – a program in execution; process execution must progress in sequential fashion n A process includes: l l l program counter stack data section Operating System Concepts – 8 th Edition 3. 4 Silberschatz, Galvin and Gagne © 2009

The Process n Multiple parts l The program code, also called text section l

The Process n Multiple parts l The program code, also called text section l Current activity including program counter, processor registers l Stack containing temporary data 4 n Function parameters, return addresses, local variables l Data section containing global variables l Heap containing memory dynamically allocated during run time Program is passive entity, process is active l Program becomes process when executable file loaded into memory n Execution of program started via GUI mouse clicks, command line entry of its name, etc n One program can be several processes l Consider multiple users executing the same program Operating System Concepts – 8 th Edition 3. 5 Silberschatz, Galvin and Gagne © 2009

Process State n As a process executes, it changes state l new: The process

Process State n As a process executes, it changes state l new: The process is being created l ready: The process is waiting to be assigned to a processor l running: Instructions are being executed l waiting: The process is waiting for some event to occur l terminated: The process has finished execution Operating System Concepts – 8 th Edition 3. 6 Silberschatz, Galvin and Gagne © 2009

Process Control Block (PCB) Information associated with each process n Process state n Program

Process Control Block (PCB) Information associated with each process n Process state n Program counter n CPU registers n CPU scheduling information n Memory-management information n Accounting information n I/O status information Operating System Concepts – 8 th Edition 3. 7 Silberschatz, Galvin and Gagne © 2009

Process Control Block (PCB) • • Process state. The state may be new, ready

Process Control Block (PCB) • • Process state. The state may be new, ready running, waiting, halted, and so on. Program counter. The counter indicates the address of the next instruction to be executed for this process. • CPU registers. The registers vary in number and type, depending on the computer architecture. They include accumulators, index registers, stack pointers, and general-purpose registers, plus any condition-code information. Along with the program counter, this state information must be saved when an interrupt occurs, to allow the process to be continued correctly afterward • CPU-scheduling information. This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. • Memory-management information. This information may include such information as the value of the base and limit registers, the page tables, or the segment tables, depending on the memory system used by the operating system • Accounting information. This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on. • I/O status information. This information includes the list of I/O devices allocated to the process, a list of open files, and so on. Operating System Concepts – 8 th Edition 3. 8 Silberschatz, Galvin and Gagne © 2009

CPU Switch From Process to Process Operating System Concepts – 8 th Edition 3.

CPU Switch From Process to Process Operating System Concepts – 8 th Edition 3. 9 Silberschatz, Galvin and Gagne © 2009

Context Switch n When CPU switches to another process, the system must save the

Context Switch n 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. n Context of a process represented in the PCB n Context-switch time is overhead; the system does no useful work while switching l The more complex the OS and the PCB -> longer the context switch n Time dependent on hardware support l Some hardware provides multiple sets of registers per CPU -> multiple contexts loaded at once Operating System Concepts – 8 th Edition 3. 10 Silberschatz, Galvin and Gagne © 2009

Process Scheduling n Maximize CPU use – Multiprogramming n Quickly switch processes onto CPU

Process Scheduling n Maximize CPU use – Multiprogramming n Quickly switch processes onto CPU -- Time sharing n Process scheduler selects among available processes for next execution on CPU(from set of available processes) n Maintains scheduling queues of processes l Job queue – set of all processes in the system l Ready queue – set of all processes residing in main memory, ready and waiting to execute l Device queues – set of processes waiting for an I/O device l Processes migrate among the various queues Operating System Concepts – 8 th Edition 3. 11 Silberschatz, Galvin and Gagne © 2009

Ready Queue And Various I/O Device Queues Operating System Concepts – 8 th Edition

Ready Queue And Various I/O Device Queues Operating System Concepts – 8 th Edition 3. 12 Silberschatz, Galvin and Gagne © 2009

Representation of Process Scheduling Operating System Concepts – 8 th Edition 3. 13 Silberschatz,

Representation of Process Scheduling Operating System Concepts – 8 th Edition 3. 13 Silberschatz, Galvin and Gagne © 2009

Schedulers n Process migrates n Long-term scheduler (or job scheduler) – selects which processes

Schedulers n Process migrates n Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue n Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU l Sometimes the only scheduler in a system n Frequency of execution n Short term scheduler executes every 100 ms n Long term scheduler controls degree of multiprogramming n Time sharing systems may introduce medium term scheduler n To reduce the degree of multiprogramming n Process will be swapped out for some time and reintroduced after some time and continues execution Operating System Concepts – 8 th Edition 3. 14 Silberschatz, Galvin and Gagne © 2009

Schedulers (Cont. ) n Short-term scheduler is invoked very frequently (milliseconds) (must be fast)

Schedulers (Cont. ) n Short-term scheduler is invoked very frequently (milliseconds) (must be fast) n Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) n The long-term scheduler controls the degree of multiprogramming n Processes can be described as either: l I/O-bound process – spends more time doing I/O than computations, many short CPU bursts l CPU-bound process – spends more time doing computations; few very long CPU bursts Operating System Concepts – 8 th Edition 3. 15 Silberschatz, Galvin and Gagne © 2009

Addition of Medium Term Scheduling Operating System Concepts – 8 th Edition 3. 16

Addition of Medium Term Scheduling Operating System Concepts – 8 th Edition 3. 16 Silberschatz, Galvin and Gagne © 2009

Process Creation n Parent process create children processes, which, in turn create other processes,

Process Creation n Parent process create children processes, which, in turn create other processes, forming a tree of processes n Generally, process identified and managed via a process identifier (pid) n Resource sharing l Parent and children share all resources l Children share subset of parent’s resources l Parent and child share no resources n Execution l Parent and children execute concurrently l Parent waits until children terminate Operating System Concepts – 8 th Edition 3. 17 Silberschatz, Galvin and Gagne © 2009

Process Creation (Cont. ) n Address space l Child duplicate of parent l Child

Process Creation (Cont. ) n Address space l Child duplicate of parent l Child has a program loaded into it n UNIX examples l fork system call creates new process l exec system call used after a fork to replace the process’ memory space with a new program Operating System Concepts – 8 th Edition 3. 18 Silberschatz, Galvin and Gagne © 2009

Process Creation Operating System Concepts – 8 th Edition 3. 19 Silberschatz, Galvin and

Process Creation Operating System Concepts – 8 th Edition 3. 19 Silberschatz, Galvin and Gagne © 2009

A Tree of Processes on Solaris Operating System Concepts – 8 th Edition 3.

A Tree of Processes on Solaris Operating System Concepts – 8 th Edition 3. 20 Silberschatz, Galvin and Gagne © 2009

Process Termination n Process executes last statement and asks the operating system to delete

Process Termination n Process executes last statement and asks the operating system to delete it (exit) l Output data from child to parent (via wait) l Process’ resources are deallocated by operating system n Parent may terminate execution of children processes (abort) l Child has exceeded allocated resources l Task assigned to child is no longer required l If parent is exiting 4 Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading termination Operating System Concepts – 8 th Edition 3. 21 Silberschatz, Galvin and Gagne © 2009

Interprocess Communication n Processes within a system may be independent or cooperating n Cooperating

Interprocess Communication n Processes within a system may be independent or cooperating n Cooperating process can affect or be affected by other processes, including sharing data n Reasons for cooperating processes: l Information sharing l Computation speedup l Modularity l Convenience n Cooperating processes need interprocess communication (IPC) n Two models of IPC l Shared memory l Message passing Operating System Concepts – 8 th Edition 3. 22 Silberschatz, Galvin and Gagne © 2009

Communications Models a. Message passing b. Shared Memory Operating System Concepts – 8 th

Communications Models a. Message passing b. Shared Memory Operating System Concepts – 8 th Edition 3. 23 Silberschatz, Galvin and Gagne © 2009

Interprocess Communication – Message Passing n Mechanism for processes to communicate and to synchronize

Interprocess Communication – Message Passing n Mechanism for processes to communicate and to synchronize their actions n n without sharing address space Message system – processes communicate with each other without resorting to shared variables IPC facility provides two operations: n send(message) – message size fixed or variable n receive(message) If P and Q wish to communicate, they need to: n establish a communication link between them n exchange messages via send/receive Implementation of communication link n physical (e. g. , shared memory, hardware bus) n logical (e. g. , logical properties) Operating System Concepts – 8 th Edition 3. 24 Silberschatz, Galvin and Gagne © 2009

Direct Communication n Processes must name each other explicitly: l send (P, message) –

Direct Communication n Processes must name each other explicitly: l send (P, message) – send a message to process P l receive(Q, message) – receive a message from process Q n Properties of communication link l Links are established automatically l A link is associated with exactly one pair of communicating processes l Between each pair there exists exactly one link l The link may be unidirectional, but is usually bi-directional Operating System Concepts – 8 th Edition 3. 25 Silberschatz, Galvin and Gagne © 2009

Indirect Communication n Messages are directed and received from mailboxes (also referred to as

Indirect Communication n Messages are directed and received from mailboxes (also referred to as ports) l Each mailbox has a unique id l Processes can communicate only if they share a mailbox n Properties of communication link l Link established only if processes share a common mailbox l A link may be associated with many processes l Each pair of processes may share several communication links l Link may be unidirectional or bi-directional Operating System Concepts – 8 th Edition 3. 26 Silberschatz, Galvin and Gagne © 2009

Indirect Communication n n Operations l create a new mailbox l send and receive

Indirect Communication n n Operations l create a new mailbox l send and receive messages through mailbox l 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 Operating System Concepts – 8 th Edition 3. 27 Silberschatz, Galvin and Gagne © 2009

Indirect Communication n n Mailbox sharing l P 1, P 2, and P 3

Indirect Communication n n Mailbox sharing l P 1, P 2, and P 3 share mailbox A l P 1, sends; P 2 and P 3 receive l Who gets the message? Solutions l Allow a link to be associated with at most two processes l Allow only one process at a time to execute a receive operation l Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. Operating System Concepts – 8 th Edition 3. 28 Silberschatz, Galvin and Gagne © 2009

Synchronization n Message passing may be either blocking or non-blocking n Blocking is considered

Synchronization n Message passing may be either blocking or non-blocking n Blocking is considered synchronous n l Blocking send has the sender block until the message is received l Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous l Non-blocking send has the sender send the message and continue l Non-blocking receive has the receiver receive a valid message or null Operating System Concepts – 8 th Edition 3. 29 Silberschatz, Galvin and Gagne © 2009

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

Buffering n 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 Operating System Concepts – 8 th Edition 3. 30 Silberschatz, Galvin and Gagne © 2009

Background to critical section n Concurrent access to shared data may result in data

Background to critical section n Concurrent access to shared data may result in data inconsistency n Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes n Critical Section is a code segment that accesses shared variables and has to be executed as an atomic action. It means that in a group of cooperating processes, at a given point of time, only one process must be executing its critical section. Operating System Concepts – 8 th Edition 3. 31 Silberschatz, Galvin and Gagne © 2009

Critical section Operating System Concepts – 8 th Edition 3. 32 Silberschatz, Galvin and

Critical section Operating System Concepts – 8 th Edition 3. 32 Silberschatz, Galvin and Gagne © 2009

Solution to Critical-Section Problem Requirements: 1. Mutual Exclusion - If process Pi is executing

Solution to Critical-Section Problem Requirements: 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes Operating System Concepts – 8 th Edition 3. 33 Silberschatz, Galvin and Gagne © 2009

Semaphore semaphore, in its most basic form, is a protected integer variable that can

Semaphore semaphore, in its most basic form, is a protected integer variable that can facilitate and restrict access to shared resources in a multi-processing environment. The two most common kinds of semaphores are counting semaphores and binary semaphores. Counting semaphores represent multiple resources, while binary semaphores, as the name implies, represents two possible states (generally 0 or 1; locked or unlocked). Semaphores are commonly use for two purposes: to share a common memory space and to share access to files. Semaphores are one of the techniques for interprocess communication (IPC). Operating System Concepts – 8 th Edition 3. 34 Silberschatz, Galvin and Gagne © 2009

Single and Multithreaded Processes Operating System Concepts – 8 th Edition 3. 35 Silberschatz,

Single and Multithreaded Processes Operating System Concepts – 8 th Edition 3. 35 Silberschatz, Galvin and Gagne © 2009

Benefits n Responsiveness n Resource Sharing n Economy n Scalability Operating System Concepts –

Benefits n Responsiveness n Resource Sharing n Economy n Scalability Operating System Concepts – 8 th Edition 3. 36 Silberschatz, Galvin and Gagne © 2009

Multicore Programming n Multicore systems putting pressure on programmers, challenges include: l Dividing activities

Multicore Programming n Multicore systems putting pressure on programmers, challenges include: l Dividing activities l Balance l Data splitting l Data dependency l Testing Operating System Concepts – 8 th Edition and debugging 3. 37 Silberschatz, Galvin and Gagne © 2009

Multithreaded Server Architecture Operating System Concepts – 8 th Edition 3. 38 Silberschatz, Galvin

Multithreaded Server Architecture Operating System Concepts – 8 th Edition 3. 38 Silberschatz, Galvin and Gagne © 2009

Kernel Threads n Supported by the Kernel n Examples l Windows XP/2000 l Solaris

Kernel Threads n Supported by the Kernel n Examples l Windows XP/2000 l Solaris l Linux l Tru 64 UNIX l Mac OS X Operating System Concepts – 8 th Edition S. N. User-Level Threads Kernel-Level Thread 1 User-level threads are faster to Kernel-level threads are slower create and manage. to create and manage. 2 Implementation is by a thread library at the user level. 3 User-level thread is generic and Kernel-level thread is specific to can run on any operating the operating system. 4 Multi-threaded applications cannot take advantage of multiprocessing. 3. 39 Operating system supports creation of Kernel threads. Kernel routines themselves can be multithreaded. Silberschatz, Galvin and Gagne © 2009

Multithreading Models n Many-to-One S. N. Process Thread n One-to-One 1 Process is heavy

Multithreading Models n Many-to-One S. N. Process Thread n One-to-One 1 Process is heavy weight or resource intensive. Thread is light weight, taking lesser resources than a process. n Many-to-Many 2 Process switching needs Thread switching does not need to interaction with operating system. interact with operating system. 3 In multiple processing All threads can share same set of environments, each process open files, child processes. executes the same code but has its own memory and file resources. If one process is blocked, then While one thread is blocked and no other process can execute waiting, a second thread in the until the first process is same task can run. unblocked. Multiple processes without using Multiple threaded processes use threads use more resources. fewer resources. 4 5 6 Operating System Concepts – 8 th Edition In multiple processes each One thread can read, write or process operates independently change another thread's data. of the others. 3. 40 Silberschatz, Galvin and Gagne © 2009

Many-to-One n Many user-level threads mapped to single kernel thread n Examples: l Solaris

Many-to-One n Many user-level threads mapped to single kernel thread n Examples: l Solaris Green Threads l GNU Portable Threads Operating System Concepts – 8 th Edition 3. 41 Silberschatz, Galvin and Gagne © 2009

Many-to-One Model Operating System Concepts – 8 th Edition 3. 42 Silberschatz, Galvin and

Many-to-One Model Operating System Concepts – 8 th Edition 3. 42 Silberschatz, Galvin and Gagne © 2009

One-to-One n Each user-level thread maps to kernel thread n Examples l Windows NT/XP/2000

One-to-One n Each user-level thread maps to kernel thread n Examples l Windows NT/XP/2000 l Linux l Solaris 9 and later Operating System Concepts – 8 th Edition 3. 43 Silberschatz, Galvin and Gagne © 2009

One-to-one Model Operating System Concepts – 8 th Edition 3. 44 Silberschatz, Galvin and

One-to-one Model Operating System Concepts – 8 th Edition 3. 44 Silberschatz, Galvin and Gagne © 2009

Many-to-Many Model n Allows many user level threads to be mapped to many kernel

Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads n Allows the operating system to create a sufficient number of kernel threads n Solaris prior to version 9 n Windows NT/2000 with the Thread. Fiber package Operating System Concepts – 8 th Edition 3. 45 Silberschatz, Galvin and Gagne © 2009

Many-to-Many Model Operating System Concepts – 8 th Edition 3. 46 Silberschatz, Galvin and

Many-to-Many Model Operating System Concepts – 8 th Edition 3. 46 Silberschatz, Galvin and Gagne © 2009

Two-level Model n Similar to M: M, except that it allows a user thread

Two-level Model n Similar to M: M, except that it allows a user thread to be bound to kernel thread n Examples l IRIX l HP-UX l Tru 64 UNIX l Solaris 8 and earlier Operating System Concepts – 8 th Edition 3. 47 Silberschatz, Galvin and Gagne © 2009

Two-level Model Operating System Concepts – 8 th Edition 3. 48 Silberschatz, Galvin and

Two-level Model Operating System Concepts – 8 th Edition 3. 48 Silberschatz, Galvin and Gagne © 2009

End of Chapter 3 Operating System Concepts – 8 th Edition Silberschatz, Galvin and

End of Chapter 3 Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009