Lecture 02 - Chapter 3 - Process.pdf
Document Details
Uploaded by PatientParadise6906
Imam Abdulrahman Bin Faisal University
Full Transcript
Chapter 3: Processes Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Objectives ◼ To introduce the notion of a process -- a program in execution, which forms the...
Chapter 3: Processes Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Objectives ◼ To introduce the notion of a process -- a program in execution, which forms the basis of all computation ◼ To describe the various features of processes, including scheduling, creation and termination, and communication ◼ To describe communication in client-server systems Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013 Key Terms Batch Processing: In batch processing, same types of jobs batch together and execute at a time. Grouping of several processing jobs to be executed one after another by a computer without any user interaction. The OS was simple, its major task was to transfer control from one job to the next. ▪ Multiprogramming ▪ Multiprogramming is a technique to execute number of programs simultaneously by a single processor. Key Terms Time Sharing System: Simultaneous interactive use of a computer system by many users in such a way that each one feels that he/she is the sole user of the system. Multiple jobs are executed by switching the CPU between them. ▪ Multiprocessing ▪ Multiprocessing is the use of two or more central processing units (CPU) within a single computer system. ▪ An operating system capable of supporting and utilizing more than one computer processor. Key terms Multiprogramming: The concurrent residency of more than one program in the main memory is called as multiprogramming. Multitasking: The execution of more than one task simultaneously is called as multitasking. ◼ Even on a single-user system, a user may be able to run several programs at one time: ◼ a word processor, a web browser, and an e-mail package. ◼ How a single-user system can run multiple process? Introduction ◼ Process Manager Introduction: https://www.youtube.com/watch?v=bS3QuOQgUu8&list=PLmbPuZ0NsyGS8ef6zaHd2qYylzsHxL63x&i ndex=3 https://www.youtube.com/watch?v=7FRW4iGjLrc&list=PLmbPuZ0NsyGS8ef6zaHd2qYylzsHxL63x&ind ex=4 ◼ Early computer systems: ⚫ Allowed only one program to be executed at a time This program had a complete control over the system and its resources ◼ Today: ⚫ Multiple programs can be loaded into memory and executed concurrently Requires firmer control Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013 Process Concept ⚫ Textbook uses the terms job and process almost interchangeably ◼ An operating system executes a variety of programs: ⚫ Batch system – jobs ⚫ Time-shared systems – user programs or tasks ◼ Running processes are either: ⚫ Operating system processes executing system code ⚫ User processes executing user code ◼ Process – a program in execution; ⚫ Program (executable) – passive entity in the disk ⚫ Process – active entity ◼ Process execution must progress in sequential fashion Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013 Process in Memory Usually temporary data (such as function parameters, return addresses, and local variables) – stored for a short amount of time in memory ◼ A process block includes: ⚫ Stack, heap, Text and data section Dynamically allocated during process run-time Global variables Compiled code of the program Example: Memory Layout of a C Program Program and Process ◼ Two processes may be associated with the same program: (Open multiple windows of same web browser) ◼ Two separate execution sequences ◼ Text sections are equivalent, however data, heap and stack sections vary (change from each other) Program Difference in size and content Difference in size and content Difference content program counter program counter Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013 Process State ◼ As a process executes, it changes state ⚫ new: The process is being created ⚫ running: Instructions are being executed momentary states ⚫ waiting: The process is waiting for some event to occur (e.g., I/O completion) ⚫ ready: The process is waiting to be assigned to a processor ⚫ terminated: The process has finished execution Process status names vary across operating systems How many processes can we have in each status? Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013 Diagram of Process State time slice Operating System Concepts – 9th Edition 3.12 Silberschatz, Galvin and Gagne ©2013 Process Control Block (PCB) It is store in a Text Section. Contains various information associated with each process ◼ Process state ◼ Program counter – address of next instruction ◼ CPU registers ◼ CPU scheduling information –e.g., process priority, pointers to scheduling queues ◼ Memory-management information – value of base and limit registers, page or segment tables ◼ Accounting information – need CPU(real time, time used, time limits), process No etc. ◼ I/O status information – list of open files, I/O devices allocated to the process CPU Switch From Process to Process ▪ The objective of time-sharing is to switch the CPU among processes so frequently that users can interact with each program while it is running Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013 Process Scheduling Queues ◼ The goal of multiprogramming – to have some process running at all times to maximize CPU utilization ◼ To meet this goal we use a process scheduler ◼ Job queue – set of all processes in the system (this is where all processes go upon entering the system) ◼ Ready queue – set of all processes residing in main memory, ready and waiting to execute – usually a linked list (why?) ◼ Device queues – set of processes waiting for an I/O device ◼ Processes migrate among the various queues Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013 Ready Queue And Various I/O Device Queues Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013 Representation of Process Scheduling Queuing Diagram Job queue end Wait for the child to finish execution Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013 Schedulers ◼ Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue – loads processes from disk to memory ◼ Short-term scheduler (or CPU scheduler) – selects which process should be executed next (from the ready queue) and allocates CPU Short- term Long-term scheduler scheduler Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013 Schedulers (Cont) ◼ Short-term scheduler is invoked very frequently (milliseconds) (must be fast, otherwise create substantial overhead) – simple methods like FCFS, priority scheduling ◼ Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) ◼ The long-term scheduler controls the degree of multiprogramming (number of processes in memory) Operating System Concepts – 9th Edition 3.19 Silberschatz, Galvin and Gagne ©2013 Schedulers (Cont.) ◼ Processes can be described as either: ⚫ I/O-bound process – spends more time doing I/O than computations, many short CPU bursts (e.g., surfing the web, copying large files) ⚫ CPU-bound process – spends more time doing computations; few very long CPU bursts (e.g., calculating Pi, or some mathematical or statistical calculations) ◼ It is important for the long-term scheduler to select a good process mix of I/O- bound and CPU-bound processes What will happen to the ready queue and I/O queue: What if we have no long- term scheduler? ⚫ If all processes are I/O bound? ⚫ If all processes are CPU bound? 3.20 Context Switch ▪ When CPU switches to another process, the system must save the state of the old process and load the saved state (restore the state) for the new process via a context switch ▪ Context of a process represented in the PCB ▪ Context-switch time is overhead because the system does no useful work while switching. ▪ Switching speed varies from machine to machine depending upon the memory speed, the number of registers. ▪ Time dependent on hardware support Operating System Concepts – 9th Edition 3.21 Silberschatz, Galvin and Gagne ©2013 Context Switch Explain the context switching Operating System Concepts – 9th Edition 3.22 Silberschatz, Galvin and Gagne ©2013 Process Creation What is fork? Operating System Concepts – 9th Edition 3.23 Silberschatz, Galvin and Gagne ©2013 Process Creation ◼ Creating a process – using create process (fork) system call ◼ 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 (cpu time, memory, I/O devices) ⚫ Parent and children share all resources ⚫ Children share subset of parent’s resources ⚫ Parent and child share no resources One process can overload the system by creating many processes Initialization of data is passed from parent to child upon creation Operating System Concepts – 9th Edition 3.24 Silberschatz, Galvin and Gagne ©2013 Process Creation Root parent process for all user processes Network Memory File services mng. system mng. A tree of processes on a typical Solaris Operating System Concepts – 9th Edition 3.25 Silberschatz, Galvin and Gagne ©2013 Process Creation (Cont) ◼ Execution ⚫ Parent and children execute concurrently ⚫ Parent waits until children terminate ◼ Address space ⚫ Child address is duplicate of parent – same program and data ⚫ Child has a new program loaded into it (different from the parent’s program) ◼ UNIX examples ⚫ fork system call creates new process Parent’s address space is duplicated Both processes resume execution at the instruction after the fork() ⚫ exec system call used after a fork to replace the process’ memory space with a new program ⚫ Wait() – takes the process out o f the ready queue until the child process terminates. Operating System Concepts – 9th Edition 3.26 Process Creation Both processes (the parent and the child) continue execution at the instruction after the fork(), with one difference: the return code for the fork() is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned to the parent. The parent can then create more children; or, if it has nothing else to do while the child runs, it can issue a wait() system call to move itself off the ready queue until the termination of the child Operating System Concepts – 9th Edition 3.27 Silberschatz, Galvin and Gagne ©2013 Process Termination 1. When a process executes its last statement, it asks the operating system to delete it using (exit) system call ⚫ Output data from child to parent (via wait) ⚫ All process’ resources are de-allocated by operating system 2. Parent may terminate execution of children processes (abort) ⚫ Reason 1: Child has exceeded the usage of allocated resources ⚫ Reason 2: Task assigned to child is no longer required (the browser example) ⚫ Reason 3: If parent is exiting Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading termination Operating System Concepts – 9th Edition 3.30 Silberschatz, Galvin and Gagne ©2013 Process Termination ▪ 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); ▪ A process that has terminated, but whose parent has not yet called wait(), is known as a zombie process. All processes transition to this state when they terminate, but generally they exist as zombies only briefly. Once the parent calls wait(), the process identifier of the zombie process and its entry in the process table are released. ▪ If a parent terminated without invoking wait(),leaving its child as an orphan. This child process is called an orphan Traditional UNIX systems addressed this scenario by assigning the init (or systemd) process as the new parent to orphan processes (init serves as the root of the process hierarchy in UNIX systems.) The init process periodically invokes wait(), thereby allowing the exit status of any orphaned process to be collected and releasing the orphan’s process identifier and process-table entry. Operating System Concepts – 10th Edition 3.31 Silberschatz, Galvin and Gagne ©2018 Interprocess Communication ◼ Processes within a system may be independent or cooperating ⚫ Independent processes cannot affect or be affected by other running processes ⚫ Cooperating process can affect or be affected by other processes, including sharing data ◼ Reasons for cooperating processes: ⚫ Information sharing – e.g., shared file requires concurrent access ⚫ Computation speedup – parallel execution (works only with multi-processor system) ⚫ Modularity – dividing the system into several modules (processes or threads) that will need to work together ⚫ Convenience – user can work on several applications at the same time ◼ Cooperating processes need Inter-Process-Communication (IPC) ◼ Two models of IPC ⚫ Shared memory – usually resides in the address space of creating process ⚫ Message passing Operating System Concepts – 9th Edition 3.32 Communications Models Better for exchanging small messages (no Faster conflicts need to be avoided) Easier to implement Operating System Concepts – 9th Edition 3.33 Silberschatz, Galvin and Gagne ©2013 Communications Models (a) Message passing. (b) Shared memory. Faster since message-passing systems are Useful for exchanging typically implemented using system calls smaller amounts of and thus require the more time-consuming data (no conflicts need task of kernel intervention. to be avoided) In shared-memory systems, system calls are Easier to implement in required only to establish shared memory a distributed system than regions. shared memory Once shared memory is established, all accesses are treated as routine memory accesses, and no assistance from the kernel is required. Shared-Memory Systems ◼ Producer-Consumer Problem: o Paradigm for cooperating processes, where a producer process produces information that is consumed by a consumer process (e.g., compiler produces assembly code, consumed by the assembler) o Shared memory solution: unbounded-buffer places no practical limit on the size of the buffer: Producer never waits (always produce new items). Consumer waits if there is no buffer to consume bounded-buffer assumes that there is a fixed buffer size Producer must wait if all buffers are full Consumer waits if there is no buffer to consume Operating System Concepts – 9th Edition 3.35 IPC– Message Passing ◼ It is a mechanism for processes to communicate and to synchronize their actions without sharing the same address space. ◼ It is useful in a distributed environment, where the communicating processes may reside on different computers connected by a network. ◼ A message-passing facility provides two operations: ⚫ send(message) – message size can be fixed or variable ⚫ receive(message) ◼ If P and Q wish to communicate, they need to: ⚫ establish a communication link between them. This link can be implemented logically by several methods: Direct/Indirect communication Synchronous / Asynchronous communication Automatic / explicit buffering ⚫ exchange messages via send/receive ◼ Implementation of communication link ⚫ physical (e.g., shared memory, hardware bus) ⚫ logical (e.g., logical properties) Operating System Concepts – 9th Edition 3.36 Silberschatz, Galvin and Gagne ©2013 Direct Communication ◼ Processes must name each other explicitly during communication: ⚫ send (P, message) – send a message to process P ⚫ receive(Q, message) – receive a message from process Q ◼ Properties of this communication link o Links are established automatically o The link may be unidirectional, but is usually bi-directional o A link is associated with exactly one pair of communicating processes (2 processes only) o Between each pair there exists exactly one link Operating System Concepts – 9th Edition 3.37 Silberschatz, Galvin and Gagne ©2013 Direct Communication ▪ This scheme exhibits symmetry in addressing; that is, both the sender process and the receiver process must name the other to communicate. ▪ A variant of this scheme employs asymmetry in addressing. Here, only the sender names the recipient; the recipient is not required to name the sender. ▪ The disadvantage in both of these schemes (symmetric and asymmetric) is the limited modularity of the resulting process definitions. Operating System Concepts – 10th Edition 3.38 Silberschatz, Galvin and Gagne ©2018 Indirect Communication ◼ Messages are sent 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 ⚫ A Link is established only if processes have a shared mailbox ⚫ Link may be unidirectional or bi-directional ⚫ Each pair of processes may share several communication links ⚫ A link may be associated with many processes Operating System Concepts – 9th Edition 3.39 Silberschatz, Galvin and Gagne ©2013 Indirect Communication ◼ Operations ⚫ create a new mailbox ⚫ send and receive messages through mailbox ⚫ destroy a mailbox Operating System Concepts – 9th Edition 3.40 Silberschatz, Galvin and Gagne ©2013 Indirect Communication ◼ Mailbox sharing ⚫ P1, P2, and P3 share mailbox A ⚫ P1, sends; P2 and P3 receive Who gets the message? ◼ 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 Operating System Concepts – 9th Edition 3.41 Silberschatz, Galvin and Gagne ©2013 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 Operating System Concepts – 9th Edition 3.42 Buffering ◼ Whether the communication is direct or indirect, messages must reside in a temporary queue (buffer). This queue can be implemented in one of three ways: 1. Zero capacity – no messages can wait in queue. Sender must block until recipient receives the message. Sender must wait for receiver (rendezvous) 2. Bounded capacity – finite length of n messages. Sender must wait if the link (buffer/queue) is full 3. Unbounded capacity – infinite length. Sender never waits Operating System Concepts – 9th Edition 3.43 Silberschatz, Galvin and Gagne ©2013 End of Chapter 3 Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018