Brief Introduction to System Calls and Process Management COMP

15 Slides53.50 KB

Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3 July 21, 2004 March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 1

Overview System Call Interface Process Management with C – fork() – exec() – wait() – exit() March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 2

System Call Interface App Software User-running API syscall - trap Sys Software “You are here” Sys Call (OS) Interface Kernel-running exit() Zombie read(), write(), wait(), sleep() OS Sw-Hw I-face (drivers) Blocked Ready Hardware March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 3

The fork() System Call (1) A process calling fork() spawns a child process. The child is almost an identical clone of the parent: – – – – Program Text (segment .text) Stack (ss) #include sys/types.h #include unistd.h PCB (eg. registers) Data (segment .data) pid t fork(void); March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 4

The fork() System Call (2) The fork() is one of the those system calls, which is called once, but returns twice! After fork() both the parent and the child are executing the same program. Consider a piece of program (see fork pid.c example): On error, fork() returns -1 . pid t pid fork(); PID 28 PID 28 PID 34 fork() printf(“PID: %d\n”, pid); . The parent will print: p1 March 1, 2002 p1 c1 PID: 34 And the child will always print: PID: 0 Serguei A. Mokhov, [email protected] cordia.ca 5

The fork() System Call (3) Remember, after fork() the execution order is not guaranteed. Simple fork() examples: prints out return from fork() fork child.c distinguishes between the parent and the child – fork pid.c – March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 6

The exec()System Call (1) The exec() call replaces a current process’ image with a new one (i.e. loads a new program within current process). The new image is either regular executable binary file or a shell script. There’s no a syscall under the name exec(). By exec() we usually refer to a family of calls: – – – – – – int int int int int int execl(char *path, char *arg, .); execv(char *path, char *argv[]); execle(char *path, char *arg, ., char *envp[]); execve(char *path, char *argv[], char *envp[]); execlp(char *file, char *arg, .); execvp(char *file, char *argv[]); Here's what l, v, e, and p mean: – – – – l means an argument list, v means an argument vector, e means an environment vector, and p means a search path. March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 7

The exec()System Call (2) Upon success, exec() never returns to the caller. If it does return, it means the call failed. Typical reasons are: non-existent file (bad path) or bad permissions. Arguments passed via exec() appear in the argv[] of the main() function. For more info: man 3 exec; Example: exec pid.c PID 28 PID 28 exec() p1 Legend: p1 Old Program New Program March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 8

Environment The e-exec calls use the environment when attempt to invoke a new program. Name Value – – – – – – HOME PATH SHELL USER LOGNAME . set or env - will display current environment, which you can modify with: – the export command in a shell or a shell script (bash); – the setenv for tcsh – the getenv(), setenv(), putenv(), etc. in C March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 9

fork() and exec() Combined Often after doing fork() we want to load a new program into the child. E.g.: a shell. tcsh tcsh PID 28 PID 28 p1 p1 tcsh fork() PID 34 c1 tcsh ls PID 34 PID 34 exec(ls) c1 March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca c1 10

The System wait() Call Forces the parent to suspend execution, i.e. wait for its children or a specific child to die (terminate is more appropriate terminology, but a bit less common). Example: fork exec.c March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 11

The System wait() Call (2) #include sys/types.h #include sys/wait.h pid t wait(int *status); pid t waitpid(pid t pid, int *status, int options); The wait() causes the parent to wait for any child process. The waitpid() waits for the child with specific PID. The status, if not NULL, stores exit information of the child, which can be analyzed by the parent using the W*() macros. The return value is: – PID of the exited process, if no error – (-1) if an error has happened March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 12

The exit() System Call #include stdlib.h void exit(int status); This call gracefully terminates process execution. Gracefully means it does clean up and release of resources, and puts the process into the zombie state. By calling wait(), the parent cleans up all its zombie children. exit() specifies a return value from the program, which a parent process might want to examine as well as status of the dead process. exit() call is another possibility of quick death without cleanup. March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 13

Process Overview INIT PID 1 User-running syscall() - trap Kernel-running exit() ? wait() Blocked March 1, 2002 Zombie Ready Serguei A. Mokhov, [email protected] cordia.ca 14

References man 2 fork man 3 exec man 2 wait man 3 exit March 1, 2002 Serguei A. Mokhov, [email protected] cordia.ca 15

Back to top button