%include "default.mgp" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %page %center, size 12 Processes %center, size 12 And %center, size 12 Permissions %size 6, fore "red" John McPherson %size 4 Waikato Linux Users Group %page Outline What is a Process Introduction to Signals Job Control from Terminal Process Priority Processes vs. Threads Process Resource Usage Advanced Process Notes: Process details in the %cont, font "typewriter" "/proc" %cont filesystem Parent/Child Relationship File Permissions File Ownership Groups %page Processes What is a Process? Every running program runs code in a process A CPU is executing code from at most 1 process at any instant. A new process is created for every command Every process has a unique ID number Processes run in their own memory space - can't (directly) read or write to another process's memory %page Signals Processes can be sent special signals to indicate various error conditions. Example signals: STOP - pauses execution of the process KILL - makes the process immediately stop executing, without a chance to cleanup XCPU - the system sends this if a limit on CPU time has been set and exceeded PIPE - the process was writing to a pipe and the receiving process has terminated. USR1 - a "spare" signal that a program may use to perform some action (eg re-load a config file) WINCH - the terminal window was resized %page Signals - 2 You can send signals to any process that you own. The default action of most signals is to terminate the process. (Some, like WINCH, are ignored) A program can execute its own functions ("signal handlers") upon receiving a signal. (eg clean up/delete temporary files/reset terminal settings). %cont KILL and STOP cannot be trapped. Useful Commands: %font "typewriter" top %font "typewriter" kill Help: man 1 kill man 7 signal %page Job Control in the Terminal Foreground and Background Only 1 Foreground process gets terminal input Start processes in the background by using "&" Foreground process signals: Stopping (^Z) Killing (^C) Aborting (^\) Useful commands: 'jobs' 'ps' 'fg' and 'bg' the bash manpage has a large section on Job Control %page Terminal "hangups" From the bash manual page: The shell exits by default upon receipt of a SIGHUP. Before exiting, %cont an interactive shell resends the SIGHUP to all jobs, running or %cont stopped. Stopped jobs are sent SIGCONT to ensure that they receive %cont the SIGHUP. Use "disown" or "nohup" if you want a background process to continue running. $ %cont, font "typewriter" nohup %cont command $ command & %cont, font "typewriter" disown %page Process Priority Processes can run at a higher or lower "priority" than other processes. Reasons to do this: long-running processes (eg computations, filtering) running in the background. Interactive processes (such video/audio, GUI applications). Only the root user can increase a process's priority. Commands: %cont, font "typewriter" nice %cont and %cont, font "typewriter" renice interactively via %cont, font "typewriter" top. %page Threads One process can have several threads. Each thread is a separate "kernel-level %cont process", but they all run in the same memory space. For example, a GUI app that does heavy processing can have one thread %cont handling the graphical parts while another thread does the computation, so %cont that the graphics remain responsive to the user. Beware of threads when examining process memory usage. %page Examining Process Resource Usage - top top interactive, shows Process ID, process owner, priority, memory and CPU usage, and command name. Top shows threads rather than processes - each thread of a process is running in the same virtual memory. Useful keys: space = update screen immediately q = quit u = show only processes belonging to one user P = sort by process CPU usage M = sort by memory in use h = get help on all the key shortcuts %page Examining Process Resource Usage - files Processes using a particular file or partition: fuser: fuser fuser -m And other useful options.... %page Advanced Process Notes - /proc filesystem Every process has a dynamically generated directory in the /proc filesystem, %cont where you can get various information on the process. Some of the more useful human-readable files include: /proc/ directory cmdline environ fd/ (open files) status %page Advanced Process Notes - Parent/Child relationship Every process has a Parent process. (Exception: the very first process, %cont called "init"). "ps". Eg: %cont, font "typewriter" ps -o pid,ppid,command pstree nohup'd and disown'ed processes get re-parented to init processes send a "SIGCHLD" signal to their parent when stopped or terminated. %cont (ignored by default). A Zombie is a process that has terminated, but the parent process has neither %cont "disowned" nor "reaped" the child. A zombie has finished executing, so it %cont can't be sent signals. %page Permissions Outline User IDs Groups File Permissions File Ownership %page File Permissions 3 basic permissions: Read Write Execute For a %cont, font "thick" file %cont , Read/Write are explanatory, Execute means the file may be run as a program. For a %cont, font "thick" directory %cont , Read allows listing contents, Write allows creation/overwriting of files, and Execute allows access into the directory (required for Read or Write). %page File Permissions II Read, Write and Execute ("RWX") permission for 3 different sets of users: User (the file owner), Group, Other (users not in the same group) $ ls -l %font "typewriter", size 3 -rwxr-xr-x 1 root root 72460 2003-10-05 12:10 /bin/ls Change permissions with "chmod" User = "u", Group = "g", Other = "o" All = "a" %font "typewriter" $ chmod a+r %font "typewriter" $ chmod a+r,go-wx,u+w %page File Permissions Important Please make sure that your personal files and directories have appropriate %cont permissions! Don't allow other students to copy your work! %font "typewriter" chmod go-rwx Assignment1 %page Groups Seeing what groups you are in: 'groups' or 'id' Changing the group of a file: chgrp group chown :group You must have write access to the file to change ownership. You must belong to the new group to change permissions to that group. Changing your currently "active" group: newgrp %page The End Questions?