Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-2 Implementing Processes, Threads, and Resources Copyright © 2004 Pearson Education, Inc. 6 Operating Systems: A Modern Perspective, Chapter 6 Processes • • • • Process Concept Process Scheduling Operating on Processes Threads • Internet Links to Multi-threads: – IBM – Tom Wagner Site Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-3 Process Concept • An operating system executes a variety of programs: – Batch systems -- Jobs – Time-Shared system -- user programs or tasks • A Process -- a program in execution • A Process -- a schedulable unit of computation • There may be several processes executing the same program at the same time. E.g. several users running vi at the same time: – Each instance of vi creates a separate process. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-4 Implementing the Process Abstraction Pi CPU Pj CPU Pi Executable Memory Pj Executable Memory Pk CPU … Pk Executable Memory OS Address Space CPU ALU Control Unit Pi Address Space Pk Address Space … Pj Address Space Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Machine Executable Memory OS interface Slide 6-5 External View of the Process Manager Application Program Device Mgr UNIX Memory Mgr File Mgr exec() Memory Mgr File Mgr Process Mgr Device Mgr wait() CreateThread() CloseHandle() CreateProcess() WaitForSingleObject() Process Mgr fork() Windows Hardware Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-6 Slide 6-7 Process Manager • In a multi-programmed OS, several processes can be “executed at the same time”. • The Process Manager is that part of the OS that is responsible for managing all the processes on the system. • When the computer is powered on, there is only one program in execution: the initial process. • The initial process creates the OS, which can then create other processes as needed. • A process can create another process with a system call (e.g. fork in UNIX). • The created process is called a child process Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Manager Slide 6-8 • When a process is created, it specifies to the Process Manager its resource needs (e.g. memory requirements, files etc.) • The Process Manager allocates the needed resources and causes the process to be executed. • The process manager is responsible – for monitoring the state of each process executing on the system – process scheduling on CPU – process synchronization and deadlock – protection & security Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Manager Responsibilities Slide 6-9 • Define & implement the essential characteristics of a process and thread – Algorithms to define the behavior – Data structures to preserve the state of the execution • Define what “things” threads in the process can reference – the address space (most of the “things” are memory locations) • Manage the resources used by the processes/threads • Tools to create/destroy/manipulate processes & threads • Tools to time-multiplex the CPU – Scheduling the (Chapter 7) • Tools to allow threads to synchronization the operation with one another (Chapters 8-9) • Mechanisms to handle deadlock (Chapter 10) • Mechanisms to handle protection (Chapter 14) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Model Slide 6-11 • A process is composed of the following elements: – a program (code) – The data operated on by the process – A set of resources to provide an environment for execution – A Process Descriptor: a record kept by the OS to keep track of the progress of each process. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Modern Processes and Threads Thrdj in Pi Thrdk in Pi … Pi CPU … … OS interface … Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-12 Slide 6-13 Processes &Threads Stack State Copyright © 2004 Pearson Education, Inc. Static data Map Operating Systems: A Modern Perspective, Chapter 6 Program Map Resources Address Space Stack State Process Address Space • A process address space includes: – program code – data section – stack section • Process Descriptor is kept in OS space. Copyright © 2004 Pearson Education, Inc. Stack Section Data Section Program text Main Memory Operating Systems: A Modern Perspective, Chapter 6 Slide 6-14 Slide 6-15 The Address Space Address Space Address Binding Executable Memory Process Files Other objects Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Building the Address Space • Some parts are built into the environment – Files – System services • Some parts are imported at runtime – Mailboxes – Network connections • Memory addresses are created at compile (and run) time Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-16 Slide 6-17 Tracing the Hardware Process Hardware process progress Machine is Powered up Bootstap Process Interrupt Loader Manager Handler P1 Load the kernel Initialization Execute a thread Schedule Pn … Service an interrupt Copyright © 2004 Pearson Education, Inc. P,2 Operating Systems: A Modern Perspective, Chapter 6 The Abstract Machine Interface Slide 6-18 Application Program Abstract Machine Instructions Trap Instruction User Mode Instructions fork() open() OS User Mode Instructions Copyright © 2004 Pearson Education, Inc. Supervisor Mode Instructions Operating Systems: A Modern Perspective, Chapter 6 create() Context Switch Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-19 Slide 6-20 Context Switching Executable Memory Initialization Interrupt 1 Process Manager 7 8 Interrupt Handler 2 4 3 P1 9 5 P2 6 Pn Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Descriptors Slide 6-21 • OS creates/manages process abstraction • Descriptor is data structure for each process – – – – – – Register values Logical state Type & location of resources it holds List of resources it needs Security keys etc. (see Table 6.3 and the source code of your favorite OS) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Descriptor Slide 6-22 Contains information associated with a process: • PID • Process state • Owner • Parent process • List of child processes • list of allocated resources • list of open files • …. • Copy of CPU registers at the last time the process executed on the CPU Process Descriptor Table Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Process Descriptor EPROCESS KPROCESS NT Kernel NT Executive Copyright © 2004 Pearson Education, Inc. … uint32 uint32 … Byte … void … KernelTime; UserTime; state; *UniqueProcessId; Operating Systems: A Modern Perspective, Chapter 6 Slide 6-23 Windows NT Process Descriptor (2) Kernel process object including: Pointer to the page directory Kernel & user time Process base priority Process state List of the Kernel thread descriptors that are using this process Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-24 Windows NT Process Descriptor (3) Slide 6-25 Parent identification Exit status Creation and termination times. Memory status Security information executable image Process priority class used by the thread scheduler. A list of handles used by this process A pointer to Win32-specific information Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Thread Descriptor EPROCESS KPROCESS ETHREAD KTHREAD NT Kernel NT Executive Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-26 Creating a Process in UNIX pid = fork(); UNIX kernel Process Table … Copyright © 2004 Pearson Education, Inc. Process Descriptor Operating Systems: A Modern Perspective, Chapter 6 Slide 6-27 Process Model CONTD Slide 6-28 Process creation/initialization: – Process Descriptor is created and initialized – Resources needed by the process are allocated (e.g. files, memory to store code, data, and stack). – Process may inherent some resources from its parent (e.g. open files, etc.) – Process Descriptor must reflect all allocated resources – Process is loaded in memory, into its Address Space, ready to begin execution – From then on, process competes for CPU and other resources with other processes. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Creation Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-29 Process Creation Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-30 Process Termination Slide 6-31 • Process executes last statement and asks the operating system to delete it – process resources are de-allocated by the operating system • A process may be terminated by another process – A parent terminates the execution of its children • When a process exits what happens to its children? – do not allow a child to exist if its parent has terminated ==> cascaded termination (VMS) – allow children to exist after parent ==> orphan processes (UNIX ) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-32 Creating a Process in NT CreateProcess(…); Win32 Subsystem ntCreateProcess(…); … ntCreateThread(…); NT Executive Handle Table NT Kernel … Copyright © 2004 Pearson Education, Inc. Process Descriptor Operating Systems: A Modern Perspective, Chapter 6 Windows NT Handles Application Handle User Space Supervisor Space Executive Object Kernel Object NT Executive NT Kernel Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-33 Slide 6-34 Simple State Diagram Request Done Running Request Schedule Start Allocate Blocked Copyright © 2004 Pearson Education, Inc. Ready Operating Systems: A Modern Perspective, Chapter 6 Process Model CONTD Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-35 Process Scheduling Queues Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-36 UNIX State Transition Diagram Request Wait by parent Done Running zombie Sleeping Schedule Request I/O Request Start Allocate Runnable I/O Complete Uninterruptible Sleep Copyright © 2004 Pearson Education, Inc. Resume Traced or Stopped Operating Systems: A Modern Perspective, Chapter 6 Slide 6-37 Slide 6-38 Windows NT Thread States CreateThread Initialized Activate Dispatch Exit Wait Running Waiting Ready Wait Complete Wait Complete Transition Preempt Reinitialize Select Terminated Dispatch Standby Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Model CONTD Slide 6-39 • Each process uses resources as it executes; main memory, I/O devices, files, and the CPU • The CPU is also a hardware resource • During execution a process may request other resources (e.g. more memory) and may release some of its resources ==> dynamic allocation/de-allocation • When a process can NOT get its requested resources it gets blocked in a queue waiting for that resource. • Multiprogramming: While one process uses the CPU, the remaining are using I/O resources or waiting for a resource (I/O or CPU) to be available. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Resources Resource: Anything that a process can request, then be blocked because that thing is not available. R = {Rj | 0 j < m} = resource types C = {cj 0 | RjR (0 j < m)} = units of Rj available Reusable resource: After a unit of the resource has been allocated, it must ultimately be released back to the system. E.g., CPU, primary memory, disk space, … The maximum value for cj is the number of units of that resource Consumable resource: There is no need to release a resource after it has been acquired. E.g., a message, input data, … Notice that cj is unbounded. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-40 Using the Model Slide 6-41 • There is a resource manager, Mgr(Rj) for every Rj • Process pi can request units of Rj if it is currently running pi can only request ni cj units of reusable Rj pi can request unbounded # of units of consumable Rj •Mgr(Rj) can allocate units of Rj to pi request Mgr(Rj) Process allocate Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 A Generic Resource Manager Resource Manager Policy Blocked Processes ProcessProcess Process request() Process release() Resource Pool Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-42 Process Hierarchies Slide 6-43 • Parent-child relationship may be significant: parent controls children’s execution Done Request Running Yield Request Schedule Suspend Ready-Active Activate Allocate Suspend Blocked-Active Suspend Start Ready-Suspended Allocate Blocked-Suspended Activate Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-44 Process Manager Overview Program Process Abstract Computing Environment File Manager Process Deadlock Description Protection Synchronization Device Manager Devices Copyright © 2004 Pearson Education, Inc. Memory Manager Memory Scheduler CPU Operating Systems: A Modern Perspective, Chapter 6 Resource Resource Resource Manager Manager Manager Other H/W Slide 6-45 UNIX Organization Process Process Libraries Process File Manager System Call Interface Process Deadlock Description Protection Synchronization Device Manager Memory Manager Scheduler Monolithic Kernel Devices Copyright © 2004 Pearson Education, Inc. Memory CPU Operating Systems: A Modern Perspective, Chapter 6 Resource Resource Resource Manager Manager Manager Other H/W Slide 6-46 Windows NT Organization T Process T T T Process Process T T T T T Libraries Subsystem Subsystem Subsystem User NT Executive NT Kernel Hardware Abstraction Layer Processor(s) Copyright © 2004 Pearson Education, Inc. Main Memory Operating Systems: A Modern Perspective, Chapter 6 I/O Subsystem Devices UNIX Processes • Each process has its own address space – subdivided into code, data & stack – a.out file describes the address apace • OS creates a Process Descriptor to manage each process. The collection of all Process Descriptors is referred to as the Process Descriptor Table Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-47 Slide 6-48 UNIX Processes • Each process is assigned a unique process ID (PID) • The PID is essentially a pointer into the Process Table of the OS. • A process can use the system call getpid() to obtain its own PID • Each process has one parent process (the process that created it), except for process 1 • Process 1 ( the init process) is the ancestor of all other processes • a process can use the system call getppid() to obtain the PID of it parent (i.e. PPID) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 UNIX Processes Slide 6-49 • When Unix is first started, it has only one process. The process is called "init", and its PID is 1. • The "init" process creates other operating system processes to do OS functions • For each port supporting user logins (e.g. a terminal), init creates a process running the getty program. • The getty process waits for a user to begin using the port. • When the port begins to be used, getty creates a new process to run the login program. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-50 UNIX Processes • The login process prompts the user for username and password, reads the username and password and verifies by looking up the /etc/passwd file. • If login successful, the login process changes directory to the user's directory and creates a new process running the shell program specified in the user's entry of the /etc/passwd file. • The shell process displays a "shell prompt" on the terminal and waits for the user to type a command. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 UNIX Processes • When the user types a command, the shell process, reads it, parses it, verifies it, and creates a new process running the program specified in the command. In the mean time, the shell process gets suspended until the command process finishes. • When the command process is done, the shell process is resumed again. • When the user logs out, the shell process is terminated and the login process is resumed, etc. etc. etc. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-51 UNIX Processes • UNIX fork creates a process • UNIX wait allows a process to wait for a child to terminate • UNIX exec allows a child to run a new program Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-52 Creating Processes Slide 6-53 • UNIX fork() creates a process fork( ) creates a child process that is identical to its parent, except that it has: – a different and unique PID – a different PPID • fork() – creates a new address space for the child process – copies code, data and stack into new address space – provides child with access to open files of parent. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Creation: fork Slide 6-54 int pid; pid = fork(); • the fork is called by the parent but returns in both the parent and the child – In the parent, it returns the PID of the child process – in the child it returns 0 • If fork() fails no child is created and -1 is returned to the parent. • After the child is created, the parent and the child processes execute concurrently starting from the instruction following the fork. • Since only one can be using CPU at a time, either may go first. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Creation: fork Slide 6-55 int pid; pid = fork( ); if ( pid == 0 ) { /* code for child here */ exit(0); } if (pid < 0) { /* fork failed... Put error handling code here */ } /*remaining code for parent goes here */ Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-56 #include <stdio.h> int main( ) { int pid; if ( (pid = fork( ) ) == 0 ) { printf(“I am the child, my pid=%d and my parent pid=%d\n”, getpid( ), getppid( ) ); exit(0); } if (pid < 0) { fprintf(stderr, “fork failed\n”) exit(1); } printf(“I am the parent, my pid=%d\n”, getpid( ) ); } Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Example: Chain of Processes #include <stdio.h> int i, n, pid; for (i=1, i < n; ++i ) if ( ( pid=fork() ) != 0) break; fprintf(stdout,”This is process %d with parent %d\n”, getpid(),getppid() ); Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-57 Example: a fan of processes #include <stdio.h> int i, n, pid; for (i=1, i<n; ++i) if ( (pid=fork()) == 0) break; fprintf(stdout,”This is process %d with parent %d\n”, getpid(),getppid() ); Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-58 Synchronization of Parent and Child Slide 6-59 • What happens to the parent after it creates a child? – They both execute concurrently • If parent wants to wait until the child terminates before proceeding further it executes a wait() or waitpid() system call. • When a process is terminated (with exit( ) ), a signal is sent to its parent notifying it of the termination. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 exit( ) Slide 6-60 • void exit(int status) • exit( ) closes all process' file descriptors, deallocates code, data, and stack, and then terminates the process. • It sends a signal to the parent process telling of its termination status and waits until the parent accepts the signal. • A process that is waiting for its parent to accept its termination is called a "zombie" • A parent accepts a child's termination by executing wait( ). Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Synch. of Parent and Child Slide 6-61 int pid; pid = fork( ); if ( pid == 0 ) { /* child executes this part concurrently with parent */ exit(0); } /*parent works concurrently with child and independent of each other*/ Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Synch. of Parent and Child int pid; pid = fork( ); if ( pid == 0 ) { /* child executes this part concurrently with parent */ exit(0); } wait(...); /* parent waits for child*/ /*parent proceeds*/ Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-62 Synch. of Parent and Child • If a parent terminates without waiting for a child, child becomes an orphan and is adopted by the system init process by setting their PPID to 1. • init periodically executes wait( ) to remove zombies from the system. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-63 Wait() Slide 6-64 #include <sys/wait.h> int wait(int *status); 1. if there are no child processes, wait returns with -1 (an error) 2. if one or more processes are already in the zombie state, wait selects an arbitrary one, stores its status in status , and returns its PID 3. otherwise, wait sleeps until one of the child processes terminates and then goes to step 2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Child Executing a Different Program Slide 6-65 • Parent process calls fork to create a child process • child process calls an exec system call. • The exec system call replaces the address space of the child with a new program • several exec calls: execv, execvp, etc. int execv(char *filename, char *argv[ ]); • filename is the name of an executable file • argv is the command-line arguments for the executable program. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Example:Parent Slide 6-66 main( ) { int pid; /*code to set up the argv array for the child here*/ pid = fork(); if (pid==0) { execv(child_prog, argv); /* execv does not return unless there is an error*/ fprintf(stderr,”error in the exec…terminating child..”); exit(1); } wait( ); /*parent waits for child to terminate*/ …….} Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Example:Child File child_prog.c: main( ) { /* code to be executed by child process */ } • child_prog.c must be compiled into an executable file. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-67 Slide 6-68 Threads (read Chap2) • A thread (or lightweight process) is a basic unit of computation. It uses less state and less resources than heavyweight process • In a system that supports threads, each (heavyweight) process consist of one or more threads. • A traditional process is a process with one thread. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-69 Threads • Each thread has its own – – – – thread ID program counter stack space possibly some of its own data • A thread shares with its sibling threads: – code section – data section – operating system resources (e.g. open files, CPU) Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-70 POSIX Threads Slide 6-71 • DEC Unix conforms to the POSIX standard ==> # include <pthread.h> • When a thread is created it is assigned a thread ID, a stack, a starting address for execution. • pthread_create(ChildID, Thread_attributes, function_name, arguments) – – – – ChildID is the returned child ID Thread_attributes: set to NULL for default attributes function_name: function to be executed by the thread arguments: a pointer to the argument to be passed to the function Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Why threads? Slide 6-72 • Reduced context switching overhead • An application that needs to block occasionally waiting for I/O (e.g. disk): While one thread waits, a second thread can run and do other computation==> better performance for the application. • Windowing systems: – heavyweight process: physical screen manager – a thread for each window: all threads share the same physical screen. Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads for a windows system Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Slide 6-73 Slide 6-74 Why threads? • Since sibling threads use same data segment, data sharing among cooperating threads is easily achieved + Applications that require sharing a common buffer (e.g. producer-consumer) can benefit from thread utilization. – no protection between threads: synchronization among threads when accessing shared data must be enforced by the programmer. • Threads can be used in multiprocessor systems (each thread runs on a separate processor, they all share same address space on a shared memory). Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6