Download 3. Processes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

DNIX wikipedia , lookup

Burroughs MCP wikipedia , lookup

Unix security wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Process Management
Process Concept
Why only the global variables?
Why only the descriptors?
A process is a program in execution.
 a program is a passive entity
 process is an active entity
Process consists of:






Program binary code
Data Section (global variables)
Process Stack
o function parameters
o return addresses
o local variables
Registers content
Open I/O Files
o Descriptors (not the content)
Current activity information (program counter,
status, signals)
Above information forms the Process Context
• The local variables are kept in the stack.
• The files could be large and not fit into the memory or any buffer. So only the file’s
current status is kept (pointer to the file’s block, file handle, buffer’s address, …
Many processes in the same RAM
CPU
First
Process
One program
becomes to
two processes
Second
Process
editor.exe
Several processes share the computer
simultaneously. Reasons:
o BetterSpace
utilization
• Address
- A group of
o Convenience
memory addresses used by
something (process, module,…)
• How many different address
spaces we see in the RAM?
PIDs Process Tree
 Most operating systems identify processes according to a unique
process identifier (or pid).
 The pid can be used as an index to access various attributes of a
process within the kernel.
 During the course of execution, a process may create several new
processes.
 The creating process is called a parent process
 the new processes are called the children of that process.
 Each of these new processes may in turn create
other processes, forming a tree of processes.
 The init process (which always
has a pid of 1) serves as the root
parent process for all user
processes.
Parent
C h i l d r e n
Parent process
C h i l d r e n
Process Table
 To keep track of the state of all the processes, the operating
system maintains a table known as the process table.
 Inside this table, every process is listed along with the resources
the process is using and the current state of the process.
ps –ef
UID
PID
PPID
C
UID
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
stud1
stud2
stud2
stud2
root
studnt
root
root
or
Process Status (Linux)
ps –ef | less
- The Effective User Identifier of process.
- The process identification number
- PID of the parent process.
- CPU usage (in percents).
PID
1
2
3
4
5
6
7
8
9
13
592
597
686
704
711
713
714
715
716
719
805
1189
1197
1277
1319
1321
2384
2385
2422
2423
PPID
0
1
1
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
711
1
1
1189
713
1277
1319
805
2384
2385
2422
C
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
STIME
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
10:00
10:00
10:00
10:00
TTY
?
?
?
?
?
?
?
?
?
?
?
?
?
?
tty1
tty3
tty4
tty5
tty6
tty1
?
tty2
tty2
tty3
tty3
pts/4
?
pts/0
pts/0
pts/0
TIME
00:00:04
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:04
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:02
00:00:00
00:00:00
00:00:00
STIME - The start time of task
TTY
- The terminal on which
the process originated.
TIME - Cumulative CPU time the
process used, in minutes
and seconds (usually in
HH.MM.SS format)
CMD - The name of the
command being executed
CMD
init [3]
[keventd]
[kapm-idled]
[ksoftirqd_CPU0]
[kswapd]
[kreclaimd]
[bdflush]
[kupdated]
[mdrecoveryd]
[kjournald]
syslogd -m 0
klogd -2
xinetd -stayalive -reuse crond
login -- root
login -- stud2
/sbin/mingetty tty4
/sbin/mingetty tty5
/sbin/mingetty tty6
-bash
sshd
login -- stud1
-bash
-bash
/usr/bin/mc -P
bash -rcfile .bashrc
sshd
-bash
su
bash
ps –ef
UID
PID
PPID
C
UID
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
stud1
stud2
stud2
stud2
root
studnt
root
root
Process Status (Linux)
or ps –ef | less
- The Effective User Identifier of process.
- The process identification number
- PID of the parent process.
- CPU usage (in percents).
PID
1
2
3
4
5
6
7
8
9
13
592
597
686
704
711
713
714
715
716
719
805
1189
1197
1277
1319
1321
2384
2385
2422
2423
PPID
0
1
1
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
711
1
1
1189
713
1277
1319
805
2384
2385
2422
C
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
STIME
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
10:00
10:00
10:00
10:00
TTY
?
?
?
?
?
?
?
?
?
?
?
?
?
?
tty1
tty3
tty4
tty5
tty6
tty1
?
tty2
tty2
tty3
tty3
pts/4
?
pts/0
pts/0
pts/0
TIME
00:00:04
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:04
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
00:00:02
00:00:00
00:00:00
00:00:00
STIME - The start time of task
TTY
- The terminal on which
the process originated.
TIME - Cumulative CPU time the
process used, in minutes
and seconds (usually in
HH.MM.SS format)
CMD - The name of the
command being executed
CMD
init [3]
[keventd]
[kapm-idled]
[ksoftirqd_CPU0]
[kswapd]
[kreclaimd]
[bdflush]
[kupdated]
[mdrecoveryd]
[kjournald]
syslogd -m 0
klogd -2
xinetd -stayalive -reuse crond
login -- root
login -- stud2
/sbin/mingetty tty4
/sbin/mingetty tty5
/sbin/mingetty tty6
-bash
sshd
login -- stud1
-bash
-bash
/usr/bin/mc -P
bash -rcfile .bashrc
sshd
-bash
su
bash
Looking at the Process Tree
1. Login as studnt on the 1st virtual terminal.
2. Run the mc command
mc
3. Call some manual page inside MC.
man ls
4. Run pstree tool to look at the process tree.
pstree
[studnt@linuxhost studnt]$ pstree
init-+-crond
|-kapm-idled
|-keventd
|-kjournald
|-klogd
|-login---bash---su---bash
|-2*[login---bash]
|-mdrecoveryd
|-3*[mingetty]
|-sshd---sshd-+-bash---mc---bash---man---sh---sh-+-less
|
|
`-nroff---groff---grotty
|
`-bash---pstree
`-syslogd
Another Look at the Process Tree
4. Another form of process tree.
ps -ejH
5
4
1
2
3
9
13
458
463
574
26970
26971
27081
27171
27208
27209
27454
594
1
1
0
1
1
1
1
458
463
574
574
26971
27081
27171
27208
27209
27454
594
1
1
0
1
1
1
1
458
463
574
574
26971
27081
27171
27171
27171
27171
594
?
?
?
?
?
?
?
?
?
?
?
pts/0
?
pts/1
pts/1
pts/1
pts/1
?
00:00:00 kswapd
00:00:00 ksoftirqd_CPU0
00:00:04 init
00:00:00
keventd
00:00:00
kapm-idled
00:00:00
mdrecoveryd
00:00:02
kjournald
00:00:00
syslogd
00:00:00
klogd
00:00:00
sshd
00:00:01
sshd
00:00:00
bash
00:00:00
sftp-server
00:00:00
bash
00:00:00
su
00:00:00
bash
00:00:00
ps
00:00:00
crond
Process Control Block PCB, TCB
 PCB is the repository of process.
 Everything about the processes in the
system is kept in PCBs
 PCB is a container of the Process Context
 If the OS kernel supports threads then the
system could manage also TCBs.
 The whole PCB collection is called Process
Table.
 PCB contains many pieces of information
associated with a specific process
PCB content






Pointer to the next PCB i.e. pointer to the PCB of the next process to run.
Process state: The state may be new, ready, running, waiting, halted, and so on.
Process number: The identifier of the process (PID).
Program counter: The counter indicates the address of the next instruction to
be executed for this process.
CPU registers: The registers vary in number and type,
depending on the computer architecture. They include:
 Accumulators
 index registers
 stack pointers
 general-purpose registers
 any condition-code information.
Along with the program counter, this state information must be
saved when an interrupt occurs, to allow the process to be
continued correctly afterward.
Pointer to next PCB
PCB content

Memory-management information:
 The memory translation map.






the value of the base and limit registers
the page tables
the segment tables
CPU-scheduling information:



process priority
pointers to scheduling queues
and any other scheduling parameters.
Process accounting information



such as when the process was last run
how much CPU and real time it has accumulated
time limits.
I/O Information
 I/O devices allocated to this process
 list of opened files, etc
Pointer to next PCB
Process Scheduling
• The responsibility of determining how to allocate processor time
among all the ready processes is known as scheduling (CPU time
scheduling).
• The objective of multiprogramming is to have some process running
at all times, so as to maximize CPU utilization.
• The objective of time-sharing is to switch the CPU among processes so
frequently that users can interact with each program while it is
running.
• A uniprocessor system can have only one running process.
• If more processes exist, the rest must wait until the CPU is free
and can be rescheduled.
Process States
Process State Diagram
Choose a task to
run on CPU
Queues
Interrupt the
running task
After I/O request

(Blocked)


New: The process is being created.
Running: Instructions are being executed on CPU.
 process has all the resources it needs for execution
 Only one process can be in the running state at any
given time
http://courses.cs.vt.edu/~csonline/OS/Lessons/Processes/index.html


Waiting: wait for some event to occur
 such as an I/O completion
 or reception of a signal.
Ready: The process is waiting for
permission to use the CPU.
 waiting and ready states are
implemented as queues which hold
the processes in these states
Terminated: The process has finished
execution.
Process States on Unix
ps -aux | more
USER
root
root
root
root
root
root
studnt
studnt
studnt
studnt
studnt
root
%CPU
%MEM
VSZ
RSS
START
STAT
PID %CPU %MEM
1 0.0 0.8
2 0.0 0.0
4 0.0 0.0
9 0.0 0.0
13 0.0 0.0
592 0.0 0.9
2469 0.0 2.1
3606 0.0 1.2
3609 0.0 1.4
3610 0.0 1.5
3614 0.0 1.6
3734 0.0 1.3
VSZ RSS TTY
1412 520 ?
0
0 ?
0
0 ?
0
0 ?
0
0 ?
1472 576 ?
2468 1312 pts/1
1872 788 pts/1
2208 924 pts/1
2216 964 pts/1
2228 1016 pts/1
2788 832 pts/0
STAT
S
SW
SWN
SW<
SW
S
S
S
S
S
S
R
START
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
10:10
21:42
21:42
21:42
21:42
22:02
TIME
0:04
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
COMMAND
init [3]
[keventd]
[ksoftirqd_CPU0]
[mdrecoveryd]
[kjournald]
syslogd -m 0
-bash
man top
sh -c (cd /usr/share/man
sh -c (cd /usr/share/man
/bin/sh /usr/bin/nroff–m
ps -aux
The task's share of the CPU time expressed as a percentage of total CPU time.
The task's share of the physical memory.
The total amount of virtual memory used by the task in kilobytes.
The total amount of physical memory used by the task in kilobytes.
start time of task.
The process state.
Process States on Unix
ps -aux | more
USER
root
root
root
root
root
root
studnt
studnt
studnt
studnt
studnt
root
PID %CPU %MEM
1 0.0 0.8
2 0.0 0.0
4 0.0 0.0
9 0.0 0.0
13 0.0 0.0
592 0.0 0.9
2469 0.0 2.1
3606 0.0 1.2
3609 0.0 1.4
3610 0.0 1.5
3614 0.0 1.6
3734 0.0 1.3
VSZ RSS TTY
1412 520 ?
0
0 ?
0
0 ?
0
0 ?
0
0 ?
1472 576 ?
2468 1312 pts/1
1872 788 pts/1
2208 924 pts/1
2216 964 pts/1
2228 1016 pts/1
2788 832 pts/0
STAT
S
SW
SWN
SW<
SW
S
S
S
S
S
S
R
START
Feb11
Feb11
Feb11
Feb11
Feb11
Feb11
10:10
21:42
21:42
21:42
21:42
22:02
TIME
0:04
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
COMMAND
init [3]
[keventd]
[ksoftirqd_CPU0]
[mdrecoveryd]
[kjournald]
syslogd -m 0
-bash
man top
sh -c (cd /usr/share/man
sh -c (cd /usr/share/man
/bin/sh /usr/bin/nroff–m
ps -aux
PROCESS STATE CODES
D - uninterruptible (unkillable) sleep (usually I/O processes)
– waiting queue
R - runnable - these processes now are working
– ready queue
S - sleeping - these processes are not running but they are active and wait to serve requests
–waiting queue
T - traced or stopped
Z - a defunct ("zombie") process
- this process tried to die, partially is dead but its
name exists in process list
Scheduling Queues
Two types of queues are present: the ready queue and a waiting queue.
Processes Waiting to run on CPU
Processes waiting to write/read on HDD
I/O
•
•
•
The waiting queue could be represented as a
set of device queues.
The list of processes waiting for a particular I/O
device is called a device queue.
Each device has its own device queue.
Another view how the processes are put on different queues
HDD Queue
Flush Disk Queue
Printer Queue
Device Queues
Processes leaving CPU
Once the process is assigned to the CPU and is executing, one of several events could occur:
• The process could issue an I/O request, and then be placed in an I/O queue.
Who invokes the I/O request?
• The process could create a new subprocess and wait for its termination.
Call
Who
invokes
• The process could be removed forcibly from the CPU, as Blocking
a result ofI/O
an
interrupt,
andfork?
be put
Non Blocking
I/O callFork
?
Blocking
back in the ready queue.
Non Blocking Fork ?
Wait Interrupt ?
Where is the child ?
Child
Parent
• New process arrived
• I/O of other process is finished
Concurrent and parallel processes
 Parallelism is when the processes run simultaneously.
 Concurrent processes could run simultaneously or interleaving
What is the main problem of concurrency and how it’s resolved ?
o The hardware is single.
Solution:
o Multiplex the processes in time on the same hardware
o Make illusion of multiple CPUs each of which is dedicated for the
appropriate process.
o All non CPU resources (I/O devices and memory) are shared by virtual
CPUs
I/O Devices
Multiprogramming vs. Multiprocessing
• Parallelism ?
• Concurrency ?
Batch Processing
Multiprogrmming
• Why some tasks take longer to run?
Time Sharing
• Will the process leave CPU before time slice expires?
Context Switch
Switching the CPU to another process requires:
• saving the state of the old process
• and loading the saved state for the new process.
• This task is known as a
context switch.
• PCBs are loaded.
• New translation map
is loaded
• Context-switch time is
pure overhead
• because the system
does no useful work
while switching
Stack push example on x86 assembly
General Purpose
Registers 4 (X)
Index Registers
(Pointers) (P or I)
Base Registers
or Segment (S)
Registers