Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT Processes.

Similar presentations


Presentation on theme: "CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT Processes."— Presentation transcript:

1 CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT Processes

2 CIT 140: Introduction to ITSlide #2 Topics 1.What is a process? 2.Process states and multitasking 3.How does the shell execute processes? 4.Foreground and background processes 5.Job control 6.Process tree 7.Process commands

3 CIT 140: Introduction to ITSlide #3 Introduction A process is a program in execution. A process is created every time you run an external command and is removed after the command finishes its execution.

4 CIT 140: Introduction to ITSlide #4 Multitasking Only one process is using a CPU at a time. A process uses the CPU until: 1.It makes an I/O request, or 2.It reaches the end of its time slice, or 3.It is terminated. The technique used to choose the process that gets to use the CPU next is called Process scheduling.

5 CIT 140: Introduction to ITSlide #5 Process State As a process executes, it changes state new: The process is being created. running: Instructions are being executed. waiting: The process is waiting for some event to occur. ready: The process is waiting to be assigned to a CPU. terminated: The process has finished execution. zombie: Process has been terminated and is waiting for its resources to be recovered.

6 CIT 140: Introduction to ITSlide #6 Process States A UNIX process can be in one of many states, typically ready, running, or waiting.

7 CIT 140: Introduction to ITSlide #7 Process Scheduling First come, First serve(FCFS) –Process that enters system first is assigned highest priority. Priority Scheduling –Assign priorities based on accumulated CPU time. –New and I/O bound processes have highest priorities. Round Robin (RR) –A process gets to use the CPU for one time slice, then the CPU is given to another process, the next process in the queue of processes waiting to use the CPU.

8 CIT 140: Introduction to ITSlide #8 Process Scheduling Modern UNIX scheduler uses mixture of strategies. –Priority values constantly recalculcated. –Smallest priority numbers have highest priority. Priority value = Threshold priority + Nice value + (Recent CPU usage/2) –Threshold priority is an integer from 40 or 60. –CPU usage is the number of clock ticks for which the process has used the CPU –Nice value is a positive integer with a default of 20.

9 CIT 140: Introduction to ITSlide #9 Execution of shell Commands A shell command can be external or internal. Internal (built-in) command: is part of the shell: ex: bg, cd, continue, echo, exec External command: separate program or script ex: grep, more, cat, mkdir, rmdir, ls

10 CIT 140: Introduction to ITSlide #10 Execution of shell Commands A UNIX process is created by the fork() system call, which creates an exact copy of the original process. The forking processis known as the parent process. The created (forked) process is called the child process.

11 CIT 140: Introduction to ITSlide #11 Execution of shell Commands To execute an external command, the exec() system call is used after fork. exec() replaces the current process in memory with the specified file and begins running it.

12 CIT 140: Introduction to ITSlide #12 Execution of shell Commands

13 CIT 140: Introduction to ITSlide #13 Execution of Shell Scripts Shell script: a series of shell commands in a file Execution different from binary programs. –Current shell creates a child shell and lets the child shell execute commands in the shell script, one by one. –Child shell creates a child process for every command it executes. –While the child shell is executing commands in the script file, the parent shell waits for the child to terminate, after which it comes out of waiting state and resumes execution. –Only purpose of child shell is to execute commands and eof means no more commands.

14 CIT 140: Introduction to ITSlide #14 Execution of shell Commands

15 CIT 140: Introduction to ITSlide #15 Execution of shell Commands % ps PIDTTY TIME CMD 15672 pts/0 :03 -csh % sh $ echo This is Bourne shell. This is Bourne shell. $ ksh $ echo This is Korn shell. This is Korn shell. $ ps PID TTY TIME CMD 15672 pts/0 0:03 -csh 15772 pts/0 0:00 sh 15773 pts/0 0:00 ksh $ ^D %

16 CIT 140: Introduction to ITSlide #16 Execution of shell Commands (contd.)

17 CIT 140: Introduction to ITSlide #17 Process Attributes Processes have a variety of attributes: 1.User owner 2.Group owner 3.Process name 4.Process ID (PID) 5.Parent process ID (PPID) 6.Process state 7.Length of time running 8.Amount of memory used

