470 likes | 614 Views
Process Management. Chapter 6. Processes. Process Concept Process Scheduling Operating on Processes Threads Internet Links to Multi-threads: IBM Tom Wagner Site. Process Concept. An operating system executes a variety of programs: Batch systems -- Jobs
E N D
Process Management Chapter 6 A. Berrached:CMS:UHD
Processes • Process Concept • Process Scheduling • Operating on Processes • Threads • Internet Links to Multi-threads: • IBM • Tom Wagner Site A. Berrached:CMS:UHD
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. A. Berrached:CMS:UHD
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 A. Berrached:CMS:UHD
Process Manager • 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 A. Berrached:CMS:UHD
Process Model • 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. A. Berrached:CMS:UHD
Process Descriptor 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 A. Berrached:CMS:UHD
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 execution • From then on, process competes for CPU and other resources with other processes. A. Berrached:CMS:UHD
Process Address Space • A process address space includes: • program code • data section • stack section • Process Descriptor is kept in OS space. Stack Section Data Section Program text Main Memory A. Berrached:CMS:UHD
Process Model CONTD • Each process uses resources as its 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. A. Berrached:CMS:UHD
Process Model CONTD A. Berrached:CMS:UHD
Process Scheduling Queues A. Berrached:CMS:UHD
Context Switch A. Berrached:CMS:UHD
Process Creation A. Berrached:CMS:UHD
Process Creation A. Berrached:CMS:UHD
Process Termination • 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 ) A. Berrached:CMS:UHD
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 A. Berrached:CMS:UHD
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 initprocess) 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) A. Berrached:CMS:UHD
UNIX Processes • 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. A. Berrached:CMS:UHD
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. A. Berrached:CMS:UHD
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. A. Berrached:CMS:UHD
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 A. Berrached:CMS:UHD
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 • 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. A. Berrached:CMS:UHD
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 • 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. A. Berrached:CMS:UHD
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 */ A. Berrached:CMS:UHD
#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( ) ); } A. Berrached:CMS:UHD
Process Creation: fork( ) • 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. A. Berrached:CMS:UHD
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() ); A. Berrached:CMS:UHD
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() ); A. Berrached:CMS:UHD
Synchronization of Parent and Child • 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. A. Berrached:CMS:UHD
exit( ) • void exit(int status) • exit( ) closes all process' file descriptors, de-allocates 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( ). A. Berrached:CMS:UHD
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*/ A. Berrached:CMS:UHD
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*/ A. Berrached:CMS:UHD
Synch. of Parent and Child • If a parent terminates without waiting for a child, child becomes an orphan and is adopted by the system initprocess by setting their PPID to 1. • init periodically executes wait( ) to remove zombies from the system. A. Berrached:CMS:UHD
Wait() #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 A. Berrached:CMS:UHD
Child Executing a Different Program • Parent process calls fork to create a child process • child process calls an execsystem 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. A. Berrached:CMS:UHD
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*/ …….} A. Berrached:CMS:UHD
Example:Child File child_prog.c: main( ) { /* code to be executed by child process */ } • child_prog.c must be compiled into an executable file. A. Berrached:CMS:UHD
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. A. Berrached:CMS:UHD
Threads A. Berrached:CMS:UHD
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) A. Berrached:CMS:UHD
POSIX Threads • 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 A. Berrached:CMS:UHD
Why threads? • 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. A. Berrached:CMS:UHD
Threads for a windows system A. Berrached:CMS:UHD
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). A. Berrached:CMS:UHD