730 likes | 746 Views
This chapter explores the process concept in operating systems, covering process scheduling, operations, IPC systems, and more. Learn about process states, control blocks, threads, and scheduling techniques.
E N D
Operating System Principles Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 3 Process Concept • 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 3 Process Concept
Chapter 3 Process Concept • Overview • Process Scheduling • Operations on Processes • Interprocess Communication • Examples of IPC Systems • Communication in Client-Server Systems • Summary • Exercises Chapter 3 Process Concept
3.1 Overview • 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 3 Process Concept
A process is a program in execution Text section Program code Data section Global variables Heap Dynamically allocated memory 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 3.1.1 The Process Chapter 3 Process Concept
Process in Memory Chapter 3 Process Concept
3.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 3 Process Concept
Diagram of process state Chapter 3 Process Concept
3.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 • Vary 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 3 Process Concept
3.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 3 Process Concept
Process control block (PCB) Chapter 3 Process Concept
Diagram showing CPUswitch from process to process Chapter 3 Process Concept
3.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 3 Process Concept
Chapter 3 Process Concept • Overview • Process Scheduling • Operations on Processes • Interprocess Communication • Examples of IPC Systems • Communication in Client-Server Systems • Summary • Exercises Chapter 3 Process Concept
3.2 Process Scheduling • Multiprogramming • Have some processes 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 3 Process Concept
3.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 3 Process Concept
The ready queue andvarious I/O device queues Chapter 3 Process Concept
Queueing-diagram representation of process scheduling Chapter 3 Process Concept
3.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 3 Process Concept
3.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 3 Process Concept
3.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 3 Process Concept
3.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 3 Process Concept
3.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 3 Process Concept
Addition ofmedium-term scheduling Chapter 3 Process Concept
3.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 3 Process Concept
Chapter 3 Process Concept • Overview • Process Scheduling • Operations on Processes • Interprocess Communication • Examples of IPC Systems • Communication in Client-Server Systems • Summary • Exercises Chapter 3 Process Concept
3.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 3 Process Concept
3.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 • Each process is identified by its unique process identifier (PID) • An integer number Chapter 3 Process Concept
A tree of processes on a typical Solaris system Chapter 3 Process Concept
3.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 is a duplicate of the parent. • Child has a program loaded into it. Chapter 3 Process Concept
3.3.1 Process Creation • UNIX example • fork system call creates a new process • A copy of the address space of the original process • execlp system call used after a fork to replace the process’ memory space with a new program. Chapter 3 Process Concept
C ProgramForking a Separate Process pid_t pid = fork(); if (pid<0) { /* error occurred */ fprintf(stderr, “Fork Failed”); exit(-1); } else if (pid==0) { /* child process */ execlp(“/bin/ls”, “ls”, NULL); } else { /* parent process */ wait(NULL); printf(“Child Complete”); exit(0); } Chapter 3 Process Concept
Process Creation Chapter 3 Process Concept
3.3.1 Process Creation • Windows example • CreateProcess Win32 API creates new process • STARTUPINFO: specify many properties of the new process • PROCESS_INFORMATION: contain a handle and the identifiers to the newly created process and its thread Chapter 3 Process Concept
3.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 a child to continue if its parent terminates. • Cascading termination Chapter 3 Process Concept
Chapter 3 Process Concept • Overview • Process Scheduling • Operations on Processes • Interprocess Communication • Examples of IPC Systems • Communication in Client-Server Systems • Summary • Exercises Chapter 3 Process Concept
3.4 Interprocess Communication • 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 • Reasons for process cooperation • Information sharing • Computation speed-up • Modularity • Convenience Chapter 3 Process Concept
3.4 Interprocess Communication • Cooperating processes require an interprocess communication (IPC) mechanism • To exchange data and information • Two fundamental models • Shared memory • A region of memory that is shared by cooperating processes is established. • Message passing • Messages are exchanged between cooperating processes • Most operating systems implement both Chapter 3 Process Concept
Communication Models Chapter 3 Process Concept
3.4 Interprocess Communication • Shared memory • Fast access • At memory speed • Problems exist • Protection and synchronization • Suitable for large amounts of data • Message passing • Slow access • Require kernel intervention • Easy to implement • Suitable for small amounts of data Chapter 3 Process Concept
3.4.1 Shared-Memory Systems • Normally, OS prevents one process from accessing another process’s memory • Shared-memory • One process creates the shared-memory segment • Other processes attach it to their address space • Exchange information by reading and writing data in the shared areas • Not under OS’s control • Process are responsible for ensuring not to write to the same location simultaneously Chapter 3 Process Concept
3.4.1 Shared-Memory Systems • Producer-Consumer Problem • A producer process produces information that is consumed by a consumer process. • The producer and consumer must be synchronized. • A buffer can be provided • Filled by the producer • Emptied by the consumer • Two types of buffers • unbounded-buffer • no practical limit on the size of the buffer • bounded-buffer • a fixed buffer size Chapter 3 Process Concept
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 3 Process Concept
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 3 Process Concept
Producer Process item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Chapter 3 Process Concept
Consumer Process item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } Chapter 3 Process Concept
3.4.2 Message-Passing Systems • Allow processes to communicate and to synchronize their actions • Without sharing the same address space • Useful in a distributed environment • Two operations • send(message) and receive(message) • Messages could be • Fixed size • Straightforward system-level implementation • More difficult programming • Variable size • More complex system-level implementation • Simpler programming Chapter 3 Process Concept
3.4.2 Message-Passing Systems • 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 3 Process Concept
3.4.2 Message-Passing Systems • Several methods for logical implementation • Direct or indirect communication • Synchronous or asynchronous communication • Automatic or explicit buffering Chapter 3 Process Concept
3.4.2.1 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 3 Process Concept