04 The Fork Exec Model


[MUSIC]. Now that we know how, what a process is and how to create processes, let's see how we do interesting things with processes. We're going to be talking about this fork-exec model. And as we saw before, fork creates a copy of the current process, the process that called fork and execve what's going to do, is going to replace the current process code and address space with a code for a different program. okay? In fact there's a whole family of exec calls I encourage you to explore them, 'kay. So for example here, in this, in this code we are calling fork so if the PID is not zero, that's the parents they created a child, okay. And otherwise if the PID is 0, that's the child, okay. And when we coexecve the figure the child is going to replace its codes and its, its data in its address space with the codes and addresses from a different Program. And this line here is going to be printed by the parent only. And we'll see. And, and that's, that's because execv is now going to return. So let's, let's see an animation, of how this happens. Okay? So this, this is the parent process right here. Parent process, okay? So, and, when you say that's, that's, that's the bash process right the shell that you might be probobaly running in your system, okay. When you type alas for example, what its going to do is bash is going to fork because it needs to create a new process to execute the command alas. Okay and here's how its going to happen. So bash calls fork, okay. So that's the parent, that's the original process here and now its going to create a child, that at that point is exactly the same. These two are the same but now what it needs to do, it needs to call exec and replace what was there in the child with the stack, the, the address space, and the codes for the program LS it's going to list a directory. Okay? So that's a high level view, okay? So the bash which is process happens to be the shell when you type LS, it's going to create a new process. And it's going to replace the child process with the code and address space for ls, because ls is, is just a program. Okay? Okay, so now here is how execve works, in more detail. That's the prototype of the function, 'kay, recieves three parameters. 'Kay, one is a filename, it's a filename of the executable containing the program that we want to, to exec. Then the argument that were going to pass to, to, to the program in this MPSD environment that the lift of environemt varibales things like for example the current directory and so on. Now note that execve does not return, why is that? Well, because it's going to be executing a different program now. And the execve call was part of the original program that was it's, it's part of the program was running that, that was running before execve. So as soon as you do execve, that current program is no longer running, that's why it doesn't return. Unless there's an error, then it's going to return. Okay. And as I said before it overrides the code, the data, and the stack, okay. It keeps the, the process ID open files and a few other files. Okay. Now, let's see how this collection of data structures here look like in memory. Okay, so, that's, that's a stack frame for main. Now we have what, we have D argument count, the, vector of arguments. Okay, that's five points to a list of, it points to a vector. And it each intuit is vector is a pointer. To the strings of the command line arguments. Okay. Now the environment NP points to a, another vector. It's a list of pointers to the contents of the environment variables, so it's a pretty beefy data structure. Okay. alright, so this is how exit field works. And now how do we end the process? We're going to use a call called exit. And we use a parameter a status of why the process exits. So, status code zero is normally used for normal exits and non zero is abnormal exit like, you know, there was an exception or there was something, something happened to the process. Okay? So, and if you, if you want to register functions to be executed when you exit the process, you can call at exit, it's something to be executed at exit. For example, say that you call cleanup here. So if you say atexit cleanup, when, when the process here exits, it's going to call this function cleanup here. Okay? So, but once this happens it doesn't really completely destroy the process. It creates what we call a zombie, zombie process. Okay, so and the reason we call it a zombie is because when the process terminates. It still consumes resources. Okay. So things like, many tables in the OS keep track of processes, and so on. We call it zombie because kind of like a living corpse half alive and half dead. But what happens if they build up, well there's a process called reaping that cleans up after a dead process and its performed by the parent process on a terminated child. OK, so when a parent process creates a child process, and the child process exits, dies, the parent is given it's exit status information and with the reaping process tell the kernels to discard. Whatever was left from the process and recycle those, those resources mm-hm. Now what if a parent doesn't reap? Well, that could be a problem if the parent runs for a long time. But if any parent terminates without reaping, then the child will be reaped by init process, by the init process. By the way the init process is a process that is a parent of all processes in your system. 'Kay, when your operating system boots, and is ready to execute something it increases an init processes it's sort of like the canonical process from which all other processes arrive, kay. So if a parent terminates without reaping a child, then the child will be reaped by the init process. Okay, but one thing to note is that if you have a long running process you need to do explicit reaping otherwise nobodies going to reaping and it's going to be using resource and it's eventually it's going to exhaust it's resources. An example of long live processes that need to do reaping are things like shells and servers and so on, okay. Alright, so now that we know what happens when the process dies, let's see how we use that for synchronization. There's this function called "wait" that's used to synchronize with children processes. 'Kay? So, what it does is, when you call wait, it suspends the current process, for example the parent, until one of its children terminates, okay. And the return value is the PID of the child process that terminated because a parent could have multiple child that its waiting to die on. And on successful return the, the child process is then reaped, okay? And by the way, if the child's status is set to, this is a parameter to, to the wait function if it's set to not know, then the, the integer that it points to, because note that's an integer pointer and that it points to will be set to a status indicating why the child process terminated. Okay? So because now we can read the exit status of the process. This int return here just returns the pid. That's the pid. 'Kay. And this points to the status at exit that the child had. 'Kay. So and are there are special macros for this to interpret this status. And you can see wait two[INAUDIBLE] , wait two and, and see what those mean. So, now note that if apparent processes multiple children, if you call wait, it will return the, when any of the children terminates, that's why there's another call called waitpid. And here's what it does, you can wait on a specific child process by passing it's PID as a parameter. Okay? So, let's an example of wait here. So, we have this function called fork wait. It forks here. If it's zero, again it's the child. It says hello from child. Otherwise that's the parent, that's and it's going to do the following, the child pid is going to wait on the, on child status, just an integer here. Okay? And when, when that returns it's because the child has died. So, now when, when that happens we're going to print the pid of the child, note that this returned by weight. Okay, and it's going to say it has terminated, okay, and then it's goign to say bye. So let's see how this works here. There you have it, originally the process was running, then it forks, okay, then eventually the child is going to say bye. Whenever the says bye its because he has returned, therefore we come back here, and then the parent calls bye. Pretty interesting, right? Super simple and effective, you can imagine very complicated trees and what, arrangements of how, of how, parents and childs, synchronize. So to, to summarize, recall that fork gets two copies of the same process. Okay? But it, but fork returns different values to the two process. In fork is a special function. You call once, but return twice, because that's the point where it replicates the process, okay? So execve has a new process, replaces itself with one, with the one that called it, okay? So it's a two process program, okay? So when you you call fork, so heres an example of a two process you first call fork and if the pid is zero thats a child code otherwise exit the parent code. And that's where you'd use in the child code thats where you call exactly to replace itself with a new program. Okay, so now at that point you have two completely two completely different programs. 'Kay? Great. So, inwait and waitpid is used to synchronize parent and child execution and whenever you do this execution process, the child the like remains of the child process is collected when it exits, okay. So in the final sumary of this section we are now ending our section processes so that keeping in mind that at any given time the system has a lot of active processes in fact if you type in Linux PS for example minus ZF to, to, to make it, to make it return here. To make it easy to read. When you type that you're going to see the list of all processes running. Okay, that's, that's your prompt here you type ps minus ef you're going to get a list. But if you have a single CPU only one can execute at a time. And the process has the illusion that it has full control of the CPU. That's pretty cool right? It's a really important abstraction Alright, okay. And from time to time the OS has to do the context switch because there's a single CPU, you want to give the illusion that things are running at the same time. So, it keeps bouncing back and forth between, between the running processes. Okay? So, and we do process management with this fork-exec model that you just learned. That concludes the section, see you soon.

Wyszukiwarka

Podobne podstrony:
04 The Fork Exec Model
Angel [Wicked Christmas 04] The Curse (pdf)
04 The LAND
3E D&D Adventure 04 The Candlemaker s Fire
Zelazny, Roger Amber Short Story 04 The Shroudling and the Guisel
Forgotten Realms Rogues 04 The Yellow Silk (v0 9)
De Camp L Sprague Krishna 04 The Hand of Zei (v1 0) (html)
2008 04 The Watcher Monitoring Remote Servers with Ipmi
2014 05 04 THE ESSENTIALS OF A HEALTHY?MILY part 3
[Proulx & Heine] Death and Black Diamonds Meaning, Mortality & the Meaning Maintenance Model
04 How The Heart Approaches What It Yearns
P N Elrod The Vampire Files 04 Art in the Blood (v1 1)
2009 04 Tag Master Public Key Infrastructure with the Dogtag Certificate System
FIDE Trainers Surveys 2013 04 01, Georg Mohr Bobby Fischer and the square d5
2006 04 Get the Spin
William Morrison Galaxy 1953 10 The Model of a Judge
2002 04 Gphoto Make the Most of Your Digital Cameras
2005 04 To the Test

więcej podobnych podstron