18 CIT 140: Introduction to ITSlide #18 Process Status ps [options] (System V version) PurposeReport process status OutputAttributes of process running on the system Commonly used options/features: -aDisplay Information about the processes executing on your terminal except the session header (your login shell) -eDisplay information about all the processes running on the system -fDisplay full list (8 columns) of status report. -lDisplay long list (14 columns) of status report. -u uidlistDisplay information about processes belonging to the users with UIDs in the ‘uidlist’ (UIDs separated by commas)

19 CIT 140: Introduction to ITSlide #19 Examples of ps > ps PID TTY TIME CMD 24621 pts/13 0:00 bash 24740 pts/13 0:00 ps > ps -f UID PID PPID C STIME TTY TIME CMD waldenj 24621 24615 0 14:51:55 pts/13 0:00 -bash waldenj 24745 24621 0 14:55:06 pts/13 0:00 /usr/bin/ps -f > ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 S 1249 24621 24615 0 50 20 ? 331 ? pts/13 0:00 bash 8 O 1249 24746 24621 0 50 20 ? 123 pts/13 0:00 ps > ps -ef|head UID PID PPID C STIME TTY TIME CMD root 0 0 0 Jun 16 ? 0:01 sched root 1 0 0 Jun 16 ? 1:39 /etc/init - root 2 0 0 Jun 16 ? 0:01 pageout root 3 0 1 Jun 16 ? 970:56 fsflush root 332 1 0 Jun 16 ? 0:00 /usr/lib/saf/sac -t 300 root 312 1 0 Jun 16 ? 0:00 /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf root 325 1 0 Jun 16 ? 0:05 /usr/sbin/vold root 50 1 0 Jun 16 ? 0:00 /usr/lib/sysevent/syseventd root 60 1 0 Jun 16 ? 0:01 /usr/lib/picl/picld

20 CIT 140: Introduction to ITSlide #20 Top Top shows process activity in real time. By default, top shows –Summary of CPU/memory usage. –Highest 15 CPU using processes. –Updated every 5 seconds. Top commands h Help mShow top processes by memory usage qQuit

21 CIT 140: Introduction to ITSlide #21 Example top Output load averages: 0.12, 0.13, 0.14 zappa 14:57:00 102 processes: 99 sleeping, 2 stopped, 1 on cpu CPU states: 83.3% idle, 0.6% user, 16.1% kernel, 0.0% iowait, 0.0% swap Memory: 512M real, 253M free, 238M swap in use, 1.1G swap free PID USERNAME THR PR NCE SIZE RES STATE TIME FLTS CPU COMMAND 24793 waldenj 1 49 0 1896K 1152K cpu00 0:00 0 0.34% top 20561 mysql 9 59 0 40.3M 5272K sleep 133:28 0 0.07% mysqld 24621 waldenj 1 59 0 2648K 2056K sleep 0:00 0 0.05% bash 232 root 1 59 0 4416K 1504K sleep 7:14 0 0.02% sendmail 349 root 7 59 0 2872K 1728K sleep 47:20 0 0.00% mibiisa 191 root 13 59 0 3488K 1616K sleep 9:07 0 0.00% syslogd 343 root 1 59 0 13.9M 4784K sleep 7:48 0 0.00% Xsun 361 root 1 59 0 7904K 2896K sleep 2:24 0 0.00% dtgreet 209 root 23 59 0 3696K 2400K sleep 1:47 0 0.00% nscd 1 root 1 59 0 1224K 248K sleep 1:38 0 0.00% init 5069 nobody 1 59 0 20.9M 14.0M sleep 0:56 0 0.00% httpd 5675 glasnova 1 59 0 960K 560K sleep 0:49 0 0.00% generate 5683 glasnova 1 59 0 960K 560K sleep 0:48 0 0.00% generate 348 root 1 59 0 2400K 912K sleep 0:44 0 0.00% fbconsole 13323 nobody 1 59 0 20.9M 12.1M sleep 0:35 0 0.00% httpd

22 CIT 140: Introduction to ITSlide #22 Process and Job control Shell manages your processes for you: –Process creation –Process termination –Suspending processes –Running foreground processes. –Running background processes. –Switching processes to foreground/background.

23 CIT 140: Introduction to ITSlide #23 Foreground and Background Default is to run processes in foreground > find / -name foo –print >foo.out 2>/dev/null > bzip2 foo.out User must wait for foreground process to finish to have shell prompt. Run processes in background with & > find / -name foo –print >foo.out 2>/dev/null & > bzip2 foo.out &

