Columbia COMS W4118 - Basic process control system calls

Unformatted text preview:

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

Columbia COMS W4118 - Basic process control system calls

Download Basic process control system calls
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Basic process control system calls and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Basic process control system calls 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?