Processes and Job Control
Total Page:16
File Type:pdf, Size:1020Kb
Processes and Job Control Jonathan K. Vis Dept. of Human Genetics, Leiden University Medical Center [email protected] Jonathan K. Vis (LUMC) Processes and Job Control 1 / 15 \an instance of a computer program that is being executed" | wikipedia.org Next to the program in execution a process has properties like: • who (user) is running it; • its id, :::; A process can interact: • processes can \talk" to each other; • processes can request resources from the OS. Jonathan K. Vis (LUMC) Processes and Job Control 2 / 15 Using the ps (process status) command Lists the running processes for the current user/terminal: PID TTY TIME CMD 6348 pts/24 00:00:06 evince 7089 pts/24 00:00:00 ps 23323 pts/24 00:00:00 bash 28359 pts/24 00:04:40 evince Jonathan K. Vis (LUMC) Processes and Job Control 3 / 15 To manage the output of ps we can use the -o option. ps -o pid,ppid,tty,uid,args PID PPID TTY UID COMMAND 6348 23323 pts/24 89604 evince processes.pdf 7237 23323 pts/24 89604 ps -o pid,ppid,tty,uid,args 23323 2727 pts/24 89604 bash 28359 23323 pts/24 89604 evince text.pdf For all the output options use: man ps. Jonathan K. Vis (LUMC) Processes and Job Control 4 / 15 What does it all mean? • PID | Every process has an id associated to it. It is an unique identifier, and that is how we can reference a specific process; • PPID | The parent's PID. Every (almost) process has a parent process, the process that was responsible for its creation; • TTY | This is a identifier of the terminal session that triggered this process. That is called the controlling terminal; • UID | This is the user id. It is the identifier for the user thats the owner of this process, and thats what will define the permissions this process will have. • ARGS | The command (followed by its arguments) that is running in this process. Jonathan K. Vis (LUMC) Processes and Job Control 5 / 15 fork() and exec() Every process is created in two stages (system calls): fork() Create a copy of the calling process. The newly created process is called the child, and the caller is the parent. This child process inherits everything that the parent has in memory, it is an almost exact copy (PID and PPID are different, for instance). exec() Replace the current process with a new one. The caller process is gone forever, and the new process takes its place. Jonathan K. Vis (LUMC) Processes and Job Control 6 / 15 Example: ls from bash 1. ls 2. bash creates an exact copy of itself using fork() 3. exec() to replace this copy with the ls process 4. when the ls is finished you are back to the parent process, that is bash. Jonathan K. Vis (LUMC) Processes and Job Control 7 / 15 $ cd nop bash: cd: nop: No such file or directory $ echo ${?} 1 Exit codes Every process exits with an exit code, that is between 0 and 255. Usually 0 means successful termination, i.e., without errors. $ cd $ echo ${?} 0 Jonathan K. Vis (LUMC) Processes and Job Control 8 / 15 Exit codes Every process exits with an exit code, that is between 0 and 255. Usually 0 means successful termination, i.e., without errors. $ cd $ echo ${?} 0 $ cd nop bash: cd: nop: No such file or directory $ echo ${?} 1 Jonathan K. Vis (LUMC) Processes and Job Control 8 / 15 Killing a running process Sometimes it is useful to terminate a running process. Ctrl+C in the controlling terminal (remember: not copy) kill -9 PID where PID is a valid process id kill -KILL PID Jonathan K. Vis (LUMC) Processes and Job Control 9 / 15 See the CPU and memory usage ps -o\%cpu,\%mem,cmd ps -p PID -o\%cpu,\%mem,cmd %CPU %MEM CMD 0.5 1.1 evince processes.pdf 0.0 0.0 ps -o %cpu,%mem,cmd 0.0 0.0 bash Jonathan K. Vis (LUMC) Processes and Job Control 10 / 15 Live monitoring top (table of processes) command Tasks: 297 total, 1 running, 296 sleeping, 0 stopped, 0 zombie %Cpu(s): 0,8 us, 0,4 sy, 0,1 ni, 97,0 id, 1,7 wa, 0,0 hi, 0,0 si, 0,0 st KiB Mem: 8082284 total, 7136656 used, 945628 free, 467828 buffers KiB Swap: 8293372 total, 148028 used, 8145344 free. 2568880 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2308 jkvis 20 0 2160736 408668 58188 S 2,0 5,1 72:12.73 gnome-shell 2727 jkvis 20 0 740416 45448 20436 S 2,0 0,6 8:57.47 gnome-terminal 1142 root 20 0 490452 151152 108720 S 1,3 1,9 74:20.12 Xorg 8992 jkvis 20 0 4669916 1,622g 159472 S 1,3 21,0 1412:26 firefox 16250 jkvis 20 0 2381724 1,040g 1,002g S 0,7 13,5 752:53.78 VBoxHeadless 1751 redis 20 0 37016 5200 1060 S 0,3 0,1 8:48.82 redis-server 2145 jkvis 20 0 391608 48904 4552 S 0,3 0,6 7:19.45 ibus-daemon 24816 jkvis 20 0 1034684 51128 26860 S 0,3 0,6 86:11.84 VirtualBox ... Use < and > to navigate Use u and a username to show processes for that user q quits Jonathan K. Vis (LUMC) Processes and Job Control 11 / 15 time command time du . real 0m8.373s user 0m0.256s sys 0m1.744s • real | wall clock time; • user | only actual CPU time used in executing the process; • sys | CPU time spent in system calls within the kernel. Jonathan K. Vis (LUMC) Processes and Job Control 12 / 15 A process in UNIX | that is something doing a particular job | can run either in foreground or background. So far we have been running jobs in foreground: • jobs start per default in foreground; • the job has control over the terminal; • we cannot enter new commands while the job is running; • until the job is finished or interrupted (e.g. kill). Running a job in the background: • use & to start a job in background; • we remain in control of the terminal; • the background job can write to the terminal; • convenient for starting graphical programs: firefox &. Jonathan K. Vis (LUMC) Processes and Job Control 13 / 15 Moving a running job to the background We run: $ yes > /dev/null Press Ctrl+Z [1]+ Stopped yes > /dev/null Move this job to the background: $ bg Now this job is running in the background and we can use the terminal. To return to the job: $ fg Jonathan K. Vis (LUMC) Processes and Job Control 14 / 15 Job list We can get a job list using: $ jobs [1]- Running evince processes.pdf & [2]+ Stopped nano Alternatively, jobs can be found using ps or top. We can use the job number to select which job we want to bring to foreground: $ fg2 We can also terminate a job using its job number: $ kill -9 %2 Jonathan K. Vis (LUMC) Processes and Job Control 15 / 15.