Popular in Department
This 24 page Study Guide was uploaded by Sharadha on Monday September 28, 2015. The Study Guide belongs to at Santa Clara University taught by in Winter2015. Since its upload, it has received 24 views.
Reviews for OS_CHAPTER2PROCESS.pdf
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/28/15
A process is basically a program in execution Eg A word processing program being run by an individual user on a PC A system task such as sending output to a printer Associated With each process is its address space a list of memory locations from O to some maximum which the process can read and write The address space contains the executable program the program s data and its stack Also associated With each process is a set of resources commonly including registers including the program counter and stack pointer ie program code is in text section It also includes the current activity as represented by the value of the program counter and the contents of the processor s registers A process generally also includes the process stack Which contains temporary data such as function parameters return addresses and local variables and a data section Which contains global variables A process may also include a heap Which is memory that is dynamically allocated during process run time max ataals haap data lam Figura 31 Praaaaa in mamawg Only one process will run at one time In multiprogramming the CPU switches from process to process quickly running each for tens or hundreds of milliseconds While strictly speaking at any one instant the CPU is running only one process in the course of 1 second it may work on several of them giving the illusion of parallelism This rapid switching back and forth is called multiprogramming EgLets say multiple process are running The user may have started a video editing program and instructed it to convert a one hour video to a certain format something that can take hours and then gone off to surf the Web Meanwhile a background process that wakes up periodically to check for incoming email may have started running Thus we have at least three active processes the video editor the Web browser and the email receiver Periodically the operating system decides to stop running one process and start running another based on time or space multiplexing Dmeecmnter ah Fuur enumere a h FI ClEEEE quot ii II my quot 1 E L r D THREE a bl El Figure 1 1 a Mul i mgmmn ng pregame it E mepnml made ef independent eequeuil al prmeee lie lly ensue preng i3 amine at A process is suspended temporarily like this it must later be restarted in exactly the same state it had when it was stopped This means that all information about the process must be explicitly saved somewhere during the suspension In many operating systems all the information about each process other than the contents of its own address space is stored in an operating system table called the process table which is an array of structures one for each process currently in existence PI39OCCSS creation Four principal events cause processes to be created 1 System initialization 2 Execution of a process creation system call by a running process 3 A user request to create a new process 4 Initiation of a batch job When an operating system is booted typically numerous processes are created Some of these are foreground processes that is processes that interact with human users and perform work for them Others run in the background and are not associated with particular users but instead have some specific functiondaemons During the course of execution a process may create several new processes As mentioned earlier the creating process is called a parent process and the new processes are called the children of that process Each of these new processes may in turn create other processes forming a tree of processes Most operating systems including UNIX Linux and Windows identify processes according to a unique process identi er or pid which is typically an integer number The pid provides a unique value for each process in the system and it can be used as an index to access various attributes of a process within the kernel EgUNIX initializes itself when it is started just after the computer is booted A special process called init is present in the boot image The init process which always has a pid of 1 serves as the root parent process for all user processes Once the system has booted When it starts running it reads a file telling how many terminals there are Then it forks off a new process per terminal These processes wait for someone to log in If a login is successful the login process executes a shell to accept commands These commands may start up more processes and so forth Thus all the processes in the whole system belong to a single tree with init at the root In contrast Windows has no concept of a process hierarchy All processes are Equal 3am iii LEM pid my Figure 38 A of prueaeaae on a typical Uinux System On UNIX and Linux systems we can obtain a listing of processes by using the ps command For example the command ps el In general when a process creates a child process that child process will need certain resources CPU time memory files IO deVices to accomplish its task A child process may be able to obtain its resources directly from the operating system or it may be constrained to a subset of the resources of the parent process Two possibilities for execution exist 1 The parent continues to execute concurrently with its children 2 The parent waits until some or all of its children have terminated There are also two address space possibilities for the new process 1 The child process is a duplicate of the parent process it has the same program and data as the parent 2 The child process has a new program loaded into it include ltstdiohgt include ltstdlibhgt include ltsystypeshgt include ltunistdhgt int mainvoid char gt myargs5 pidt pid myargs0 quotchi1dexequot myargs1 quotarglquot myargs2 quotarg2quot myargs3 NULL puts quotforkingquot switch pid fork case 1 gt lt Fork has failed perror quotforkquot break case 0 gt lt This is processed by the child execv quotchildexequot myargs putsquotUh oh If this prints execv must have failedquot eXitEXITFAILURE break default gt lt This is processed by the parent puts quotThis is a message from the parentquot break puts quotEnd of parent programquot return 0 NOTE Process can also be created using system spawn and fork execv SYSTEMO Prototype int system const char s This function executes a command specified in s and returns after the command has been completed A few reasons not to use this method are D The child program is uninterruptible from the parent program This means that you cannot stop the program you have started If it takes 5 hours to complete that39s how long your program will wait D You cannot communicate or share variables With the child process D For various security reasons its unsafe and may leave your system open to exploitation D In system terms its relatively slow Having said that it is easy to use and these things have their place Here is a example of how to use it include ltstdlibhgt include ltstdiohgt include ltstringhgt int main char childl quotchildexequot char child2BUFSIZ gtllt Execute by passing the name directly gtquot system quotchildexequot gtllt Execute by passing an array name gtquot system childl gtllt Build a buffer and execute the commands Within it gtquot strcpy child2 quotchildexequot strcat child2 quot aparm bquot printf quotExecuting snquot child2 system child2 return 0 FORKO System call fork is used to create processes Copy on write technique It takes no arguments and returns a process ID The purpose of fork is to create a new process which becomes the child process of the caller After a new child process is created both processes will execute the next instruction following the fork system call Therefore we have to distinguish the parent from the child This can be done by testing the returned value of fork A newly forked child process continues to execute the same program as its parent process at the point where the fork call returns You can use the return value from fork to tell whether the program is running in the parent process or the child o If fork returns a negative value the creation of a child process was unsuccessful o fork returns a zero to the newly created child process o fork returns a positive value gt O the process ID of the child process to the parent The returned process ID is of type pidt defined in systypesh Normally the process ID is an integer Moreover a process can use function getpid to retrieve the process ID assigned to this process After forking a child process both the parent and child processes continue to execute normally If you want your program to wait for a child process to finish executing before continuing you must do this explicitly after the fork operation by calling wait or waitpid see section Process Completion These functions give you limited information about why the child terminated for example its exit status code Having several processes run the same program is only occasionally useful But the child can execute another program using one of the exec functions see section Executing a File The program that the process is executing is called its process image Starting execution of a new program causes the process to forget all about its previous process image when the new program exits the process exits too instead of returning to the previous process image Let us take an example to make the above points clear This example does not distinguish parent and the child processes include ltstdiohgt include ltstringhgt include ltsystypeshgt define MAXCOUNT 200 define BUFSIZE 100 void mainvoid pidt pid int i char bufBUFSIZE fork pid getpido for i 1 i lt MAXCOUNT i sprintfbuf quotThis line is from pid d value dnquot pid i writel buf strlenbuf Suppose the above program executes up to the point of the call to fork marked in red color Parent main gt ork pid If the call to fork is executed successfully Unix will o make two identical copies of address spaces one for the parent and the other for the child o Both processes will start their execution at the next statement following the fork call In this case both processes will start their execution at the assignment statement as shown below Parent Child main main fork fork gtpid gtpid Both processes start their execution right after the system call fork Since both processes have identical but separate address spaces those variables initialized before the fork call have the same values in both address spaces Since every process has its own address space any modifications will be independent of the others In other words if the parent changes the value of its variable the modi cation will only affect the variable in the parent process39s address space Other address spaces created by fork calls will not be affected even though they have identical variable names If you run this program you might see the following on the screen This line is from pid 3456 value 13 This line is from pid 3456 value 14 This line is from pid 3456 value 20 This line is from pid 4617 value 100 This line is from pid 4617 value 101 This line is from pid 3456 value 21 This line is from pid 3456 value 22 Process ID 3456 may be the one assigned to the parent or the child Due to the fact that these processes are run concurrently their output lines are intermixed in a rather unpredictable way Moreover the order of these lines are determined by the CPU scheduler Hence if you run this program again you may get a totally different result Consider one more simple example Which distinguishes the parent from the child include ltstdiohgt include ltsystypeshgt define MAXCOUNT 200 void ChildProcessvoid child process prototype void ParentProcessvoid parent process prototype void mainvoid pidt pid pid fork if pid 0 ChildProcess else ParentProcess void ChildProcessvoid int i for i 1 i lt MAXCOUNT i printfquot This line is from child value dnquot i printfquot Child process is done nquot void ParentProcessvoid int i for i 1 i lt MAXCOUNT i printfquotThis line is from parent value dnquot i printfquot Parent is done nquot In this program both processes print lines that indicate 1 Whether the line is printed by the child or by the parent process and 2 the value of variable i For simplicity printf is used When the main program executes fork an identical copy of its address space including the program and all data is created System call fork returns the child process ID to the parent and returns 0 to the child process The following gure shows that in both address spaces there is a variable pid The one in the parent receives the child39s process ID 3456 and the one in the child receives 0 Parent Child main maino gt pidfork gt pidf0rk0 if pid 0 if pid 0 ChildProcess ChildProceSSU else else ParentProcess ParentProcess void ChildProcess void ChildProcess n I n u u n a o c o 39 39 39 Now both programs i e the parent and child will execute independent of each other starting at the next statement Parent Child mam mama i pidfork pidfork gt if pid 0 gt if pid 0 ChildProcess ChildProcess else else ParentProcess ParentProcess void ChildProcess void ChildProcess u void ParentProcess void ParentProcess In the parent fork will return positive value indication parent process since pid is non zero it calls function ParentProcessO On the other hand the child has a zero pid and calls ChildProcessO as shown below Parent Child mm min 7 i pidfork pidfork if pid 0 if pid 0 ChildProcess 39 ChildProcess 39 else else ParentProcess ParentProcess void ChildProcess void ChildProcess void ParentProcess void ParentProcess Due to the fact that the CPU scheduler will assign a time quantum to each process the parent or the child process will run for some time before the control is switched to the other and the running process will print some lines before you can see any line printed by the other process Executing another program You can use these functions to make a child process execute a new program after it has been forked Function int execv const char filename char const argv The execv function executes the le named by filename as a new process image The argv argument is an array of null terminated strings that is used to provide a value for the argv argument to the main function of the program to be executed The last element of this array must be a null pointer By convention the first element of this array is the file name of the program sans directory names See section Program Arguments for full details on how programs can access these arguments The environment for the new process image is taken from the environ variable of the current process image see section Environment Variables for information about environments Function int execl const char filename const char argO This is similar to execv but the argv strings are specified individually instead of as an array A null pointer must be passed as the last such argument Function int execve const char filename char const argv char const env This is similar to execv but permits you to specify the environment for the new program explicitly as the env argument This should be an array of strings in the same format as for theenviron variable Function int execle const char filename const char argO char const env This is similar to excel but permits you to specify the environment for the new program explicitly The environment argument is passed following the null pointer that marks the last argvargument and should be an array of strings in the same format as for the environ variable Function int execvp const char filename char const argv The execvp function is similar to execv except that it searches the directories listed in the PATH environment variable see section Standard Environment Variables to find the full file name of a file from filename if filename does not contain a slash This function is useful for executing system utility programs because it looks for them in the places that the user has chosen Shells use it to run the commands that users type Function int execlp const char filename const char argO This function is like execl except that it performs the same file name searching as the execvp function include ltstdiohgt include ltstdlibhgt include ltsystypeshgt include ltunistdhgt int mainvoid char gt myargs5 pidt pid myargs0 quotchildexequot myargsl quotarglquot myargs2 quotarg2quot myargs3 NULL puts quotforkingquot switch pid fork case 1 gt lt Fork has failed gt perror quotfor quot break case 0 gt lt This is processed by the child gt execv quotchildexequot myargs putsquotUh oh If this prints execv must have failedquot eXitEXITFAILURE break default gt lt This is processed by the parent gt puts quotThis is a message from the parentquot break puts quotEnd of parent programquot return 0 Process Identi cation The pidt data type represents process IDs You can get the process ID of a process by calling getpid The function getppid returns the process ID of the parent of the current process this is also known as the parent process ID Your program should include the header files unistdh39 and systypesh39 to use these functions Data Type pidt The pidt data type is a signed integer type which is capable of representing a process ID In the GNU library this is an int Function pidt getpid void The getpid function returns the process ID of the current process Function pidt getppid void The getppid function returns the process ID of the parent of the current process Process Completion The functions described in this section are used to wait for a child process to terminate or stop and determine its status These functions are declared in the header file syswaith39 Function pidt waitpid pidt pid int StatuSptr int options The waitpid function is used to request status information from a child process Whose process ID is pid Normally the calling process is suspended until the child process makes status information available by terminating This will return the process id of the child which completes and eXits These symbolic constants are defined as values for the pid argument to the waitpid function WAITANY This constant macro whose value is 1 specifies that waitpid should return status information about any child process WAITMYPGRP This constant with value 0 specifies that waitpid should return status information about any child process in the same process group as the calling process These symbolic constants are defined as ags for the options argument to the waitpid function You can bitwise OR the ags together to obtain a value to use as the argument WNOHANG This ag specifies that waitpid should return immediately instead of waiting if there is no child process ready to be noticed WUNTRACED This ag specifies that waitpid should report the status of any child processes that have been stopped as well as those that have terminated Function pidt wait int statusptr This is a simplified version of waitpid and is used to wait until any one child process terminates The call wait ampstatus is exactly equivalent to waitpid 1 ampstatus 0 This will return the process id of the child to parent PROCESS TERMINATION one of the following conditions 1 Normal exit voluntary 2 Error exit voluntary 3 Fatal error involuntary 4 Killed by another process involuntary Most processes terminate because they have done their work When a compiler has compiled the program given to it the compiler executes a system call to tell the operating system that it is finished This call is exit in UNIX The second reason for termination is that the process discovers a fatal error For example if a user types the command cc fooc to compile the program fooc and no such file exists the compiler simply announces this fact and exits The third reason for termination is an error caused by the process often due to a program bug Examples include executing an illegal instruction referencing nonexistent memory or dividing by zero The fourth reason a process might terminate is that the process executes a system call telling the operating system to kill some other process In UNIX this call is kill Usually such a system call can be invoked only by the parent of the process that is to be terminated EXIT void exitint status terminates the process which calls this function and returns the exit status value By convention a status of 0 means normal termination any other value indicates an error or unusual occurrence KILL int kill pidt pid int Sig The kill function sends a signal to a process or process group specified by pid The signal to be sent is specified by Sig and is either 0 or one of the signals from the ltsignalhgt header file PROCESS STATES three states a process may be in 1 Running actually using the CPU at that instant 2 Ready runnable temporarily stopped to let another process run 3 Blocked unable to run until some external event happens to unblock it eg due to waitsleepinterrupt 3 F r Rhea 77 1 1 Preeeee t fer input 3 E E Scheduler p eke nether preeeee 3 Scheduler p dke lih e my 4 Input heeemee eve letle Fing 112 pmeese can be in blacked er ready State be tween these states are 35 511mm Four transitions are possible among these three states as shown Transition 1 occurs when the operating system discovers that a process cannot continue right now In some systems the process can execute a system call such as pause to get into blocked state In other systems including UNIX when a process reads from a pipe or special file e g a terminal and there is no input available the process is automatically blocked Transitions 2 and 3 are caused by the process scheduler a part of the operating system without the process even knowing about them Transition 2 occurs when the scheduler decides that the running process has run long enough and it is time to let another process have some CPU time Transition 3 occurs when all the other processes have had their fair share and it is time for the first process to get the CPU to run again Transition 4 occurs when the external event for which a process was waitingsuch as the arrival of some input happens If no other process is running at that instant transition 3 will be triggered and the process will start running Otherwise it may have to wait in ready state for a little while until the CPU is available and its turn comes Ready to blocked not possible Blocked to running possible when 1 process runs and no other process is present or running it can bypass ready state You can add two new states to this New The process is being created forking a child process Terminated The process has finished execution process eXits or is killed tanmlinate exit V V rquot 7 numniinugs l l 1 HIE or event wait Figure Diagram of pron 33 atate PROCESS IMPLEMENTATION To implement the process model the operating system maintains a table an array of structures called the process table with one entry per process Some authors call these entries process control blocks This entry contains important information about the process state including its program counter stack pointer memory allocation the status of its open les its accounting and scheduling information and everything else about the process that must be saved when the process is switched from running to ready or blocked state so that it can be restarted later as if it had never been stopped Associated with each IO class is a location typically at a fixed location near the bottom of memory called the interrupt vector It contains the address of the interrupt service procedure Suppose that user process 3 is running when a disk interrupt happens User process 3 s program counter program status word and sometimes one or more registers are pushed onto the current stack by the interrupt hardware The computer then jumps to the address specified in the interrupt vector That is all the hardware does From here on it is up to the software in particular the interrupt service procedure The interrupt service procedure serves the interrupt When it has done its job possibly making some process now ready the scheduler is called to see who to run next After that control is passed back to the code to load up the registers and memory map for the nowcurrent process and start it running Eresees management Memerg management File management Ftegisters tE39eirtter tel test segment inte Fleet ireeterg Pregram saunter Feintette ttata segment inte Werttittg sirestem ngram status were tE39eintet tel state segment ittte File desertpters Steels painter User It Pmeess state Emma ID F39rieli itijlr Seltettuttng parameters Pmeess llID Parent pmeess Preeess greup Signals time when preeess started EPLll ttme insert Elttttlrett s Et 39tJ time time at neat allarm Figures 24 Serum ef le elds sf 3 typieal presses table entry 39 re stselss pregrem 1eur1t39l39r39a ten re testis new pmgrsm eenuntr trem interrupt U t t39 Assemy illsngugre presure rmes registers AL Assemy illsnguge piresure sets up new stasis 5 I i siruiee nuns itemsquot reeds and ers input 6 Eehe u39ller testicles whieh preeess ts te run nest F I present turns 133 th sssy sense t1 Assemy Ilsnguege eresure starts up new slurrth pres Figure 5 Swaten What the Issssst level ee 5m system illness when preeess Pu eperettng system ereeess F1 interrupt er system sell beseeuting f g ease steite itnte F GE relteee stete trem F31 N A J 5 Mlle interrupt er system ssll eseeuting t ease stete tnte F61 s le relteed stete trem Fi BE beessetting Figs 34 tegrern shewing ELI switsh trern ts presses ORPHAN PROCESS parent terminated or crsahed thereby leaving its child processes as orphans Linux and UNIX address this scenario by assigning the init process as the new parent to it This is called reparenting or adoption So it wont terminate U need to use ctrl C to terminate it on shell Orphan processes take resources while they are in the system and can potentially leave a server starved for resources Having too many Orphan processes will overload the init process and can hangup a Linux system DAEMON PROCESS A daemon is a computer program that runs as a background process rather than being under the direct control of an interactive user Daemon process is a process orphaned intentionally In a UNIX environment the parent process of a daemon is often but not always the init process A daemon is usually created by a process forking a child process and then immediately exiting thus causing init to adopt the child process ZOMBIE PROCESS a zombie process or defunct process is a process that has completed execution but still has an entry in the process table This entry is still needed to allow the parent process to read its child39s exit status unlike normal processes the kill command has no effect on a zombie process When a process ends all of the memory and resources associated with it are deallocated so they can be used by other processes Modeling Multiprogramming When multiprogramming is used the CPU utilization can be improved keeps the cpu always utilized PROBABLISTIC MODEL A better model is to look at CPU usage from a probabilistic viewpoint Suppose that a process spends a fraction 9 of its time waiting for 10 to complete With n processes in memory at once the probability that all n processes are waiting for 10 in Which case the CPU Will be idle is pn The CPU utilization is then given by the formula CPU utilization 1 powpn for example that a computer has 8 GB of memory with the operating system and its tables taking up 2 GB and each user program also taking up 2 GB These sizes allow three user programs to be in memory at once With an 80 average IO wait we have a CPU utilization ignoring operating system overhead of 1 powO 83 or about 49 Adding another 8 GB of memory allows the system to go from threeway multiprogramming to sevenway multiprogramming thus raising the CPU utilization to 79 In other words the additional 8 GB will raise the throughput by 30 Adding yet another 8 GB would increase CPU utilization only from 79 to 91 thus raising the throughput by only another 12 Using this model the computer s owner might decide that the first addition was a good investment but that the second was not Figure 2 6 shows the M za n as a m io of in which ia a of mll tipmgrammimg the agree ammoniaail 1m I Bl El Jill El EPLI utlllzailon Iln pamantl I I I I I I I I I I D1 1 E 3 4 5 E T B 9 1i agree not mullipmgrarnmiing FigmrE LE EPU f l with I IIIquot of pmesses DEGREE OF MULTIPROGRAMMING No of process in the memory UNIPROGRAMMING AND MULTIPROGRAMMING Some systems allow execution of only one process at a time eg early personal computers They are called uniprogramming systems Others allow more than one process ie concurrent execution of many processes They are called multiprogramming NOT multiprocessing systems
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'