110 likes | 331 Views
CS311 – Lecture 19 Outline. Project 4 – Write your own shell. More system calls: fork(), wait(), waitpid(), execvp(), dup2(), and pipe(). Note: These lecture notes are not intended replace your notes. This is what I expect your notes to already look like, but with more explanation as needed.
E N D
CS311 – Lecture 19 Outline Project 4 – Write your own shell. More system calls: fork(), wait(), waitpid(), execvp(), dup2(), and pipe(). Note: These lecture notes are not intended replace your notes. This is what I expect your notes to already look like, but with more explanation as needed. CS311 – Operating Systems 1 1
Writing your own shell Detailed explanation of fork() Explanation of waitpid and execvp system calls. Using dup2 to implement input / output redirection. CS311 – Operating Systems 1 2
Duplicate a process: fork() Every process has a unique process ID number (PID). System Call: pid_t fork (void)fork () causes a process to duplicate. The child process is an almost-exact duplicate of the original parent process; it inherits a copy of its parent's code, data, stack, open file descriptors, and signal table. However, the parent and child have different process ID numbers and parent process ID numbers.If fork () succeeds, it returns the PID of the child to the parent process, and returns 0 to the child process. If it fails, it returns -1 to the parent process, and no child is created. CS311 – Operating Systems 1 CS311 – Operating Systems 1 3 3
wait() System call: pid_t wait(int *status)wait() causes a process to suspend until one of its children terminates. A successful call to wait() returns the pid of the child that terminated and places a status code into status.If a process executes a wait() and has no children, wait() returns immediately with -1. CS311 – Operating Systems 1 CS311 – Operating Systems 1 4 4
waitpid() System call: pid_t waitpid(pid_t pid, int *status, int options)waitpid() allows a parent to wait for a particular child.WNOHANG option causes waitpid() to return even if the status of a child is not immediately available.More details: http://publib.boulder.ibm.com/infocenter/tpfhelp/current/index.jsp?topic=/com.ibm.tpf.doc_put.cur/gtpc2/gtpc2mau.htm CS311 – Operating Systems 1 CS311 – Operating Systems 1 5 5
wait() and waitpid() - Interpreting the status Lecture 19 • Use macros to test the child’s return status. • Each takes the status value returned by wait or waitpid. • WIFEXITED: Query status to see if a child process ended normally (TRUE). • WEXITSTATUS: Obtain exit status of a child process -> returns the exit code specified by the child. • WIFSIGNALED: Query status to see if the child process exited because it raised a signal that caused it to exit. • WTERMSIG: Determine which signal caused the child process to exit -> returns the numeric value of the signal CS311 – Operating Systems 1 CS311 – Operating Systems 1 6 6 6
Side note What happens when a process terminates, but its parent does not wait for it? It becomes a zombie in Unix terminology. Zoombies stay in the system until they are waited for. Is the zombie undead forever? No. If a parent terminates without waiting for a child, the child becomes an orphan and is adopted by a special system process. This process is called init and has process ID equal to 1. CS311 – Operating Systems 1 CS311 – Operating Systems 1 7 7
Common signals defined in Unix/Linux Lecture 19 CS311 – Operating Systems 1 8 8
execvp() Library Function: int execvp(const char* path, const char* argv[])execvp() replace the calling process’s code, data, and stack from the executable who pathname is stored in path. It uses the $PATH environment variable to find path. Other system calls in exec family: execl, execle, execlp, execv, and execve CS311 – Operating Systems 1 CS311 – Operating Systems 1 9 9
dup2() System call: int dup2(int oldFd, int newFd)dup2() closes newFd if it is currently active and then points it to the same file as oldFd.It returns the index of the new file descriptor if successful, and -1 otherwise. dup2(fd, 0);File descriptor 0 is changed to point to what fd points to. CS311 – Operating Systems 1 CS311 – Operating Systems 1 10 10
pipe() System call: int pipe(int fd[2])pipe() creates an unnamed pipe and returns two file descriptors: the descriptor associated with the “read” end of the pipe is stored in fd[0], and the descriptor associated with the "write" end of the pipe is stored in fd[1]. int fd[2]; pipe(fd); CS311 – Operating Systems 1 CS311 – Operating Systems 1 11 11