Operating System Three easy pieces Remzi H ArpaciDusseau










- Slides: 10
Operating System : Three easy pieces Remzi H. Arpaci-Dusseau Andrea C. Arpaci-Dusseau API of Process <fork, exec> Bojun Seo(bojun@aces. snu. ac. kr) School of Computer Science and Engineering Seoul National University
Outline and Contents § Chapter outline § Practical method to running new process on system § Practical process to running new program on system § Contents § Things that many people already know. . . • Fork • Exec § Things that many people may not know… • Why does this API looks like this? • Examples(Redirection, Pipe) § Summary Operating System : Three easy pieces 2
Things that many people already know § New process creation should be executed on kernel mode of the OS § This makes reliable and safe creation of the new process § User program should call system call in order to create new process § fork and exec is the most important and related process API on Unix system Operating System : Three easy pieces 3
Things that many people already know § fork § Create the same process • The only two differences are PID and return value of fork system call • Caller process is called parent process and the new process is called child process § Since each process has the same PC state, each process execute the instruction just after the fork system call § exec § Change current process to new process § wait § Wait for the child process termination Source: http: //www. bambuhome. com/products/kitchen-fork, retrived 2015/04/20 Operating System : Three easy pieces 4
Things that many people may not know § Why does this API looks like this? § It looks like enough and simple to provide only one executing new program API • In other words, Why not merged fork and exec § Reason: Unix shell § Shell is one kind of program § Most of this program’s role is to execute new program by user’s command Operating System : Three easy pieces 5
Things that many people may not know § Normal execution flow of the shell § Shell create new process by calling fork § Child process runs new program by calling exec § Parent process wait for the child process termination by calling wait § After the termination of the child process, parent process print prompt to get command by user Operating System : Three easy pieces 6
Things that many people may not know § Example(Redirection: Print to the file instead of standard output) § Shell create new process by calling fork § Child process close standard out and open the redirection target file § And runs new program by calling exec § Now the printing result will be written on the file instead of standard output Operating System : Three easy pieces 7
Things that many people may not know § There are two reasons to make redirection work § First, separated API(fork, exec) § Second, managing method of file descriptor on Unix system § Background information of file descriptor § Each process has its own file descriptor § File descriptor is kind of a table that pointing currently opened files § Program prints the result at the first entry of the file descriptor if there’s no specific indication of the printing target § Let’s take a look at the details of working of redirection § Normally, first entry of the table is standard output, but in case of redirection, we close standard output and open new file § It makes the first entry of the table to be a new file § So, the printing result of the child process goes to the file automatically Operating System : Three easy pieces 8
Things that many people may not know § Example(Pipe: parameter of the second program is the printing result of the first program) § $ ls | cat Operating System : Three easy pieces 9
Summary § fork, exec are process relative API on Unix system which can be used in executing new program § They are separated to support lots of features on shell Operating System : Three easy pieces 10