Download presentation
Presentation is loading. Please wait.
Published byChad Ira Weaver Modified over 9 years ago
1
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6
2
CS4315 A. Berrached:CMS:UHD2 Processes n Process Concept n Process Scheduling n Operating on Processes n Threads n Internet Links to Multi-threads: –IBMIBM –Tom Wagner SiteTom Wagner Site
3
CS4315 A. Berrached:CMS:UHD3 Process Concept n An operating system executes a variety of programs: –Batch systems -- Jobs –Time-Shared system -- user programs or tasks n A Process -- a program in execution n A Process -- a schedulable unit of computation n 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.
4
CS4315 A. Berrached:CMS:UHD4 Process Manager n In a multi-programmed OS, several processes can be “executed at the same time”. n The Process Manager is that part of the OS that is responsible for managing all the processes on the system. n When the computer is powered on, there is only one program in execution: the initial process. n The initial process creates the OS, which can then create other processes as needed. n A process can create another process with a system call (e.g. fork in UNIX). n The created process is called a child process
5
CS4315 A. Berrached:CMS:UHD5 Process Manager n When a process is created, it specifies to the Process Manager its resource needs (e.g. memory requirements, files etc.) n The Process Manager allocates the needed resources and causes the process to be executed. n 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
6
CS4315 A. Berrached:CMS:UHD6
7
CS4315 A. Berrached:CMS:UHD7 Process Model n 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.Process Descriptor
8
CS4315 A. Berrached:CMS:UHD8 Process Descriptor Contains information associated with a process: n PID n Process state n Owner n Parent process n List of child processes n list of allocated resources n list of open files n …. n Copy of CPU registers at the last time the process executed on the CPU Process Descriptor Table
9
CS4315 A. Berrached:CMS:UHD9 Process Model CONTD 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 executionAddress Space –From then on, process competes for CPU and other resources with other processes.
10
CS4315 A. Berrached:CMS:UHD10 Process Address Space n A process address space includes: –program code –data section –stack section n Process Descriptor is kept in OS space. Program text Data Section Main Memory Stack Section
11
CS4315 A. Berrached:CMS:UHD11 Process Model CONTD n Each process uses resources as its executes; main memory, I/O devices, files, and the CPU n The CPU is also a hardware resource n During execution a process may request other resources (e.g. more memory) and may release some of its resources ==> dynamic allocation/de- allocation n When a process can NOT get its requested resources it gets blocked in a queue waiting for that resource. n 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.
12
CS4315 A. Berrached:CMS:UHD12 Process Model CONTD
13
CS4315 A. Berrached:CMS:UHD13 Process Scheduling Queues
14
CS4315 A. Berrached:CMS:UHD14 Context Switch
15
CS4315 A. Berrached:CMS:UHD15 Process Creation
16
CS4315 A. Berrached:CMS:UHD16 Process Creation
17
CS4315 A. Berrached:CMS:UHD17 Process Termination n Process executes last statement and asks the operating system to delete it –process resources are de-allocated by the operating system n A process may be terminated by another process –A parent terminates the execution of its children n 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 )
18
CS4315 A. Berrached:CMS:UHD18 UNIX Processes n 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
19
CS4315 A. Berrached:CMS:UHD19 UNIX Processes n Each process is assigned a unique process ID (PID) n 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 n 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)
20
CS4315 A. Berrached:CMS:UHD20 UNIX Processes n When Unix is first started, it has only one process. The process is called "init", and its PID is 1. n The "init" process creates other operating system processes to do OS functions n For each port supporting user logins (e.g. a terminal), init creates a process running the getty program. n The getty process waits for a user to begin using the port. n When the port begins to be used, getty creates a new process to run the login program.
21
CS4315 A. Berrached:CMS:UHD21 UNIX Processes n The login process prompts the user for username and password, reads the username and password and verifies by looking up the /etc/passwd file. n 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. n The shell process displays a "shell prompt" on the terminal and waits for the user to type a command.
22
CS4315 A. Berrached:CMS:UHD22 UNIX Processes n 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. n When the command process is done, the shell process is resumed again. n When the user logs out, the shell process is terminated and the login process is resumed, etc. etc. etc.
23
CS4315 A. Berrached:CMS:UHD23 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
24
CS4315 A. Berrached:CMS:UHD24 Creating Processes 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 n 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.
25
CS4315 A. Berrached:CMS:UHD25 Process Creation: fork 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 n 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.
26
CS4315 A. Berrached:CMS:UHD26 Process Creation: fork 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 */
27
CS4315 A. Berrached:CMS:UHD27 #include 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( ) ); }
28
CS4315 A. Berrached:CMS:UHD28 Process Creation: fork( ) n After the child is created, the parent and the child processes execute concurrently starting from the instruction following the fork n Since only one can be using CPU at a time, either may go first.
29
CS4315 A. Berrached:CMS:UHD29 Example: Chain of Processes #include 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() );
30
CS4315 A. Berrached:CMS:UHD30 Example: a fan of processes #include 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() );
31
CS4315 A. Berrached:CMS:UHD31 Synchronization of Parent and Child n 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. n When a process is terminated (with exit( ) ), a signal is sent to its parent notifying it of the termination.
32
CS4315 A. Berrached:CMS:UHD32 exit( ) n void exit(int status) n exit( ) closes all process' file descriptors, de-allocates code, data, and stack, and then terminates the process. n It sends a signal to the parent process telling of its termination status and waits until the parent accepts the signal. n A process that is waiting for its parent to accept its termination is called a "zombie" n A parent accepts a child's termination by executing wait( ).
33
CS4315 A. Berrached:CMS:UHD33 Synch. of Parent and Child 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*/
34
CS4315 A. Berrached:CMS:UHD34 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*/
35
CS4315 A. Berrached:CMS:UHD35 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. n init periodically executes wait( ) to remove zombies from the system.
36
CS4315 A. Berrached:CMS:UHD36 Wait() #include 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
37
CS4315 A. Berrached:CMS:UHD37 Child Executing a Different Program n Parent process calls fork to create a child process child process calls an exec system call. n The exec system call replaces the address space of the child with a new program n several exec calls: execv, execvp, etc. int execv(char *filename, char *argv[ ]); n filename is the name of an executable file n argv is the command-line arguments for the executable program.
38
CS4315 A. Berrached:CMS:UHD38 Example:Parent 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*/ …….}
39
CS4315 A. Berrached:CMS:UHD39 Example:Child File child_prog.c: main( ) { /* code to be executed by child process */ } n child_prog.c must be compiled into an executable file.
40
CS4315 A. Berrached:CMS:UHD40 Threads (read Chap2) n A thread (or lightweight process) is a basic unit of computation. It uses less state and less resources than heavyweight process n In a system that supports threads, each (heavyweight) process consist of one or more threads. n A traditional process is a process with one thread.
41
CS4315 A. Berrached:CMS:UHD41 Threads
42
CS4315 A. Berrached:CMS:UHD42 Threads n Each thread has its own –thread ID –program counter –stack space –possibly some of its own data n A thread shares with its sibling threads: –code section –data section –operating system resources (e.g. open files, CPU)
43
CS4315 A. Berrached:CMS:UHD43 POSIX Threads n DEC Unix conforms to the POSIX standard ==> # include n When a thread is created it is assigned a thread ID, a stack, a starting address for execution. n 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
44
CS4315 A. Berrached:CMS:UHD44 Why threads? n Reduced context switching overhead n 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. n Windowing systems: –heavyweight process: physical screen manager –a thread for each window: all threads share the same physical screen.
45
CS4315 A. Berrached:CMS:UHD45 Threads for a windows system
46
CS4315 A. Berrached:CMS:UHD46 Why threads? n 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. n Threads can be used in multiprocessor systems (each thread runs on a separate processor, they all share same address space on a shared memory).
47
CS4315 A. Berrached:CMS:UHD47 User vs. kernel threads n A heavyweight process consists of one or more sibling threads, all competing for the processor n A thread scheduler (or thread manager) switches the processor among the threads. n In some systems, the thread manager is a user program, supported by a thread library (user- supported threads)-- older versions of UNIX n In other systems the thread manager is part of the OS (kernel-supported threads)-- OS/2, NT, Solaris
48
CS4315 A. Berrached:CMS:UHD48 User vs. kernel threads n User threads: –switching among threads is faster -- since they don’t involve calling the kernel –Any call to the OS can cause the whole heavywheight process to be context switched out of the CPU (since in this case OS supports only process scheduling). – Thread scheduling can be unfair, in favor of processes with large number of threads
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.