570 likes | 723 Views
PROCESS MANAGMENT. Processes and Process Control Blocks. Process can be defined as the followings: A program in execution An instance of a program running on a computer. The entity that can be assigned to and executed on a processor.
E N D
Processes and Process Control Blocks • Process can be defined as the followings: • A program in execution • An instance of a program running on a computer. • The entity that can be assigned to and executed on a processor. • A unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system resources.
Process… A batch system executes jobs, whereas a time- shared system has user programs, or tasks. Even on a single-user system, such as Microsoft Windows and Macintosh OS, a user may be able to run several programs at one time: a word processor, web browser, and e-mail package.
Process… Even if the user can execute only one program at a time, the operating system may need to support its own internal programmed activities, such as memory management.
Process… OS can manage the execution of applications so that: Resources are made available to multiple applications. The physical processor is switched among multiple applications so all will appear to be progressing. The processor and I/O devices can be used efficiently.
Process… Two essential elements of a process are program code(which may be shared with other processes that are executing the same program) and a set of data associated with that code. At any given point in time, while the program is executing, this process can be uniquely characterized by a number of elements, including the following:
Process… The information in the list is stored in a data structure, typically called a process control block (PCB), that is created and managed by the OS and uniquely identifies a process.
Process… Accounting information: This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on. status information: The information includes the list of I/O devices allocated to this process, a list of open files(Active files) , and so on.
Process… The process control block is the key tool that enables the OS to support multiple processes and to provide for multiprocessing. When a process is interrupted, the current values of the program counter and the processor registers (context data) are saved in the appropriate fields of the corresponding process control block, and the state of the process is changed to some other value, such as blocked or ready.
Process States We can characterize the behavior of an individual process by listing the sequence of instructions that execute for that process. Such a listing is referred to as a trace of the process. We can characterize behavior of the processor by showing how the traces of the various processes are interleaved. A small dispatcher program is used to switch the processor from one process to another.
A two state process model The operating system’s principal responsibility is controlling the execution of processes; this includes determining the interleaving pattern for execution and allocating resources to processes.
Process States … The Creation and Termination of Processes When a new process is to be added to those currently being managed, the OS builds the data structures that are used to manage the process and allocates address space in main memory to the process. When one process spawns another, the former is referred to as the parent process, and the spawned process is referred to as the child process.
Process States … Reasons for job creation
Process States … Any computer system must provide a means for a process to indicate its completion. A batch job should include a Halt instruction or an explicit OS service call for termination. In the former case, the Halt instruction will generate an interrupt to alert the OS that a process has completed. For an interactive application, the action of the user will indicate when the process is completed. On a personal computer or workstation, a user may quit an application (e.g., word processing or spreadsheet). All of these actions ultimately result in a service request to the OS to terminate the requesting process.
A five state model Five state process model
Process States … Running: The process that is currently being executed Ready: A process that is prepared to execute when given the opportunity. Blocked/Waiting: A process that cannot execute until some event occurs, such as the completion of an I/O operation.
Process States … New: A process that has just been created but has not yet been admitted to the pool of executable processes by the OS. Typically, a new process has not yet been loaded into main memory, although its process control block has been created. Terminated: A process that has been released from the pool of executable processes by the OS, either because it halted or because it aborted for some reason.
Process States… • The new process will terminate, usually due to one of the following conditions: 1. Normal exit (voluntary). 2.Error exit (voluntary).3.Fatal error (involuntary).4.Killed by another process(involuntary).
Process States… • Process can be executed in one of the following modes: • User mode • Kernel mode • Considerations of memory protection and privileged instructions lead to the concept of modes of operation. • A user program executes in a user mode, in which certain areas of memory are protected from the user’s use and in which certain instructions may not be executed. • The monitor executes in a system mode, or called kernel mode, in which privileged instructions may be executed and in which protected areas of memory may be accessed.
OS Control Structures • If the OS is to manage processes and resources, it must have information about the current status of each process and resource. • Although the details will differ from one OS to another, fundamentally, all operating systems maintain information in these four categories. • Memory tables • I/O tables • File tables and • Process tables
OS Control… To manage and control a process the OS must know first where the process is located; second, it must know the attributes of the process that are necessary for its management (e.g., process ID and process state). A process includes a set of programs to be executed, data, stack, and attributes. We can refer to this collection as the process image.
Process/context Switching At some time, a running process is interrupted and the OS assigns another process to the Running state and turns control over to that process. A process switch may occur any time that the OS has gained control from the currently running process.
Process/context Switching First, let us consider system interrupts. Actually, we can distinguish, as many systems do, two kinds of system interrupts, one of which is simply referred to as an interrupt, and the other as a trap. The former is due to some sort of event that is external to and independent of the currently running process, such as the completion of an I/O operation. The latter relates to an error or exception condition generated within the currently running process, such as an illegal file access attempt.
Cooperating Processes The concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process is independent if it cannot affect or be affected by the other processes executing in the system. Clearly, any process that does not share any data (temporary or persistent) with any other process is independent. On the other hand, a process is cooperating if it can affect or be affected by the other processes executing in the system. Clearly, any process that shares data with other processes is a cooperating process.
Cooperating Processes… We may want to provide an environment that allows process cooperation for several reasons: Information sharing: Since several users may be interested in the same piece of information(for instance, a shared file), we must provide an environment to allow concurrent access to these types of resources. Computation speedup: If we want a particular task to run faster, we must break it into subtasks, each of which will be executing in parallel with the others. Such a speedup can be achieved only if the computer has multiple processing elements (such as CPU or I/O channels).
Cooperating Processes… Modularity: We may want to construct the system in a modular fashion, dividing the system functions into separate processes or threads. Convenience: Even an individual user may have many tasks on which to work at one time. For instance, a user may be editing, printing, and compiling in parallel.
Cooperating Processes… To illustrate the concept of cooperating processes, let us consider the producer-consumer problem, which is a common paradigm for cooperating processes. A producer process produces information that is consumed by a consumer process. For example, a print program produces characters that are consumed by the printer driver. A compiler may produce assembly code, which is consumed by an assembler. The assembler, in turn, may produce object modules, which are consumed by the loader.
Threads The concept of a process as embodying two characteristics: Resource ownership: A process includes a virtual address space to hold the process image; recall that the process image is the collection of program, data, stack, and attributes defined in the process control block. From time to time, a process may be allocated control or ownership of resources. The OS performs a protection function to prevent unwanted interference between processes with respect to resources. Scheduling/execution: The execution of a process follows an execution path(trace) through one or more programs. This execution may be interleaved with that of other processes. Thus, a process has an execution state and a dispatching priority and is the entity that is scheduled and dispatched by the OS.
Threads… These two characteristics are independent and could be treated independently by the OS. To distinguish the two characteristics, the unit of dispatching is usually referred to as a thread or lightweight process, while the unit of resource ownership is usually still referred to as a process or task.
Threads… • Processes do not share resources well • high context switching overhead • Idea: Separate concurrency from protection • Multithreading: a single program made up of a number of different concurrent activities • A thread (or lightweight process) • basic unit of CPU utilization; it consists of: program counter, register set and stack space • A thread shares the following with peer threads: code section, data section and OS resources (open files, signals) • No protection between threads • Collectively called a task. • Heavyweight process is a task with one thread.
Threads… Benefits of Multithreaded programming Responsiveness Resource Sharing Economy: It is much more time consuming to create and manage processes that threads. Eg. In Solaris, creating a process is thirty times slower than creating thread and context switching is five times slower. Utilization of Multiprocessor Architectures
Threads… Multithreading Multithreading refers to the ability of an OS to support multiple, concurrent paths of execution within a single process. In a multithreaded environment, a process is defined as the unit of resource allocation and a unit of protection.
Threads… Thread State • State shared by all threads in process/addr space • Contents of memory (global variables, heap) • I/O state (file system, network connections, etc) • State “private” to each thread • Kept in TCB (Thread Control Block) • CPU registers (including, program counter) • Execution stack • Parameters, Temporary variables • return PCs are kept while called procedures are executing
Threads… • Thread context switch still requires a register set switch, but no memory management related work!! • Thread states – • ready, blocked, running, terminated • Threads share CPU and only one thread can run at a time. • No protection among threads.
Threads… Types of threads • Kernel Supported Threads (Mach and OS/2) • User Level Threads • Hybrid approach implements both user-level and kernel-supported threads (Solaris 2)
Threads… • Kernel Threads • Supported by the Kernel • Native threads supported directly by the kernel • Every thread can run or block independently • Downside of kernel threads: a bit expensive • Need to make a crossing into kernel mode to schedule • Examples • Windows XP/2000, Solaris, Linux,Tru64 UNIX, Mac OS X, Mach, OS/2
Threads… • User Level Threads • Supported above the kernel, via a set of library calls at the user level. • Thread management done by user-level threads library • User program provides scheduler and thread package • May have several user threads per kernel thread • User threads may be scheduled non-premptively relative to each other (only switch on yield()) • Advantages • Cheap, Fast • Threads do not need to call OS and cause interrupts to kernel • Disadv: If kernel is single threaded, system call from any thread can block the entire task. • Example thread libraries: • POSIX Pthreads, Win32 threads, Java threads
Threads… Multithreading Models • There are three common ways of establishing relationship between user threads and kernel threads • Many – to – one • One – to – one • Many – to – many
Threads… • Efficient • The entire process may block if a thread make a blocking system call • Examples: • Solaris Green Threads • GNU Portable Threads Many – to – one model Maps many user level threads to one kernel thread
Threads… • One – to – One model • Each user thread maps to a kernel thread • Creating kernel threads for each user can burden the performance of an application: Most OS limit the number of threads • Example: • Windows NT/XP/2000; Linux; Solaris 9 and later
Threads… • Many – to – Many Model • Allows many user level threads to be mapped to many kernel threads • Allows the OS to create a sufficient number of kernel threads • Solaris prior to version 9 • Windows NT/2000 with the ThreadFiber package
Interprocess Communication (IPC) • Separate address space isolates processes • High Creation/Memory Overhead; (Relatively) High Context-Switch Overhead • Mechanism for processes to communicate and synchronize actions. • Via shared memory - Accomplished by mapping addresses to common DRAM • Read and Write through memory • Via Messaging system - processes communicate without resorting to shared variables. • send() and receive() messages • Can be used over the network! • Messaging system and shared memory not mutually exclusive can be used simultaneously within a single OS or a single process.
Shared Memory Communicatrion • Communication occurs by “simply” reading/writing to shared address page • Really low overhead communication • Introduces complex synchronization problems
Cooperating Processes via Message Passing • IPC facility provides two operations. send (message) - message size can be fixed or variable receive (message) • If processes P and Q wish to communicate, they need to: • establish a communication link between them • exchange messages via send/receive • Fixed vs. Variable size message • Fixed message size – straightforward physical implementation, programming task is difficult due to fragmentation • Variable message size - simpler programming, more complex physical implementation.
Direct Communication • Sender and Receiver processes must name each other explicitly: • send(P, message) - send a message to process P • receive(Q, message) - receive a message from process Q • Properties of communication link: • Links are established automatically. • A link is associated with exactly one pair of communicating processes. • Exactly one link between each pair. • Link may be unidirectional, usually bidirectional.
Indirect Communication • Messages are directed to and received from mailboxes (also called ports) • Unique ID for every mailbox. • Processes can communicate only if they share a mailbox. send(A, message) /* send message to mailbox A */ receive(A, message) /* receive message from mailbox A */ • Properties of communication link • Link established only if processes share a common mailbox. • Link can be associated with many processes. • Pair of processes may share several communication links • Links may be unidirectional or bidirectional
Mailbox … • Operations • create a new mailbox • send/receive messages through mailbox • destroy a mailbox • Issue: Mailbox sharing • P1, P2 and P3 share mailbox A. • P1 sends message, P2 and P3 receive… who gets message?? • Possible Solutions • disallow links between more than 2 processes • allow only one process at a time to execute receive operation • allow system to arbitrarily select receiver and then notify sender.