Presentation is loading. Please wait.

Presentation is loading. Please wait.

Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait.

Similar presentations


Presentation on theme: "Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait."— Presentation transcript:

1 Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait

2 Source: T.Y. Wong, Chinese University of Hong Kong Exec System Call Family The exec system call family has the following members. –execl, execlp, execle, execv, execvp, and execve. Naming convention: execxy For x, l – provide a list of program arguments, terminated by NULL; v – provide a vector (array) of program arguments. For y, p – PATH* environment variable will be searched; e – a vector of environment variables* should be supplied. * See: supplementary notes.

3 Source: T.Y. Wong, Chinese University of Hong Kong wait() & waitpid() They are waiting for the child processes of the calling process to change states. –from ready/running to suspended; or –from ready/running to terminated, i.e., zombie. wait() and waitpid() have different usages: –wait() returns when any one of the children terminates. –waitpid() returns when the specified child terminates. –With appropriate arguments, waitpid() returns when the spec ified child is suspended, by signals SIGTSTP (Ctrl + Z) and S IGSTOP. When a child is terminated, the wait() & the waitpid() calls clear the process structure of the terminated child process. See man pages for yourself! Penguin Specific

4 Source: T.Y. Wong, Chinese University of Hong Kong Fork + Exec + Wait We have a simple shell. int main(void) { char input[1024]; while(1) { printf("[shell]$ "); fgets(input, 1024, stdin); input[strlen(input)-1] = '\0'; if(fork() == 0) { execlp(input, input, NULL); fprintf(stderr, "command not found\n"); exit(1); } else wait(NULL); } [shell]$ _ Shell Process Penguin Specific

5 Source: T.Y. Wong, Chinese University of Hong Kong Fork + Exec + Wait fork() creates the child shell process. int main(void) { char input[1024]; while(1) { printf("[shell]$ "); fgets(input, 1024, stdin); input[strlen(input)-1] = '\0'; if(fork() == 0) { execlp(input, input, NULL); fprintf(stderr, "command not found\n"); exit(1); } else wait(NULL); } [shell]$ ls _ Shell Process Shell Process Penguin Specific

6 Source: T.Y. Wong, Chinese University of Hong Kong Fork + Exec + Wait The parent is suspended until the child terminates. int main(void) { char input[1024]; while(1) { printf("[shell]$ "); fgets(input, 1024, stdin); input[strlen(input)-1] = '\0'; if(fork() == 0) { execlp(input, input, NULL); fprintf(stderr, "command not found\n"); exit(1); } else wait(NULL); } [shell]$ ls _ Shell Process Shell Process Suspended Penguin Specific

7 Source: T.Y. Wong, Chinese University of Hong Kong Fork + Exec + Wait The child shell process becomes “ls” process. int main(void) { char input[1024]; while(1) { printf("[shell]$ "); fgets(input, 1024, stdin); input[strlen(input)-1] = '\0'; if(fork() == 0) { execlp(input, input, NULL); fprintf(stderr, "command not found\n"); exit(1); } else wait(NULL); } [shell]$ ls shell shell.c _ Shell Process “ls” Process Suspended Penguin Specific

8 Source: T.Y. Wong, Chinese University of Hong Kong Fork + Exec + Wait Parent returns from wait() and waits from the next in put. int main(void) { char input[1024]; while(1) { printf("[shell]$ "); fgets(input, 1024, stdin); input[strlen(input)-1] = '\0'; if(fork() == 0) { execlp(input, input, NULL); fprintf(stderr, "command not found\n"); exit(1); } else wait(NULL); } [shell]$ ls shell shell.c [shell]$ _ Shell Process Penguin Specific


Download ppt "Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait."

Similar presentations


Ads by Google