Unix Process Model • What is a processes? Process Control in Unix • Properties of a process • Processes organization • Interacting with a process Operating Systems Hebrew University Spring 2004 Resources What is a process • Advanced Programming in the Unix • An entry in the kernel’s process table Environment, Stevens [243.51 St 48] • Most common unit of execution • POSIX.1 Spec • Execution state • Machine instructions, data and environment Properties of a process Properties of a Process - cont • Process ID • Controlling terminal • Parent Process ID • Current working directory • Process group ID • Root directory • Session ID • Open files descriptors • User ID of the process • File mode creation mask • Group ID of the process • Resource limits • Effective user ID • Process times • Effective group ID �
Process Trees Who am I? 0 • Only an existing • getpid #include <sys/types.h> process can create a 1 init #include <unistd.h> new process • Returns the PID of the • Parent-Child relations current process pid_t getpid(void); Who is my parent? Talking directly to a process • getppid • Send a signal #include <sys/types.h> #include <sys/types.h> • The ONLY way to talk to #include <signal.h> #include <unistd.h> a process • Returns the PID of the int kill(pid_t pid, int sig) parent of the current pid_t getppid(void); • Lots of signals process • More next week SIGILL – illegal instruction SIGKILL – kill (cannot catch or ignore) SIGTERM - terminate Creating a process Fork • Make a clone of #include <sys/types.h> • Only an existing process can create a new myself #include <unistd.h> process • ONLY difference is pid_t fork(void); • Step 1: Make a clone of yourself PID and PPID! • Very cheap, Copy on Write • Step 2: Have the clone change itself • -1 = failure – Reason via ERRNO �
Fork Example Changing a process #include <unistd.h> if ( (pid = fork()) == 0 ) int execve(const char *filename, { code for child } char *const argv[], else char *const envp[]); { code for parent } • Execute a new program in this space • argv and envp terminated by null pointer • NEVER returns on success, -1 and errno on failure Exec example Better ways #include <unistd.h> extern char **environ; if ((pid = fork()) == 0 ){ exec( arguments ); int execl(const char *path, const char *arg, …); int execlp(const char *file, const char *arg, …); exit(-1); int execle(const char *path, const char *arg, …, char *const } envp[]); // parent continues here int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); Rendezvous wait status returns • Meeting up with your child processes • WIFEXITED(status) • WEXITSTATUS(status) • Resources are freed only when the parent acknowledges the death of the child • WIFSIGNALED(status) #include <sys/types.h> – WIFTERMSIG(status) #include <sys/wait.h> • WIFSTOPPED(status) pid_t wait(int *status); – WSTOPSIG(status) pid_t waitpid(pid_t pid, int *status, int options); �
Leaving a process Leaving a process (quickly) • Leave and tell my #include <stdlib.h> • Just like exit, but…. #include <stdlib.h> parent why I left void exit(int status); • Don’t call the exit void _exit(int status); • NEVER returns handlers • Flush output buffers • Don’t flush output buffers • Close all open streams • Close file descriptors • Call exit handlers When to use _exit Orphans • In the fork branch of a C++ child • A process whose parent has exited. • Fail Fast! • Orphaned processes are inherited by init • Its slot in the process table is immediately released when an orphan terminates. Zombies Daemons • Leaving Home: Disconnecting from my • A process that no longer exists, but still parent so that I can live my own life ties up a slot in the system process table • Somewhat tricky to do correctly • Equivalently: • Examples: – A process that has terminated, but whose – inetd parent exists and has not – atd waited/acknowledged the child's termination – nfsd �
How to make a daemon • Fork -- Create pid-1 • Fork again – Create pid-2 • pid-1 exits, pid-2 is now an orphan • Chdir(“/”) – free current directory • Close all file descriptors (0…MAXINT) �
Recommend
More recommend