Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process API COMP 755.

Similar presentations


Presentation on theme: "Process API COMP 755."— Presentation transcript:

1 Process API COMP 755

2 Questions How to create processes?
What interfaces should the OS present for process creation and control? How should these interfaces be designed to enable ease of use as well as utility?

3 Process creation in UNIX systems
UNIX presents one of the most intriguing ways to create a new process with a pair of system calls: fork() and exec(). A third routine, wait(), can be used by a process wishing to wait for a process it has created to complete.

4 The fork() System Call The fork() system call is used to create a new process. Run p1.c

5 fork function The return value of the fork function is: -1 = Error
0 = This is the process that was just created. number = This process is the parent that just executed the fork function. The number returned is the PID of the child process.

6 Fork Action Parent Program p = fork(); PCB code data p = 567 x = 3
y = 5 stack heap PCB

7 Fork Action Parent Program p = fork(); code data p = 567 x = 3 y = 5
stack heap Child Program p = fork(); code data p = 0 x = 3 y = 5 stack heap parent PCB child PCB

8 The exec() System Call The exec() system call is useful when you want to run a program that is different from the calling program. Recall, that calling fork() is only useful if you want to keep running copies of the same program. If you want to run a different program; exec() will be the correct choice. See code on the next slide for an example.

9 What’s really going on with exec()?
What is really going on with the exec() system call is that it takes as input the command wc and the input file that it executes on. It loads the code (static data: p3.c) from that executable and overwrites its current code segment with it. The heap, stack and other parts of the memory space of the program are re-initialized. Then the OS simply runs that program, passing in any arguments as the argv of the process. Thus, it does not create a new process, rather, it transforms the currently running program into a different running program, namely wc. After the exec() in the child, it is almost as if p3.c never ran. Note: a successful call to exec() never returns.

10 Motivation for API: Why would we build such an odd interface to what should be the simple act of creating a new process? Answer: Well it turns out that separation of fork() and exec() is essential in building a UNIX shell, because it lets the shell run code after the fork() but before the call to exec(); this code can alter the environment of the next program to be executed, and thus enables a variety of interesting features to be readily built. Example: $> wc p3.c > newfile.txt The above output is redirected into the output file (newfile.txt). The way the shell accomplishes this task is quest simple: when the child is created, before calling exec(), the shell closes standard output and opens the file newfile.txt. By doing so, any output from the soon-to-be- running program wc is sent to the file instead of the screen.

11 Homework Modify p4.c so that the output file p4.output is created but also displayed to standard output ( the screen ). This should be done by another instance of exec(). Implement the pipe() command to do the following: $> grep –o else p4.c | wc –l


Download ppt "Process API COMP 755."

Similar presentations


Ads by Google