Chapter 3 Process Concepts Overview Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server Systems
OS executes a variety of programs 3.1 Overview OS executes a variety of programs Batch system –– jobs Time-shared systems –– user programs or tasks We use the terms job and process almost interchangeably Process – a program in execution Process execution must progress sequentially A process includes Program counter and the processor’s registers Stack (temporary data, method parameters, return address, local variables) Data section (contains global variables) Heap — memory dynamically allocated during process run time
3.1 Overview Processes associated with the same program are considered separate execution sequences Several users may be running the copies of the mail program The same user may invoke many copies of the editor program Each of these is a separate process, and, although the text sections (program codes) are equivalent, the data section will vary A process may spawn (繁衍) many processes as it runs 例:網頁瀏覽器中「新增」另一新視窗
3.1 Overview: Process State As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting (blocked): The process is waiting for some event to occur (e.g. I/O completion or reception of a signal) ready: waiting to be assigned to a processor terminated: The process has finished execution
3.1 Overview: Process Control Block … 或稱為Task Control Block Each process is represented by a process control block (PCB) Process state: N, R, T, W, Run Program counter CPU registers CPU scheduling information Process priority, pointers to scheduling queues, and any other scheduling parameters Memory-management information Accounting information I/O status information …
Figure 3.4: CPU Switch from Process to Process
A single-processor system can have only one running process 3.2 Process Scheduling Multiprogramming has some process running at all times, to maximize CPU utilization The objective of time sharing is to switch the CPU among the processes so frequently that users can interact with each program while it is running A single-processor system can have only one running process The rest waits until CPU is free and is rescheduled Process scheduling (排程) is required
Process Representation in Linux Represented by the C structure task_struct pid_t pid; /* process identifier */ long state; /* state of the process */ struct sched_entity se; /* scheduling information */ struct task_struct *parent; /* this process’s parent */ struct list_head children; /* this process’s children */ struct files_struct *files; /* list of open files */ struct mm_struct *mm; /* address space of this process */
3.2 Process Scheduling: Scheduling Queues Job queue Consists of all processes in the system Ready queue Keeps processes that are residing in main memory and are ready and waiting to execute Device queue List of processes waiting for a particular I/O device Each device has its own device queue
Ready Queue and Various I/O Device Queues
Queueing Diagram of Process Scheduling Resources Queues Parent may wait for its termination Processes migrate between various queues
3.2 Process Scheduling: Schedulers A process migrates between the various scheduling queues throughout its lifetime Process selection is carried out by the appropriate scheduler Long-term, short-term, mid-term Long-term scheduler (job scheduler) Selects processes from process pool and loads them into memory (ready queue) Controls the degree of multiprogramming, i.e., number of processes in memory Should select a good process mix of I/O-bound and CPU-bound processes
3.2 Process Scheduling: Schedulers Short-term scheduler (CPU scheduler) Selects one of the ready processes and allocates the CPU to it Medium-term scheduler Removes processes from memory and reduces the degree of multiprogramming
3.2 Process Scheduling: Schedulers Short-term and long-term schedulers differ mainly in the frequency of their execution Short-term scheduler is invoked very frequently (milliseconds) must be fast Long-term scheduler is invoked infrequently (seconds, minutes) may be slow 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
3.2 Process Scheduling: Context Switch 例:圖書館書桌由大家共用,每個人有獨特的文具擺設需求,使用書桌前令其恢復習慣的樣貌 Switching CPU to another process requires saving the state of the old process and loading the saved state for the new process This task is known as a context switch Context-switch time is pure overhead The system does no useful work while switching 1~1000μs Dependent on hardware support Some processors provide multiple sets of registers; a context switch simply includes changing the pointer to the current register set Memory management, address space of current processes
3.3 Operations on Processes Processes can execute concurrently, and must be created/deleted dynamically Process creation Parent process create children processes, which in turn create other processes a tree of processes
3.3 Operations on Processes: 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
3.3 Operations on Processes: Process Creation UNIX examples fork creates a new process exec system call after fork replaces the process’ memory space with a new program execlp loads a binary file into memory
fork()猶如火影忍者鳴人常用的招術
Process Creation*: UNIX Process Address Space Three regions of memory A read-only text region (executable code) A read-write region consisting of initialized data, uninitialized data, dynamic area This region is often collectively called the data region A 2nd read-write region containing the process’ user stack This region grows implicitly Per-process limits on the maximum data and stack sizes of processes text data Uninitialized data dynamic stack
Process Creation*: Before and After fork() child process (pid = p) fork( ) //return 0 By executing fork the parent process creates a clone of itself One thread of control flows into fork() but two threads of control flow out, each in a separate address space From the parent’s point of view, nothing happens except that fork() returns the process ID of the new process New process starts off life by returning from fork. It always views fork as returning a zero 複製完整的 address space fork( ) //return p parent process
Process Creation*: exec() Loads a New Image Child process (after) prog’s text prog’s data args for prog The purpose of creating a new process is mostly to run a new, different program Once a new process has been created, the exec system call can be used to load a new program image into itself, replacing the prior contents of the process’s address space exec(prog, args) Child process (before)
A Stripped-Down Shell* (以UNIX為例) while (TRUE) { /* repeat forever */ type_prompt( ); /* display prompt */ read_command (command, parameters); /* input from terminal */ if (fork() != 0) { /* fork off child process */ /* Parent code */ waitpid(-1, &status, 0); /* wait for child to exit */ } else { /* Child code */ execve (command, parameters, 0); /* execute command */
Creating a Separate Process via Windows API
4.3 Operations on Processes: Process Termination Process executes its final statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process’ resources deallocated by the OS Parent may terminate execution of children processes (abort) Child has exceeded allocated resources Task assigned to the child is no longer required Parent is exiting Many systems do not allow a child to continue if its parent terminates cascading termination
3.4 Interprocess Communication Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process Example: producer-customer problem Environment for process cooperation Information sharing Computation speed-up Modularity Convenience Two models of interprocess communication Shared memory, message passing
Message Passing, Shared Memory
Producer-Consumer Problem Common paradigm for cooperating processes Producer process produces information that is consumed by a consumer process Producer and customer must be synchronized, so the customer does not try to consume an item that has not been produced Unbounded-buffer producer-consumer problem Places no practical limit on the size of the buffer Consumer may have to wait for new items; producer can always produce new items Bounded-buffer producer-consumer problem Assumes a fixed buffer size Customer must wait if the buffer is empty; producer must wait if the buffer is full
Shared-Memory Solution to the Bounded Buffer Problem #define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Shared variables In Out
Shared-Memory Solution to the Bounded Buffer Problem (Only Use BUFFER_SIZE-1 Elements) #define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Shared variables In Out item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } Animation
3.4 Interprocess Communication (IPC) Message passing enables processes to communicate and to synchronize their actions Processes communicate with each other without resorting to shared variables IPC facility provides at least two operations send(message) – message size fixed or variable receive(message) P and Q wishing to communicate need to Establish a communication link between them Exchange messages via send/receive
3.4 Interprocess Communication Implementation of communication link Physical (e.g., shared memory, hardware bus) Logical (e.g., logical properties) Implementation questions How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes? Link capacity? Size of a message on a link is fixed or variable? Unidirectional or bi-directional link?
3.4 Interprocess Communication Logical implementation of a link 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
3.4 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 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 The link may be unidirectional, but is usually bi-directional
Example: The Producer-Consumer Problem producer process consumer process While (TRUE) { ... produce an item in nextp send(consumer,nextp); } While (TRUE) { receive(producer,nextc); ... consume the item in nextc }
3.4 Interprocess Communication: Indirect Communication Messages are sent to and received from mailboxes (ports) Each mailbox has a unique id. 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 Link may be unidirectional or bi-directional
3.4 Interprocess Communication: Indirect Communication (續) Operations Create a new mailbox Send and receive messages through mailbox Destroy a mailbox Mailbox sharing P1, P2, and P3 share mailbox A P1, sends; P2 and P3 receive Who gets the message? Allow a link to be associated with at most two processes At most one process at a time executes a receive operation The system selects arbitrarily one receiver. Sender is notified who the receiver was
Indirect Communication: Who Owns the Mailbox? Owned by a process Distinguish between the owner and the user Owner: receive messages User: send messages Owned by OS OS provides a mechanism allowing a process to Create a new mailbox Send and receive messages through the mailbox Destroy a mailbox Garbage collection for deleted mailboxes
3.4 Interprocess Communication: Synchronization Message passing may be either blocking or non-blocking Blocking is considered synchronous Non-blocking is considered asynchronous send and receive primitives may be either blocking or non-blocking Blocking send Sending process blocks until message is received Nonblocking send Blocking receive Receiver blocks until message is available Nonblocking receive Receiver retrieves either a valid message or a null
3.4 Interprocess Communication: Buffering A link has some capacity determining the number of messages that can reside (be queued) in it temporarily Implementation Zero capacity (queue length = 0) The sender must wait until the recipient receives the message This synchronization is called a rendezvous Bounded capacity (queue length = n) Wait when the queue is full Unbounded capacity Sender is never delayed
3.5 Interprocess Communication: Mach Example §3.5自行研讀; 不列入考試範圍 3.5 Interprocess Communication: Mach Example Mach (Message-based OS) Developed at Carnegie Mellon University Provide multiple threads of control Most communication is carried out by messages Messages are sent to and received from mailboxes, called ports in Mach Even system calls are made by messages When each task is created, a Kernel mailbox and a Notify mailbox are also created The former is used by the kernel to communicate with the task Kernel sends notification of event occurrences to the Notify port
3.5 Interprocess Communication: Mach Example Only three system calls are needed for message transfer Msg_send Msg_receive Msg_rpc (send a message and waits for exactly one return message from the receiver) The port_allocate system call creates a new mailbox and allocates space for its queue of messages
3.5 Interprocess Communication: Mach Example send and receive operations are quite flexible If the mailbox is not full, the message is copied and the sending thread continues Otherwise, the sending thread has four options: Wait indefinitely until there is room in the mailbox Wait at most n milliseconds Do not wait at all, but return immediately Temporarily cache a message (OS keeps only one such message)
3.6 Communication in Client-Server Systems: Socket A socket is defined as an endpoint for communication (IP address concatenated with a port #) Socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets
Consider this “Date” server: Sockets in Java 3 types of sockets Connection-oriented (TCP) Connectionless (UDP) MulticastSocket class– data can be sent to multiple recipients Consider this “Date” server:
3.6 Communication in Client-Server Systems: Remote Procedure Call (RPC) Remote procedure call abstracts procedure calls between processes on networked systems Stub (存根、票根): client-side proxy for the actual procedure on the server Client-side stub locates the server and marshals (安排整齊、列隊) the parameters Parameter marshalling packages the parameters into a form which may be transmitted over a network Server-side stub receives this message, unpacks the marshaled parameters, and performs the procedure on the server
3.6 Communication in Client-Server Systems: Remote Procedure Call (RPC)
3.6 Communication in Client-Server Systems: Execution of Remote Procedure Call (RPC)
3.6 Communication in Client-Server Systems: Ordinary Pipes Ordinary pipes allow communication in standard producer-consumer style Producer writes to one end (the write-end of the pipe) Consumer reads from the other end (the read-end of the pipe) Ordinary pipes are therefore unidirectional Require parent-child relationship between communicating processes Windows calls these anonymous pipes See Unix and Windows code samples in textbook
3.6 Communication in Client-Server Systems: Named Pipes Named pipes are more powerful than ordinary pipes Communication is bidirectional No parent-child relationship is necessary between the communicating processes Several processes can use the named pipe for communication Provided on both UNIX and Windows systems