24 CIT 140: Introduction to ITSlide #24 Example of Background Processes > find / -name foo 2>/dev/null & [1] 25299 > bzip2 bash.man & [2] 25304 > ps PID TTY TIME CMD 24621 pts/13 0:00 bash 25299 pts/13 0:03 find 25305 pts/13 0:00 ps > [2]+ Done bzip2 bash.man

25 CIT 140: Introduction to ITSlide #25 Suspending a Process What if you forgot to background a process? –Suspend the process with –Use fg or bg to unsuspend process. Use stop to suspend background processes. –If you don’t have a stop command, use alias stop=“kill –STOP” –Example

26 CIT 140: Introduction to ITSlide #26 Managing Background Processes: fg fg[%jobid] Purpose: Resume execution of the process with job number ‘jobid’ in the foreground or move background processes to the foreground. Commonly used values for ‘%jobid’ % or %+Curent job %-Previous job %NJob Number N %NameJob beginning with ‘Name’ %?NameCommand containing ‘Name’

27 CIT 140: Introduction to ITSlide #27 Managing Background Processes: bg bg[%jobid-list] Purpose: Resume execution of suspended processes/jobs with job numbers in ‘jobid-list’ in the background. Commonly used values for ‘%jobid’: % or %+ Curent job %- Previous job %N Job Number N %Name Job beginning with ‘Name’ %?Name Command containing ‘Name’

28 CIT 140: Introduction to ITSlide #28 Managing Background Processes: jobs jobs [option] [%jobid-list] Purpose: Display the status of the suspended and background processes specified in ‘jobid-list’; with no list, display the status of the current job. Commonly used options/features: -lAlso display PID of jobs > find / -name foo 2>/dev/null & [1] 26629 > bzip2 bigFile & [2] 26630 > jobs [1]- Running find / -name foo 2>/dev/null & [2]+ Running bzip2 bigFile & > fg %2 >

29 CIT 140: Introduction to ITSlide #29 Which job is the current job? The current job is either 1)The most recently stopped job, or 2)If no stopped jobs exist, the most recent background job. The following fg will return you to vim, not sleep. > vim smallFile Ctrl-z [2]+ Stopped vim smallFile > sleep 10000 & [3] 28137 > fg

30 CIT 140: Introduction to ITSlide #30 UNIX Daemons A daemon is a system process running in the background. Most system services are daemons: E-mail Printing Ssh Web

31 CIT 140: Introduction to ITSlide #31 Sequential and Parallel Execution cmd1; cmd2; … ; cmdN Purpose: Execute the ‘cmd1’, ‘cmd2’, ‘cmd3’,…,’cmdN’ commands in sequence. cmd1 & cmd2 & … & cmdN & Purpose: Execute commands ‘cmd1’,cmd2’,…’cmdN’ in parallel as separate processes.

32 CIT 140: Introduction to ITSlide #32 Sequential and Parallel Execution > date; bzip2 -v bigFile; date Sat Nov 5 15:28:19 EST 2005 bigFile: 11.350:1, 0.705 bits/byte, 91.19% saved, 2940850 in, 259096 out. Sat Nov 5 15:28:28 EST 2005 > date & bzip2 -v bigFile & echo Hello, World & uname -a & date [1] 25658 Sat Nov 5 15:30:29 EST 2005 [2] 25659 Hello, World bigFile: [3] 25660 SunOS zappa 5.9 Generic_112233-07 sun4u sparc SUNW,Ultra-250 [4] 25661 Sat Nov 5 15:30:29 EST 2005 [1] Done date [3] Done echo Hello, World [4] Done uname -a > 11.350:1, 0.705 bits/byte, 91.19% saved, 2940850 in, 259096 out. [2]+ Done bzip2 -v bigFile

33 CIT 140: Introduction to ITSlide #33 Combining Sequential and Parallel

34 CIT 140: Introduction to ITSlide #34 Subshells Shell scripts run in a subshell. You can start a subshell without a script –Group commands using ()’s –Use ; to separate sequence of commands. Advantages of subshells –Redirect I/O of a group of commands at once. –Put group of commands in background at once. –Manage group of commands as a single background process.

