640 likes | 659 Views
This chapter explains the concept of processes in operating systems, including process scheduling, operations on processes, cooperating processes, interprocess communication, and communication in client-server systems. It also provides exercises for practice.
E N D
Operating System Concepts Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 4 Processes • A process • A program in execution • A system consists of a collection of processes • OS processes • System code • User processes • User code • All processes are executed concurrently • Switching the CPU between processes • Make the computer more productive Chapter 4 Processes
Chapter 4 Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems • Summary • Exercises Chapter 4 Processes
4.1 Process Concept • An operating system executes a variety of programs • Batch system – jobs • Time-shared systems – user programs or tasks • All these activities are similar • Called processes • The terms job and process are used almost interchangeably Chapter 4 Processes
A program in execution Consist of Text section Program code Data section Global variables An active entity A program is a passive entity Two processes may be associated with the same program Stack Temporary data Current activity Program counter Contents of registers 4.1.1 The Process Chapter 4 Processes
4.1.2 Process State • As a process executes, it changes state. • Each process may be in one of the following states • 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. • Only one process can be running on any processor at any instant. Chapter 4 Processes
Diagram of process state Chapter 4 Processes
4.1.3 Process Control Block • Each process is presented by a process control block (PCB) – also called a task control block. • Process state • Program counter • The address of the next instruction to be executed • CPU registers • Very in number and type, depending on the computer architecture • Accumulators, index registers, stack pointer, general-purpose registers.. • CPU scheduling information • A process priority, pointers to scheduling queues… Chapter 4 Processes
4.1.3 Process Control Block • Memory-management information • Base and limit registers • Page or segment tables • Accounting information • The amount of CPU, real time used, time limits… • I/O status information • A list of I/O devices allocated to this process • A list of open files • Repository for any information that may vary from process to process • Context switch • Save/load the state of the old/new process Chapter 4 Processes
Diagram showing CPUswitch from process to process Chapter 4 Processes
Process control block (PCB) Chapter 4 Processes
4.1.4 Threads • A process is a program that performs a single thread of execution. • Could not simultaneously type in characters and run the spell checker within the same process • Extend the process concept to allow a process to have multiple threads of execution. Chapter 4 Processes
Chapter 4 Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems • Summary • Exercises Chapter 4 Processes
4.2 Process Scheduling • Multiprogramming • Have some process running at all times • To maximize CPU utilization • Time-sharing • Switch CPU among processes so frequently • Users can interact with each program while it is running Chapter 4 Processes
4.2.1 Scheduling Queues • 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. • Each device has its own device queue. • Other queues Chapter 4 Processes
The ready queue andvarious I/O device queues Chapter 4 Processes
Queueing-diagram representation of process scheduling Chapter 4 Processes
4.2.2 Schedulers • A process migrates between various scheduling queues throughout its lifetime. • Carried out by the appropriate scheduler • Long-term scheduler (or job scheduler) • select which processes should be brought into the ready queue (load into memory for execution) • Short-term scheduler (or CPU scheduler) • select which process should be executed next and allocate CPU Chapter 4 Processes
4.2.2 Schedulers • Primary distinction between these two schedulers • The frequency of their execution • Short-term executes frequently • Must be fast. • Long-term executes much less frequently • Control the degree of multiprogramming – the number of processes in memory • Afford to take more time to select a process Chapter 4 Processes
4.2.2 Schedulers • Processes can be described as • I/O-bound process • spend more time doing I/O than computations • CPU-bound process • spend more time doing computations • Best performance • A combination of CPU-bound and I/O-bound processes • Long-term scheduler Chapter 4 Processes
4.2.2 Schedulers • Long-term scheduler may be absent or minimal. • Put every process in memory for the short-term scheduler • Stability depends on • Physical limitation • Self-adjusting nature of human users Chapter 4 Processes
4.2.2 Schedulers • Medium-term scheduler • Remove processes from memory (swap out) • Reduce the degree of multiprogramming • Reintroduce the process and continue its execution (swap in) • Such a scheme is called swapping • May be necessary to • Improve the process mix • A change in memory requirement has overcommitted available memory Chapter 4 Processes
4.2.3 Context Switch • Context Switch • When CPU switches to another process, the system must • save the state of the old process • load the saved state for the new process. • Context-switch time • pure overhead • the system does no useful work while switching. • Highly depend on hardware support • Multiple sets of registers Chapter 4 Processes
Chapter 4 Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems • Summary • Exercises Chapter 4 Processes
4.3 Operations on Processes • Processes can execute concurrently • Be created and deleted dynamically • OS must provide a mechanism (or facility) for process creation and termination. Chapter 4 Processes
4.3.1 Process Creation • A process may create new processes • Create-process system call • The creating process is called a parent process, whereas the new processes are called the children of that process. • May form a tree of processes Chapter 4 Processes
A tree of processes on a typical UNIX system Chapter 4 Processes
4.3.1 Process Creation • Resource sharing • Parent and children share all resources. • Children share subset of parent’s resources. • Parent and child share no resources. • Execution • Parent and children execute concurrently. • Parent waits until children terminate. • Address space • Child duplicate of parent. • Child has a program loaded into it. Chapter 4 Processes
4.3.1 Process Creation • UNIX examples • Each process is identified by its process identifier (PID) • A unique integer • fork system call creates new process • execlp system call used after a fork to replace the process’ memory space with a new program. Chapter 4 Processes
4.3.2 Process Termination • Process executes last statement and asks the operating system to delete it (use exit system call). • Output data from child to parent (via wait system call). • Process’ resources are deallocated by operating system. • Parent may terminate execution of children processes (abort system call). • Child has exceeded allocated resources. • Task assigned to child is no longer required. • Parent is exiting. • Operating system does not allow child to continue if its parent terminates. • Cascading termination Chapter 4 Processes
Chapter 4 Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems • Summary • Exercises Chapter 4 Processes
4.4 Cooperating Processes • Independent process • A process cannot affect or be affected by the execution of another process. • Cooperating process • A process can affect or be affected by the execution of another process • Advantages of process cooperation • Information sharing • Computation speed-up • Modularity • Convenience Chapter 4 Processes
Producer-Consumer Problem • Paradigm for cooperating processes • A producer process produces information that is consumed by a consumer process. • The producer and consumer must be synchronized. • Categories • unbounded-buffer • no practical limit on the size of the buffer • bounded-buffer • a fixed buffer size • The buffer can be provided by • Interprocess-communication (IPC) facility • Shared memory Chapter 4 Processes
Producer-Consumer Problem • Bounded-buffer • Shared-memory solution • Shared data #define BUFFER_SIZE 10 Typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Chapter 4 Processes
Producer-Consumer Problem • The share buffer is a circular array with two logical pointers • in : the next free position in the buffer • out : the first full position in the buffer • Empty buffer • in == out • Full buffer • ( (in+1) % BUFFER_SIZE) == out • At most BUFFER_SIZE – 1 items Chapter 4 Processes
Producer Process item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Chapter 4 Processes
Consumer Process item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } Chapter 4 Processes
Chapter 4 Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems • Summary • Exercises Chapter 4 Processes
4.5 Interprocess Communication • IPC provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space. • Particularly useful in a distributed environment • Best provided by a message-passing system Chapter 4 Processes
4.5.1 Message-Passing System • An IPC facility provides at least two operations • send(message) • receive(message) • Messages could be • Fixed size • Straightforward system-level implementation • More difficult programming • Variable size • More complex system-level implementation • Simpler programming Chapter 4 Processes
4.5.1 Message-Passing System • If 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 (e.g., shared memory, hardware bus) • logical (e.g., logical properties) Chapter 4 Processes
Methods for logical implementation • Direct or indirect communication • Symmetric or asymmetric communication • Automatic or explicit buffering • Send by copy or send by reference • Fixed-sized or variable-sized messages Chapter 4 Processes
4.5.2 Naming • Direct communication • Explicitly name the recipient or sender of the communication • send (P, message) – send a message to process P • receive(Q, message) – receive a message from process Q • Properties • A link is established automatically between every pair of processes that want to communicate. • Know each other’s identity • A link is associated with exactly two processes. • Exactly one link exists between each pair of processes. Chapter 4 Processes
4.5.2 Naming • Symmetry in addressing • Both the sender and the receiver must name the other to communicate • Asymmetry in addressing • Only the sender name the recipient • The recipient is not required to name the sender • send (P, message) – send a message to process P • receive(id, message) – receive a message from any process • id is set to the name of the process with which communication has taken place • Disadvantage in both schemes • Limited modularity Chapter 4 Processes
4.5.2 Naming • Indirect communication • Messages are sent to and received from mailboxes or ports. • Each mailbox has a unique identification • send and receive primitives • send (A, message) – send a message to mailbox A • receive(A, message) – receive a message from mailbox A • Properties • A link is established only if processes have a shared mailbox • A link may be associated with more than two processes. • Each pair of processes may share several communication links. Chapter 4 Processes
4.5.2 Naming • A mailbox is owned by • A process • the owner and the user • The operating system • Not attached to any particular process • Provide a mechanism that allows a process to • Create a new mailbox • Send and receive messages through the mailbox • Delete a mailbox Chapter 4 Processes
4.5.3 Synchronization • Message passing may be either blocking (synchronous) or nonblocking (asynchronous) • Blocking send • The sending process is blocked until the message is received. • Nonblocking send • The sending process sends the message and resumes operation. • Blocking receive • The receiver blocks until a message is available. • Nonblocking receive • The receive retrieve either a valid message or a null. • A rendezvous • Both the send and receive are blocking Chapter 4 Processes
4.5.4 Buffering • Messages exchanged by communicating processes reside in a temporary queue. • Zero capacity • Maximum length is 0 • The sender must block until the recipient receives the message. • Bounded capacity • A finite length n • The sender must block until the space is available in the queue. • Unbounded capacity • An infinite length • The sender never blocks. Chapter 4 Processes
Chapter 4 Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems • Summary • Exercises Chapter 4 Processes
4.6.1 Sockets • A socket • An endpoint for communication • Identified by an IP address concatenated with a port number • The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 • Client-server • The server waits for incoming client requests by listening to a specific port • Port 23: telnet • Port 21: ftp • Port 80: http • All ports below 1024 are considered well known. Chapter 4 Processes