--author Giovanni Agosta --title Processes --date today Brief overview of processes and related operation (ALP chapter 3, GaPiL chapter 3 and 9) --newpage agenda --heading Agenda * Process Identification * Viewing Active Processes * Killing a Process * Creating Processes * Process Scheduling * Process Termination * Signals --newpage pid --heading Process Identification * pid: process ID (unsigned 16 bit integer * low pid values: reserved for kernel processes * pid=1: init --beginoutput #include #include /* pid of the current process */ pid_t getpid(void) /* pid of the parent process */ pid_t getppid(void) --endoutput --newpage view --heading Viewing Active Processes From the shell: * ps * pstree ps has many options controlling the format and filtering --newpage kill --heading Killing a Process From the shell: * kill kill sends a termination signal to process From a program --beginoutput #include #include int kill (pid_t pid, int sig); --endoutput --newpage create --heading Creating Processes Using system --beginoutput #include int main () { int return_value; return_value = system ("ls -l /"); return return_value; } --endoutput List contents of the root directory --newpage fork --heading Creating Processes Using fork --beginoutput #include #include pid_t fork(void) --endoutput * fork creates a new process (child) * The new process is a copy of the parent * The new process shares the text segment of the parent * fork returns 0 in the child, child pid in the parent * Failure: usually, when too many processes have been created --newpage fork --heading Creating Processes Using fork and exec --beginoutput #include int execve(const char *filename, char *const argv[], char *const envp[]) --endoutput * execve changes the program executed by the current process * execve returns only on error * Many different error conditions * Several exec family variants (argv as list or vector; use PATH or not; use environ or explicit environment array) --newpage scheduling --heading Process Scheduling From shell: * nice -n * renice * Only root can decrease niceness! From a program: --beginoutput #include int nice(int inc); --endoutput --newpage schedstatus --heading Process Status --boldon Status PS Description --boldoff Runnable R Running or runnable Sleep S Waiting for system response, can be interrupted Uninterrutible D Waiting for system response, cannot be interrupted Sleep Stopped T Stopped with SIGSTOP Zombie Z Terminated, but status is preserved --newpage term --heading Process Termination Termination modes: * exit(status) function call * return from main (equivalent to exit) * _exit system call (called by _exit) * abort() call * unhandled signal received Effects: * File descriptors closed * Termination status is saved * Children are assigned a new parent (init) * SIGCHLD signal sent to parent * Other cleanup operations for session/group --newpage signals --heading Signals * A mechanism for communication between processes * Signal: asynchronous message sent to a process * Many different signals (/usr/include/bits/signum.h) * Signal disposition: * Ignore * Terminate * Terminate and core dump * Use given signal handler function