itec 400 unix processes

36
1 itec 400 Unix Processes George Vaughan Franklin University

Upload: marigold-suarez

Post on 04-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: itec 400 Unix Processes

1

itec 400Unix Processes

George Vaughan

Franklin University

Page 2: itec 400 Unix Processes

2

Topics

• What is a process?

• Life cycle of a process

• Process States

• Monitoring Processes

• Signals

• Managing Processes

• cron Utility

Page 3: itec 400 Unix Processes

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

Page 4: itec 400 Unix Processes

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

Page 5: itec 400 Unix Processes

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 #112594 pts/2 00:00:00 bash <= Login shell from telnet #212663 pts/2 00:00:00 bash <= Script shell from telnet #212664 pts/2 00:00:00 sleep <= sleep from script on telnet #212665 pts/0 00:00:00 ps <= ps on telnet #1

Page 6: itec 400 Unix Processes

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.

Page 7: itec 400 Unix Processes

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

Page 8: itec 400 Unix Processes

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.

Page 9: itec 400 Unix Processes

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.

Page 10: itec 400 Unix Processes

10

Example ex1000.c0001 #include <stdio.h>0002 #include <sys/types.h>0003 #include <unistd.h>00040005 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):>ex1000PARENT: 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 upProcess: 14028 has just terminated CHILD: process just woke upProcess: 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

Page 11: itec 400 Unix Processes

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.

Page 12: itec 400 Unix Processes

12

ex1010.c and ex1020.c0001 #include <stdio.h>0002 #include <sys/types.h>0003 #include <unistd.h>00040005 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>00030004 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:>ex1010ex1010 PARENT: process ID is: 14410, BEFORE fork()ex1010 CHILD: process ID is: 14411ex1010 PARENT: process ID is: 14410ex1010 Process: 14410 has just terminatedex1020 process ID is: 14411

How come we don’t see?:ex1010 Process: 14411 has just terminated

Page 13: itec 400 Unix Processes

13

Process States

Fork

Blocked(Sleeping)

Run-able Running

Exiting

ParentProcess

Ready

Scheduled

Paused

Waiting on ResourceResource

Available

exit

Deleted

See last slide for References

Page 14: itec 400 Unix Processes

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().

Page 15: itec 400 Unix Processes

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”.

Page 16: itec 400 Unix Processes

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.

Page 17: itec 400 Unix Processes

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.

Page 18: itec 400 Unix Processes

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).

Page 19: itec 400 Unix Processes

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

Page 20: itec 400 Unix Processes

20

Monitoring Processes• ps - list active processes (Solaris [bsd

version])>/usr/ucb/ps aux | moreUSER PID %CPU %MEM SZ RSS TT S START TIME COMMANDrhodes06 25983 27.2 36.3 52776 8365008? R 23:00:47 14:16 a.outrhodes06 25980 27.0 33.7 52764 8338936? R 23:00:41 14:27 a.outrhodes06 25982 8.6 0.1 1448 904 ? S 23:00:47 4:47 a.outrhodes06 25981 8.4 0.1 1448 904 ? O 23:00:41 4:51 a.outrhodes06 25979 8.4 0.1 1448 896 ? S 23:00:41 4:32 a.outrhodes06 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

Page 21: itec 400 Unix Processes

21

Monitoring Processes• top - show top consuming processes

(Solaris)>top | morelast pid: 29652; load averages: 3.82, 3.73, 3.60 23:38:00261 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

Page 22: itec 400 Unix Processes

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

Page 23: itec 400 Unix Processes

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…

Page 24: itec 400 Unix Processes

24

Signals• Types of signals:# Name Description Default Catch? Block? Core

Dump?Key Strokes

1 HUP Hang Up Terminate Y Y N

2 INT Interrupt Terminate Y Y N

3 QUIT Quit Terminate Y Y Y

9 KILL Kill Terminate N N N

- BUS Bus Error Terminate Y Y Y

11 SEGV Segmentation Fault

Terminate Y Y Y

15 TERM Software Termination

Terminate Y Y N Cntrl-C

- STOP Stop Stop N N N Cntrl-Z

- CONT Continue Ignore Y N N Cntrl-Z

Page 25: itec 400 Unix Processes

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…

Page 26: itec 400 Unix Processes

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).

Page 27: itec 400 Unix Processes

27

Managing Processes0001 >cat bigJob0002 while : ;0003 do0004 echo bigJob $1 is alive0005 sleep 50006 done0007 >bigJob John &0008 [1] 58230009 >bigJob John is alive00100011 bigJob John is alive0012 bigJob Alice &0013 bigJob John is alive0014 [2] 58300015 bigJob Alice is alive0016 bigJob John is alive0017 bigJob Alice is alive0018 bigJob John is alive0019 bigJob Alice is alive

0020 jobs00210022 [2] + Running bigJob Alice &0023 [1] - Running bigJob John &0024 bigJob John is alive0025 bigJob Alice is alive0026 bigJob John is alive0027 bigJob Alice is alive0028 kill %10029 [1] - Terminated bigJob John &0030 bigJob Alice is alive0031 jobs0032 [2] + Running bigJob Alice &0033 bigJob Alice is alive0034 fg %20035 bigJob Alice0036 bigJob Alice is alive0037 bigJob Alice is alive0038 ^D

Page 28: itec 400 Unix 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”

Page 29: itec 400 Unix Processes

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.

Page 30: itec 400 Unix Processes

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

Page 31: itec 400 Unix Processes

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.

Page 32: itec 400 Unix Processes

32

cron Utility

• Format of crontab entryminutes hours day-of-month month weekday command

minutes Minutes past the hour (0-59)

hours Hour of day (0-23)

Day-of-month 1-31

month 1-12

weekday 0-6

command Output mailed if not redirected.

Page 33: itec 400 Unix Processes

33

cron Utility

• Create file with entries• Use crontab to install entries• Use crontab -l to check entries0001: $ cat cronfile0002: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> ~/trash/output0003: $ crontab cronfile0004: $ crontab -l0005: # 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

Page 34: itec 400 Unix Processes

34

cron Utility

• check output for progress• remove crontab entries• check removal with crontab -l0012: $ cat output0013: Tue Feb 25 21:55:00 EST 20030014: Tue Feb 25 22:00:00 EST 20030015: Tue Feb 25 22:05:01 EST 20030016: Tue Feb 25 22:10:00 EST 20030017: $ crontab -r0018: $ crontab -l

0019: no crontab for gvaughan

Page 35: itec 400 Unix Processes

35

cron Utility

• Look at cron log (must be root)

0021: tail /var/log/cron0022: 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)

Page 36: itec 400 Unix Processes

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