Basic process control system callsProcess model for shellExecute programForkingWait for child processI/O redirection - dupError handlingSpring 2007Basic process control system callsCOMS W4118Henning SchulzrinneProcess model for shellshell(pid 10)foreground(pid 12)background(pid 14)background(pid 15)fork()child child childExecute program#include <unistd.h>int execve(const char *filename, char *const argv [], char *const envp[]); •Example: execve(“./a.out”, argv, envp);•Replaces existing code with program–on success, does not return•process exits instead–on error, returns -1 (see errno)Forking•“fork creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0.”•Returns–on success, PID of child is returned to parent–0 is returned to child–on failure, return -1 (and errno)if ((pid = fork()) == 0) { /* here’s the child */ exec();} else { /* here’s the parent */ wait for child(pid);}Wait for child process•Can either wait for process (synchronous)•or poll (WNOHANG)–use with SIGCHLD signal handler•while ((c_pid = waitpid(…)) > 0) { … }•Returns process ID of process that exited#include <sys/types.h> #include <sys/wait.h>pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);I/O redirection - dup•dup and dup2 create copies of a file descriptor–both are the same•dup2 makes newfd a copy of oldfd, closing newfd first–I.e., operations on newfd are done on oldfd instead–Send output to outfd instead of standard outputdup2(outfd, STDOUT_FILENO);•For shell, redirect I/O in child, then exec()#include <unistd.h>int dup(int oldfd);int dup2(int oldfd, int newfd);Error handling•errno - integer•perror() prints string followed by last error message#include <stdio.h>void perror(const char
View Full Document