35 CIT 140: Introduction to ITSlide #35 Subshells > (date; ps -ef; ls -l) | bzip2 -c >subshell.bz2 > bzcat subshell.bz2 |head Sat Nov 5 15:33:15 EST 2005 UID PID PPID C STIME TTY TIME CMD root 0 0 0 Jun 16 ? 0:01 sched root 1 0 0 Jun 16 ? 1:39 /etc/init - root 2 0 0 Jun 16 ? 0:01 pageout root 3 0 1 Jun 16 ? 971:09 fsflush root 332 1 0 Jun 16 ? 0:00 /usr/lib/saf/sac -t 300 root 312 1 0 Jun 16 ? 0:00 /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf root 325 1 0 Jun 16 ? 0:05 /usr/sbin/vold root 50 1 0 Jun 16 ? 0:00 /usr/lib/sysevent/syseventd

36 CIT 140: Introduction to ITSlide #36 Subshells > (date; bzip2 -v bigFile; date) >bzPerf.txt 2>&1 & [1] 25858 > ps PID TTY TIME CMD 24621 pts/13 0:00 bash 25858 pts/13 0:00 bash 25861 pts/13 0:00 ps 25860 pts/13 0:02 bzip2 > fg ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1 ^Z [1]+ Stopped ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1 > bg [1]+ ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1 & > who newell pts/2 Nov 1 13:09 (10.19.39.154) walden pts/13 Nov 5 14:51 (10.19.10.199) [1]+ Done ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1

37 CIT 140: Introduction to ITSlide #37 Terminating a Process Can terminate a foreground process by Can terminate a background process: 1)Find the PID with ps, then use kill PID. 2)Bring process into foreground with fg, then use

38 CIT 140: Introduction to ITSlide #38 Terminating a Process > find / -name foo 2>/dev/null >foo.txt & [4] 28344 > sort bigFile | bzip2 -c >sortFile.bz2 & [5] 28350 > jobs [2]+ Stopped vim smallFile [3] Running sleep 10000 & [4] Running find / -name foo 2>/dev/null >foo.txt & [5]- Running sort bigFile | bzip2 -c >sortFile.bz2 & > kill %5 [5]- Terminated sort bigFile | bzip2 -c >sortFile.bz2 > ps PID TT S TIME COMMAND 28022 pts/4 S 0:00 -bash 28132 pts/4 T 0:00 vim smallFile 28137 pts/4 S 0:00 sleep 10000 28344 pts/4 R 0:05 find / -name foo > kill 28344 > jobs [2]+ Stopped vim smallFile [3] Running sleep 10000 & [4]- Terminated find / -name foo 2>/dev/null >foo.txt > ps PID TT S TIME COMMAND 28022 pts/4 S 0:00 -bash 28132 pts/4 T 0:00 vim smallFile 28137 pts/4 S 0:00 sleep 10000

39 CIT 140: Introduction to ITSlide #39 Job Numbers vs PIDs PIDs –Every process has a PID. –PIDs are global (shared by every user.) Job Numbers –Every job has a job number. –Jobs may contain multiple processes. –Job numbers are local (every user has own %1...)

40 CIT 140: Introduction to ITSlide #40 Changing Process Priorities > nice -10 bzip2 biggerFile & [4] 28713 > /usr/bin/ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 R 1249 28713 28022 21 99 30 ? 1054 pts/4 0:05 bzip2 8 O 1249 28719 28022 0 50 20 ? 123 pts/4 0:00 ps 8 T 1249 28132 28022 0 40 20 ? 1115 pts/4 0:00 vim 8 S 1249 28022 28020 0 50 20 ? 334 ? pts/4 0:01 bash > bzip2 biggerFile & [4] 28643 > renice +10 28643 > /usr/bin/ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 O 1249 28656 28022 0 50 20 ? 123 pts/4 0:00 ps 8 T 1249 28132 28022 0 40 20 ? 1115 pts/4 0:00 vim 8 S 1249 28022 28020 0 50 20 ? 334 ? pts/4 0:01 bash 8 R 1249 28643 28022 68 99 30 ? 1054 pts/4 0:24 bzip2 > renice -10 28643 renice: 28643: Cannot lower nice value.

41 CIT 140: Introduction to ITSlide #41 UNIX Process Hierarchy 1.System starts init process on boot. –Init is PID 1. 2.Init starts system processes –System daemons. –Graphical login: xdm, kdm, gdm –Text login: getty -> login –Network login: sshd 3.Login runs –User shell

42 CIT 140: Introduction to ITSlide #42 Process Hierarchy in UNIX


Download ppt "CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT Processes."

Similar presentations


Ads by Google