Anda di halaman 1dari 28

Process Abstraction and

Management

Fred Kuhns
fredk@cse.wustl.edu
Applied Research Laboratory,
Department of Computer Science and Engineering,
Washington University in St. Louis

Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Recall the Model
execution environment
application
libraries
trap
user
System call interface
kernel System Services
Kernel interface

handlers File Process control protection


Subsystem subsystem
IPC
Exceptions
Interrupt

Synchronization
And deadlock Scheduler
char block Memory
Other
Subsystem Process table Resources
Device drivers

hardware
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 2
The Process Abstraction
• Process: program in execution
– Sequential execution, characterized by trace.

• Explicitly allocate resources


– Memory for data and code
– Files for persistent storage and communication
– Other resources

• Implicit resource allocation


– interleave execution of several processes
– Notion of fairness, deadlines, throughput

• Traditional unit of control for synchronization, sharing,


communication and deadlock control
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 3
Example: Process Representation
System Memory Process P2 State
Hardware State (registers)
init P0
Program counter

Kernel Process Table



P0: HW state; resources
Memory base register
Process P3 …
Process State (logical)
P2: HW state; resources
Open file table
Process P1 …
Pending Requests
PN: HW state; resources


Process P2
Memory mappings

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 4


Simple Two-State Model
Dispatch
Enter Not Exit
Running
(Create) Running (Terminate)
Pause

(a) State transition diagram

Queue
(Running)
(Ready)
Enter Dispatch Exit
Processor
(Create) (Terminate)

Pause

(b) Queuing diagram


Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 5
5 State Model – More realistic
admit dispatch
New Ready Running
pause

event terminate
wait

Blocked
Exit

• New: The process is being created.


• Running: Instructions being executed.
• Blocked (aka waiting): Must wait for some event to occur.
• Ready: Runnable but waiting to be assigned to a processor.
• Exit : The process has finished execution.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 6
3 Process Example
entity

P1

P2

P3

System ...
(dispatch)

time

Running Ready Blocked

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 7


Suspending Processes
• Processor is faster than I/O so all
processes could be waiting for I/O
• Swap these processes to disk to free up
more memory
• Blocked state becomes suspend state
when swapped to disk
• Two new states
– Blocked, suspend
– Ready, suspend

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 8


Suspending a Process

suspend

admit dispatch
New Ready Running
preempt
suspend terminate
admit event wait
activate
suspend
Ready Blocked
Blocked Exit
Suspend event Suspend activate
Suspended Processes
(possibly on backing store)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 9


OS Control Structures

Memory Tables
Process Image
Memory
I/O Tables User data
I/O User program
System stack
File File Tables PCB
Processes
Primary Table
Process 1
Process 2
 … 
Process N
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 10
Process Management Structures
• Process Control Block
– Process state
– Program counter
– CPU registers
– CPU scheduling information
– Memory-management information
– Accounting information
– I/O status information
• Scheduling Queues
– Job queue – set of all processes in the system.
– Ready queue – set of all processes residing in main memory, ready
and waiting to execute.
– Device queues – set of processes waiting for an I/O device.
– Process migration between the various queues.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 11


Ready Queue, I/O Device Queues

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 12


Process Scheduling

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 13


Schedulers
• Long-term scheduler: job scheduler
– selects which processes should be brought into the ready queue.
– invoked infrequently (seconds, minutes)
– controls the degree of multiprogramming

• Medium-term scheduler
– allocates memory for process.
– invoked periodically or as needed.

• Short-term scheduler: CPU scheduler


– selects which process should be executed next and allocates CPU.
– invoked frequently (ms)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 14


CPU Context Switch

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 15


Context Switch
• When CPU switches to another process, the
system must save the state of the old process
and load the saved state for the new process.

• Context-switch time is overhead; the system


does no useful work while switching.

• Time dependent on hardware support.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 16


Process Creation
• Parents create children; results in a tree of processes.

• Resource sharing models:


– Parent and children share all resources.
– Children share subset of parent’s resources.
– Parent and child share no resources.

• Address space models:


– Child duplicate of parent.
– Child has a program loaded into it.

• Execution models:
– Parent and children execute concurrently.
– Parent waits until children terminate.

• Compare UNIX and Windows

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 17


Process Termination
• Process executes last statement (exit).
– Output data from child to parent
– Resources deallocated by operating system.

• Parent may terminate execution of children processes


(abort).
– Child has exceeded allocated resources.
– Task assigned to child is no longer required.
– Parent is exiting.
• Operating system does not allow child to continue
• Cascading termination.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 18


Cooperating Processes
• Independent process cannot affect or be affected by
the execution of another process.

• Cooperating process can affect or be affected by the


execution of another process

• Advantages of process cooperation


– Information sharing
– Computation speed-up
– Modularity
– Convenience

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 19


UNIX Specifics
• The following slides are for your benefit and will not
be covered in class

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 20


The UNIX Process
• Most created by fork or vfork

• well defined hierarchy: one parent and zero or more


child processes. The init process is at the root of this
tree

• different programs may be run during the life of a


process by calling exec

• processes typically terminate by calling exit

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 21


UNIX Process States
fork
User
Running Initial
Idle
system call, return
interrupt fork
swtch()
exit Kernel
Zombie Runnable
Running
swtch()
wait sleep() continue
stop
wakeup
stop

Asleep stop Stopped

continue Stopped wakeup


Asleep

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 22


Process Context
• Address Space
– text, data, stack, shared memory ...
• Control information (u area, proc)
– u area, proc structure, maps
– kernel stack
– Address translation maps
• Credentials
– user and group ids
• Environment variables
– variable=value
– typically stored at bottom of stack
• Hardware context
– program counter
– stack pointer
– processor status word
– memory management registers
– FPU registers
• Machine registers saved in u area's process control block (PCB)
during context switch to another process
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 23
Process Address Space (one approach)

0xffffffff
Kernel stack

Kernel address space

0x7fffffff
stack

Process address space

Data
0x00000000 Text (shared)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 24


Big Picture
kernel memory
proc struct

Kernel stack/u area Kernel stack/u area Kernel stack/u area


User Stack User Stack User Stack

Data Data Data


Text (shared) Text (shared) Text (shared)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 25


U area and Proc structures
• U Area (user space) • Proc structure (kernel)
– PCB - HW context – process ID and group
– pointer to proc – u area pointer
– real/effective ids – process state
– args, return values or errors – queue pointers - scheduler,
to current syscall. sleep, etc.
– Signal info – Priority
– file descriptor table – memory management info
– controlling terminal vnode – flags

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 26


User Credentials
• Every user is assigned a unique user id (uid) and
group id (gid).
• Superuser (root) has uid == 0 and gid == 0
• every process has both a real and effective
pair of Ids.
– effective id => file creation and access,
– real id => sending signals.
• Senders real/effective == receivers real

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 27


User Perspective
• Creating Processes:
– fork()
• create a new process
• copy of parents virtual memory
• parent return value is child’s PID
• child return value is 0
– exec()
• overwrite with new program
• Terminating processes:
– exit() is called
• closes open files
• releases other resources
• saves resource usage statistics and exit status in proc structure
• wakeup parent
• calls swtch
– Process is in zombie state
– parent collects, via wait(), exit status and usage

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 28

Anda mungkin juga menyukai