![]() |
![]() |
So far, our shell has used the system call to pass on command lines to the default system shell for execution. Since we need to control what open files and file descriptors are passed to these processes (i/o redirection), we need more control over there execution. To do this we need to use the fork and exec system calls. fork creates a new process that is a clone of the existing one by just copying the existing one. The only thing that is different is that the new process has a new process ID and the return from the fork call is different in the two processes. The exec system call reinitializes that process from a designated program; the program changes while the process remains! Make sure you read the notes on fork and exec and understand how and why the forkexec.c example works before continuing. Tutorial Exercises
switch (pid = fork ()) { case -1: syserr("fork"); case 0: // child execvp (args[0], args); syserr("exec"); default: // parent if (!dont_wait) waitpid(pid, &status, WUNTRACED); }
Code should be in 'straight' C using the compiler of your choice (cc or gcc). Always use nice to
execute your test programs at lower priority to ensure they do not inconvenience
other users if they go 'haywire'. e.g.: The descriptions of the system functions above are drawn from
sources that include |
![]() | ![]() | ![]() |
For use only by students and instructors using the supplementary material available with the text book: "Operating Systems - Internals and Design Principles", William Stallings, Prentice Hall, 5th Edition, 2004. Not to be printed out or copied by any other persons or used for any other purpose without written permission of the author(s).
©