0% found this document useful (0 votes)
40 views

OS Structure, Processes & Process Management: What Is A Process?

The document discusses processes and process management in operating systems. Some key points: - A process is a program during execution that contains the program code and data in memory, as well as execution state like the program counter and stack pointer. The OS tracks processes using process control blocks. - Creating a new process involves forking the current process to duplicate it, then using exec to overlay the new program code and data. This allows modifying the child process before executing a new program. - The OS must manage resources for processes like memory, registers, open files, and schedule when they run on CPUs. Process state includes running, ready, blocked, and zombie/exited. Context switches efficiently save/restore CPU

Uploaded by

barani_pdy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

OS Structure, Processes & Process Management: What Is A Process?

The document discusses processes and process management in operating systems. Some key points: - A process is a program during execution that contains the program code and data in memory, as well as execution state like the program counter and stack pointer. The OS tracks processes using process control blocks. - Creating a new process involves forking the current process to duplicate it, then using exec to overlay the new program code and data. This allows modifying the child process before executing a new program. - The OS must manage resources for processes like memory, registers, open files, and schedule when they run on CPUs. Process state includes running, ready, blocked, and zombie/exited. Context switches efficiently save/restore CPU

Uploaded by

barani_pdy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

OS Structure,

Processes & Process Management


1
WhatisaProcess?
A process is a program during execution.
Program =static file (image)
Process =executing program =program +execution state.
A process is the basic unit of execution in an operating system
Each process has a number, its process identifier (pid).
Different processes may run different instances of the same
program
E.g., my javac and your javac process both run the J ava compiler
2
At a minimum, process execution requires following resources:
Memory to contain the program code and data
A set of CPU registers to support execution
ProgramtoProcess
We write a program in e.g., J ava.
A compiler turns that program into an instruction list.
The CPU interprets the instruction list (which is more a graph of p ( g p
basic blocks).
voi d X ( i nt b) {
i f ( b == 1) {

i nt mai n( ) {
3
i nt mai n( ) {
i nt a = 2;
X( a) ;
}
ProcessinMemory
Program to process.
What you wrote
What is in memory.
main; a =2
X; b=2
Stack
voi d X ( i nt b) {
i f ( b == 1) {

i nt mai n( ) {
i nt a = 2;
voi d X ( i nt b) {
i f ( b == 1) {
X; b 2
Heap
4
X( a) ;
}

i nt mai n( ) {
i nt a = 2;
X( a) ;
}
Code
What must the OS track for a
process?
ProcessesandProcessManagement
Detailsforrunningaprogram
A program consists of code and data
On running a program, the loader:
reads and interprets the executable file
sets up the processs memory to containthe code &data from sets up the process s memory to contain the code & data from
executable
pushes argc, argv on the stack
sets the CPU registers properly & calls _start()
Program starts running at _start()
_start(args) {
initialize_java();
ret =main(args);
it( t)
5
exit(ret)
}
we say process is now running, and no longer think of program
When main() returns, OS calls exit() which destroys the
process and returns all resources
Keepingtrackofaprocess
A process has code.
OS must track program counter (code location).
A process has a stack.
OS must track stack pointer.
OS stores state of processes computation in
a process control block (PCB).
E.g., each process has an identifier (process
6
g p (p
identifier, or PID)
Data (program instructions, stack & heap)
resides in memory, metadata is in PCB (which
is a kernel data structure in memory)
ProcessLifeCycle
Processes are always either executing, waiting to
execute or blocked waiting for an event to occur
Running Ready
Start Done
7
Blocked
A preemptive scheduler will force a transition from
running to ready. A non-preemptive scheduler waits.
ProcessContexts
Example:Multiprogramming
User Programn User Programn
Program1 Program2 OS
I/O
Device
main{
SystemSoftware SystemSoftware
User Program1 User Program1
User Program2 User Program2 User Program2 User Program2
User Programn User Programn
.
.
.
k: read()
startIO()
a {
main{
read{
}
save
state
save
state
schedule()
8
Operating System Operating System
SystemSoftware SystemSoftware
k+1:
endio{ interrupt
}
}
schedule()
Memory
restore
state
restore
state
save
state
save
state
WhenaprocessiswaitingforI/Owhatisits
schedulingstate?
1. Ready
2. Running
3. Blocked
4. Zombie
5. Exited
9
SchedulingProcesses
OS has PCBs for active processes.
OS puts PCB on an appropriate queue. p pp p q
Ready to run queue.
Blocked for IO queue (Queue per device).
Zombie queue.
Stopping a process and starting another is
called a context switch.
10
100-10,000 per second, so must be fast.
WhyUseProcesses?
Consider a Web server
get network message (URL) from client
fetchURL data fromdisk fetch URL data from disk
compose response
send response
How well does this web server perform?
With i i t ?
11
With many incoming requests?
That access data all over the disk?
WhyUseProcesses?
Consider a Web server
get network message (URL) from client
create child process, send it URL
Child
fetch URL data from disk
compose response
send response
If server has configuration file open for writing
12
g p g
Prevent child from overwriting configuration
How does server know child serviced request?
Need return code from child process
TheGeniusofSeparatingFork/Exec
Life with Cr eat ePr ocess( f i l ename) ;
But I want to close a file in the child.
Cr eat ePr ocess( f i l ename l i st of f i l es) ; Cr eat ePr ocess( f i l ename, l i st of f i l es) ;
And I want to change the childs environment.
Cr eat ePr ocess( f i l ename, CLOSE_FD, new_envp) ;
Etc. (and a very ugly etc.)
fork() =split this process into 2 (new PID)
Returns 0 in child
Returns pid of child in parent
13
Returns pid of child in parent
exec() =overlay this process with new program
(PID does not change)
TheGeniusofSeparatingFork/Exec
Decoupling fork and exec lets you do anything to the
childs process environment without adding it to the
CreateProcess API.
int ppid =getpid(); // Remember parents pid
fork(); // create a child
if(getpid() !=ppid) { // child continues here
// Do anything (unmapmemory, close net connections)
exec(program, argc, argv0, argv1, );
}
fork() creates a child process that inherits:
identical copy of all parents variables & memory
14
de ca copy o a pa e s a abes & e o y
identical copy of all parents CPU registers (except one)
Parent and child execute at the same point after fork() returns:
by convention, for the child, fork() returns 0
by convention, for the parent, fork() returns the process identifier of
the child
fork() return code a convenience, could always use getpid()
ProgramLoading:exec()
The exec() call allows a process to load a different
program and start execution at main (actually _start).
It allows a process to specify the number of
arguments (argc) and the string argument array
(argv).
If the call is successful
it is the same process
b t it diff t !!
15
but it runs a different program !!
Code, stack & heap is overwritten
Sometimes memory mapped files are preserved.
Whatcreatesaprocess?
1 Fork 1. Fork
2. Exec
3. Both
16
GeneralPurposeProcessCreation
In the parent process:
main()

int ppid =getpid(); // Remember parents pid


fork(); // create a child
if(getpid() !=ppid) { // child continues here
exec_status =exec(calc, argc, argv0, argv1, );
printf(Why would I execute?);
}
17
else { // parent continues here
printf(Whos your daddy?);

child_status =wait(pid);
}
Ashellforksandthenexecsacalculator
i nt pi d = f or k( ) ;
i f ( pi d == 0) {
cl ose( . hi st or y) ;
i nt pi d = f or k( ) ;
i f ( pi d == 0) {
cl ose( . hi st or y) ;
i nt pi d = f or k( ) ;
i f ( pi d == 0) {
cl ose( . hi st or y) ;
i nt cal c_mai n( ) {
i nt q = 7;
do_i ni t ( ) ;
i nt pi d = f or k( ) ;
i f ( pi d == 0) {
cl ose( . hi st or y) ;
pid = 127 pid = 128
exec( / bi n/ cal c) ;
} el se {
wai t ( pi d) ;
exec( / bi n/ cal c) ;
} el se {
wai t ( pi d) ;
OS
USER
exec( / bi n/ cal c) ;
} el se {
wai t ( pi d) ;
l n = get _i nput ( ) ;
exec_i n( l n) ;
exec( / bi n/ cal c) ;
} el se {
wai t ( pi d) ;
18
p
open files = .history
last_cpu = 0
p
open files = .history
last_cpu = 0
Process Control
Blocks (PCBs)
pid = 128
open files =
last_cpu = 0
Ashellforksandthenexecsacalculator
main; a =2
Heap
Stack
0xFC0933CA
main; a =2
Heap
Stack
0xFC0933CA
Heap
Stack
0x43178050
pid = 127 pid = 128
i nt shel l _mai n( ) {
i nt a = 2;

Code
Heap
0xFC0933CA
i nt shel l _mai n( ) {
i nt a = 2;

Code
Heap
0xFC0933CA
i nt cal c_mai n( ) {
i nt q = 7;

Code
Heap
0x43178050
OS
USER
19
p
open files = .history
last_cpu = 0
p
open files = .history
last_cpu = 0
pid = 128
open files =
last_cpu = 0
Process Control
Blocks (PCBs)
Atwhatcost,fork()?
Simple implementation of fork():
allocate memory for the child process
copyparents memory and CPU registers to childs copy parents memory and CPU registers to childs
Expensive !!
In 99% of the time, we call exec() after calling fork()
the memory copying during fork() operation is useless
the child process will likely close the open files & connections
overhead is therefore high
vfork()
a systemcall that creates a process withoutcreatingan identical
20
a system call that creates a process without creating an identical
memory image
child process should call exec() almost immediately
Unfortunate example of implementation influence on interface
Current Linux & BSD 4.4 have it for backwards compatibility
Copy-on-write to implement fork avoids need for vfork
OrderlyTermination:exit()
After the program finishes execution, it calls exit()
This system call:
takes the resultof the programas an argument takes the result of the program as an argument
closes all open files, connections, etc.
deallocates memory
deallocates most of the OS structures supporting the process
checks if parent is alive:
If so, it holds the result value until parent requests it; in this case,
process does not really die, but it enters the zombie/defunct state
If not, it deallocates all data structures, the process is dead
21
cleans up all waiting zombies
Process termination is the ultimate garbage collection (resource
reclamation).
Thewait()SystemCall
A child program returns a value to the parent, so the parent
must arrange to receive that value
The wait() system call serves this purpose
it puts the parent to sleep waiting for a childs result
when a child calls exit(), the OS unblocks the parent and returns
the value passed by exit() as a result of the wait call (along with the
pid of the child)
if there are no children alive, wait() returns immediately
also, if there are zombies waiting for their parents, wait() returns
22
one of the values immediately (and deallocates the zombie)
ProcessControl
OS must include calls to enable special control of a process:
Priority manipulation: y p
nice(), which specifies base process priority (initial priority)
In UNIX, process priority decays as the process consumes CPU
Debugging support:
ptrace(), allows a process to be put under control of another
process
The other process can set breakpoints, examine registers, etc.
Alarms and time:
23
Alarms and time:
Sleep puts a process on a timer queue waiting for some number of
seconds, supporting an alarm functionality
TyingitAllTogether:TheUnixShell
while(! EOF) {
read input
handle regular expressions
int pid = fork(); // create a child p ()
if(pid == 0) { // child continues here
exec(program, argc, argv0, argv1, );
}
else { // parent continues here

}
24
Translates <CTRL-C> to the kill() system call with SIGKILL
Translates <CTRL-Z> to the kill() system call with SIGSTOP
Allows input-output redirections, pipes, and a lot of other stuff that
we will see later
ADoseofReality:SchedulinginSolaris
25
Close to our scheduling diagram, but more complicated
Anatomy of a Process
Code
Header
Stack
DLLs
mapped segments
Processs
Initialized data
Initialized data
Heap
Stack
address space
PC
Stack Pointer
Process Control
Block
26
Executable File
Code
Initialized data Stack Pointer
Registers
PID
UID
Scheduling Priority
List of open files

Unixfork()example
The execution context for the child process is a copy of the
parents context at the time of the call
fork() returns child PID in parent, and 0 in child
main {
int childPID;
S
1
;
childPID = fork();
if(childPID == 0)
<code for child process>
Code Code
fork()
childPID
0
childPID
0
27
f p
else {
<code for parent process>
wait();
}
S
2
;
}
Data
Stack
Data
Stack
Parent Child
=0 =0
childPID
=xxx
childPID
=xxx

You might also like