1 / 36

itec 400 Unix Processes

itec 400 Unix Processes. George Vaughan Franklin University. Topics. What is a process? Life cycle of a process Process States Monitoring Processes Signals Managing Processes cron Utility. Unix Processes. What is a process? an executing program that has its own address space.

Download Presentation

itec 400 Unix Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. itec 400Unix Processes George Vaughan Franklin University

  2. Topics • What is a process? • Life cycle of a process • Process States • Monitoring Processes • Signals • Managing Processes • cron Utility

  3. Unix Processes • What is a process? • an executing program that has its own address space. • Each process has its own unique ID (called the process id). • Every process (with very few exceptions) are created by a parent process

  4. User Processes • When you log into a Unix/Linux system, a shell process is started on your behalf. • When you execute a command (like ‘ps’), another process is created for that command: >ps PID TTY TIME CMD 12325 pts/1 00:00:00 bash 12367 pts/1 00:00:00 ps

  5. User Processes • Assume you write a one-line script named ‘test01’: sleep 10 #my one-line script • When you run ‘test01’, 2 processes are created: • a new shell process is created which executes the script • a process is created to run sleep. Sleep runs within the context of the new shell - not the parent shell (login shell). • when your script ends, the script shell terminates • this means the parent shell (the login shell) won’t be affected by the script • Example (using 2 telnet sessions): PID TTY TIME CMD 1434 pts/0 00:00:00 bash <= Login shell from telnet #1 12594 pts/2 00:00:00 bash <= Login shell from telnet #2 12663 pts/2 00:00:00 bash <= Script shell from telnet #2 12664 pts/2 00:00:00 sleep <= sleep from script on telnet #2 12665 pts/0 00:00:00 ps <= ps on telnet #1

  6. What is a process? • Book’s definition: “A process is a single executable program that is running in its own address space.” • Note that a command may spawn other processes. • All user processes are descendents of init. • Typical of a single user to have more than one active process.

  7. What is a process? • Almost all system processes are descendents of init. • There are a few processes that are created ‘by hand’ during system startup. • In Solaris hand crafted processes are: • scheduler • init • pager • fsflush

  8. What is a process? • The kernel keeps track of process state information, such as: • address space map • current state of process • priority • process owner • list of open files • etc.

  9. Life cycle of a process • All processes are created from another process (i.e. all processes have a parent, except init). • Parent process creates a copy of itself using fork() - see manpage • In child copy, fork returns 0. • In parent copy, fork returns PID of child • This is how parent and child can tell who is who.

  10. 0001 #include <stdio.h> 0002 #include <sys/types.h> 0003 #include <unistd.h> 0004 0005 main() 0006 { 0007 printf ("PARENT: process ID is: %d, BEFORE fork()\n", getpid()); 0008 pid_t procID = fork(); 0009 if (procID != 0) { /* Am I the PARENT process? */ 0010 printf ("PARENT: process ID is: %d. ", getpid()); 0011 printf ("Sleep 30 sec...\n"); 0012 sleep(30); 0013 printf("PARENT: process just woke up\n"); 0014 } 0015 else { /* I must be the CHILD process */ 0016 printf (" CHILD: process ID is: %d. ", getpid()); 0017 printf ("Sleep 60 sec...\n"); 0018 sleep(60); 0019 printf(" CHILD: process just woke up\n"); 0020 } 0021 printf ("Process: %d has just terminated\n", getpid()); 0022 } Line 8: process forks into 2 processes Line 9: Test to see if we are the parent process Lines 10-13: This code is executed only by parent Lines 16-19: This code is executed only by child Line 21: This line is executed by both the parent and the child. OUTPUT (telnet session 1): >ex1000 PARENT: process ID is: 14028, BEFORE fork() CHILD: process ID is: 14029. Sleep 60 sec... PARENT: process ID is: 14028. Sleep 30 sec... PARENT: process just woke up Process: 14028 has just terminated CHILD: process just woke up Process: 14029 has just terminated OUTPUT (telnet session 2); >ps -u vaughang PID TTY TIME CMD 13295 pts/40 0:00 ksh 6704 pts/6 0:00 ksh 14032 pts/40 0:00 ps 14029 pts/6 0:00 ex1000  Child Process 14028 pts/6 0:00 ex1000  Parent Process 13281 pts/9 0:00 ksh 14001 pts/4 0:00 vi 6687 pts/4 0:00 ksh Example ex1000.c

  11. Life cycle of a process • The child process may then overwrite its own address space with the desired program using exec() - see manpage. • Although child runs new program, it does so within the inherited environment of the parent. • The child process will continue to run until it exits or is terminated. exit() - see man page • When a process ends, it sends a signal to parent process.

  12. 0001 #include <stdio.h> 0002 #include <sys/types.h> 0003 #include <unistd.h> 0004 0005 main() 0006 { 0007 printf ("ex1010 PARENT: process ID is: %d, BEFORE fork()\n", getpid()); 0008 pid_t procID = fork(); 0009 if (procID != 0) { /* Am I the PARENT process? */ 0010 printf ("ex1010 PARENT: process ID is: %d\n", getpid()); 0011 } 0012 else { /* I must be the CHILD process */ 0013 printf ("ex1010 CHILD: process ID is: %d\n", getpid()); 0014 execl("ex1020", (char *) 0); 0015 } 0016 printf ("ex1010 Process: %d has just terminated\n", getpid()); 0017 } ------------------------------------------------------------------ 0001 #include <stdio.h> 0002 #include <unistd.h> 0003 0004 main() { 0005 printf("ex1020 process ID is: %d\n", getpid()); 0006 } ex1010: Line 8: fork() occurs here Line 9: Check to see if I am parent or child Line 8-10: These lines are only executed by parent Line 12: process must be child Lines13-15: These lines are only executes by child Line 14: Child executes “execl()” which will now execute ex1020 ex1020: Line 5: print out that this process is now executing 1020 OUTPUT: >ex1010 ex1010 PARENT: process ID is: 14410, BEFORE fork() ex1010 CHILD: process ID is: 14411 ex1010 PARENT: process ID is: 14410 ex1010 Process: 14410 has just terminated ex1020 process ID is: 14411 How come we don’t see?: ex1010 Process: 14411 has just terminated ex1010.c and ex1020.c

  13. Fork Run-able Running Exiting Process States Ready exit Scheduled Deleted Parent Process Paused Waiting on Resource Resource Available Blocked (Sleeping) See last slide for References

  14. Process States • Fork Event: • A Process makes an exact copy of itself • New process inherits environment from parent • Child process has new PID • Child process has unique address space • The child overwrites address space with image of desired program. See exec().

  15. Process States • Run-able State: • This state represents a queue • All processes that are run-able are on the queue. • Many processes can be in the run-able state. • Run-able means that the process is not waiting for any resources • The process is just waiting its turn to become the “running process”.

  16. Process States • “Running” State • Process is executing in CPU • Only ‘n’ processes can be running on system with ‘n’ CPU's. • Process will remain in this state until: • Process exceeds CPU time-slice • if so, process moves to run-able state • Process needs resource to continue (I/O) • if so, process is moved to blocked state • Process is pre-empted by a higher priority process. • if so, process moves to run-able state.

  17. Process States • Blocked “Sleeping” State: • A process moves to this state when it is waiting for a resource • Many processes maybe in this state (queue). • Maybe waiting on I/O, page fault, etc.

  18. Process States • Exiting State: • A process moves into the ‘Exiting’ state when exit() is invoked. • Parent process is notified and parent acknowledges termination of child (see wait()). • Abnormal situations: • If a parent refuses to acknowledge termination of child process, the child process becomes a zombie process. • If parent process already terminated, grand-parent process adopts child process and acknowledges termination (Unix). • If parent no longer exists, kernel makes child a process of init - init then adopts and acknowledges termination of child process (Linux).

  19. Monitoring Processes • uptime >uptime 11:08pm up 16 day(s), 14:49, 58 users, load average: 3.46, 3.07, 1.77 • fields • current time • elapsed time the system has been up • current number of users • average number of job in the run-able state over the last 1, 5 and 15 minutes

  20. Monitoring Processes • ps - list active processes (Solaris [bsd version]) >/usr/ucb/ps aux | more USER PID %CPU %MEM SZ RSS TT S START TIME COMMAND rhodes06 25983 27.2 36.3 52776 8365008? R 23:00:47 14:16 a.out rhodes06 25980 27.0 33.7 52764 8338936? R 23:00:41 14:27 a.out rhodes06 25982 8.6 0.1 1448 904 ? S 23:00:47 4:47 a.out rhodes06 25981 8.4 0.1 1448 904 ? O 23:00:41 4:51 a.out rhodes06 25979 8.4 0.1 1448 896 ? S 23:00:41 4:32 a.out rhodes06 25984 7.8 0.1 1448 728 ? S 23:00:47 4:55 a.out • Fields: • USER: logname • PID: Process ID • %CPU, %MEM: percentage of cpu and memory used by processes • SZ: Total virtual memory size in pages • RSS: Resident Set Size (size resident in memory) in bytes • TT: Associated TTY line • S: process status (R=runnable, S=sleeping, O=active, Z=zombie, T=stopped) • TIME: cumulative cpu time

  21. Monitoring Processes • top - show top consuming processes (Solaris) >top | more last pid: 29652; load averages: 3.82, 3.73, 3.60 23:38:00 261 processes: 249 sleeping, 2 running, 8 stopped, 2 on cpu Memory: 1024M real, 15M free, 2934M swap in use, 341M swap free PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND 25983 1961 1 10 0 1027M 83M run 18:15 28.89% a.out 25980 1961 1 11 0 1027M 78M cpu/0 19:52 23.51% a.out 25984 1961 1 32 0 1448K 720K sleep 6:06 8.71% a.out 25982 1961 1 32 0 1448K 896K sleep 5:59 8.47% a.out 25979 1961 1 33 0 1448K 888K run 6:16 7.89% a.out • Fields • PID, USERNAME, SIZE, RES, STATE, TIME, CPU, COMMAND: nothing new • THR: # of executing threads in the process • PRI: prioirity • NICE: nice value

  22. Monitoring Processes • pstree - shows parent-child process relationships $ pstreeinit-+-atd |-cannaserver |-cpumemusage_app |-crond |-cserver |-deskguide_apple |-dhcpcd |-gconfd-1 |-gdm---gdm-+-X | `-gnome-session |-gnome-name-serv |-gnome-smproxy |-gnome-terminal-+-2*[bash] | |-bash---pstree | `-gnome-pty-helpe

  23. Signals • Process level interrupt • Can be used for: • inter-process commutation • user at terminal can send signals to kill, interrupt, suspend processes with special key strokes • signals can be sent with kill command • kernel can send signals on abnormal events (division by zero, invalid address, etc.) • More than 30 types of signals…

  24. Signals • Types of signals:

  25. Managing Processes • kill - kill a process: • user can kill process owned by user • root can kill any process • must specify PID • can specify signal level (e.g. kill -9 12345) • signal 15 is the default • see man page for more details…

  26. Managing Processes • Foreground process • process currently associated with keyboard. interactive process • Background process • a process that was kicked off with ‘&’ at end of command line • can have many background processes • to see active background processes, type: jobs • to bring background job to forground, type: fg %n (where n is the background job number - not PID).

  27. 0001 >cat bigJob 0002 while : ; 0003 do 0004 echo bigJob $1 is alive 0005 sleep 5 0006 done 0007 >bigJob John & 0008 [1] 5823 0009 >bigJob John is alive 0010 0011 bigJob John is alive 0012 bigJob Alice & 0013 bigJob John is alive 0014 [2] 5830 0015 bigJob Alice is alive 0016 bigJob John is alive 0017 bigJob Alice is alive 0018 bigJob John is alive 0019 bigJob Alice is alive 0020 jobs 0021 0022 [2] + Running bigJob Alice & 0023 [1] - Running bigJob John & 0024 bigJob John is alive 0025 bigJob Alice is alive 0026 bigJob John is alive 0027 bigJob Alice is alive 0028 kill %1 0029 [1] - Terminated bigJob John & 0030 bigJob Alice is alive 0031 jobs 0032 [2] + Running bigJob Alice & 0033 bigJob Alice is alive 0034 fg %2 0035 bigJob Alice 0036 bigJob Alice is alive 0037 bigJob Alice is alive 0038 ^D Managing Processes

  28. Managing Processes • nohup • “no hang-up” • a mechanism for running a program, even if you want to logout. • Usually used for large programs • Example: nohup bigJob & • Automatically invokes “nice”

  29. Managing Processes • nice • A method of running a command that may be cpu intensive that will be friendly to other processes. • Affects (lowers) process priority. • Can be used with foreground and background jobs • nohup automatically invokes nice.

  30. Managing Processes • at • a method for specifying a command or script to be executed at a specific time. • Used for “one-shot” scheduling. • For repetitive tasks, use crontab. • example: at 4am < myFile

  31. cron Utility • A Unix facility to schedule repeating tasks (scripts) • cron command starts crond deamon. • crond reads crontab file. • A unique crontab file is created on a per user basis.

  32. cron Utility • Format of crontab entry minutes hours day-of-month month weekday command

  33. cron Utility • Create file with entries • Use crontab to install entries • Use crontab -l to check entries 0001: $ cat cronfile 0002: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> ~/trash/output 0003: $ crontab cronfile 0004: $ crontab -l 0005: # DO NOT EDIT THIS FILE - edit the master and reinstall. 0006: # (cronfile installed on Tue Feb 25 21:52:24 2003) 0007: # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $) 0008: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> ~/trash/output

  34. cron Utility • check output for progress • remove crontab entries • check removal with crontab -l 0012: $ cat output 0013: Tue Feb 25 21:55:00 EST 2003 0014: Tue Feb 25 22:00:00 EST 2003 0015: Tue Feb 25 22:05:01 EST 2003 0016: Tue Feb 25 22:10:00 EST 2003 0017: $ crontab -r 0018: $ crontab -l 0019: no crontab for gvaughan

  35. cron Utility • Look at cron log (must be root) 0021: tail /var/log/cron 0022: 0023: Feb 25 22:15:00 localhost CROND[3002]: (mailman) CMD (/usr/bin/python -S /var/mailman/cron/gate_news) 0024: Feb 25 22:15:00 localhost CROND[3003]: (gvaughan) CMD (date >> ~/trash/output) 0025: Feb 25 22:15:41 localhost crontab[3013]: (gvaughan) DELETE (gvaughan) 0026: Feb 25 22:15:46 localhost crontab[3014]: (gvaughan) LIST (gvaughan) 0027: Feb 25 22:16:01 localhost CROND[3020]: (mailman) CMD (/usr/bin/python -S /var/mailman/cron/qrunner)

  36. References • http://rabbit.eng.miami.edu/class/een521/processstates.html • http://jan.netcomp.monash.edu.au/OS/l8_2.html • Essential System Administration, Aeleen Frisch, 2002 • Linux Administration Handbook, Evi Nemeth, et. al., 2002 • The UNIX Programming Environment - Kernighan and Pike, 1984 • The Design of the Unix Operating System - Maurice J. Bach, 1986

More Related