Operating Systems Lesson 2
Operating Systems Lesson 2
Operating Systems
Lesson 2
Processes!
2
What is a Program?
Program is a file containing:
• executable code (machine instructions)
• data (information manipulated by these
instructions)
that together describe a computation
• Resides on disk
• Obtained via compilation & linking
3
What is a Process?
• An instance of a program
• An abstraction of a computer:
Address Space + Execution Context + Environment
A good abstraction:
• is portable and hides implementation details
• has an intuitive and easy-to-use interface
• can be instantiated many times
• is efficient and reasonably easy to implement
4
Process != Program
Aprogram is passive:
code + data
Aprocess is alive:
code + data + stack + registers + PC…
6
Process Control Block
(PCB)
• location in memory
For each process, the OS has a
• location of executable on disk PCB containing:
• which user is executing this process
• process privilege level
• process identifier (pid)
• process arguments (for identification with ps)
• process status (Ready, waiting, finished, etc.)
• register values
• scheduling information
• PC, SP, eflags/status register
… and more!
Usually lives on the kernel stack
7
Who should be allowed to start a process?
Possibility #1:
Only the kernel may starta process
Possibility #2:
User-level processes may start processes
8
System Call Interface
Skinny! (why?)
Example:
Creating a Process
System Call
Windows: Interface
CreateProcess(…);
UNIX
fork + exec
9
CreateProcess (Simplified)
System Call:
if (!CreateProcess(
NULL, // No module name (use command line)
argv[1],// Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Ptr to PROCESS_INFORMATION structure
)
[Windows] 10
Beginning a Process via CreateProcess
Kernel has to:
• Allocate ProcessID
• Create & initialize PCB in the kernel
• Create and initialize a new address space
• Load the program into the address space
• Copy arguments into memory in address space
• Initialize h/w context to start execution at “start”
• Inform scheduler that new process is ready to run
[Windows] 11
CreateProcess (Simplified)
System Call: fork (actual form)
int pid = fork( void ☺
NULL, // No module name (use command line)
argv[1],// Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi )
)
[UNIX] 12
Beginning a Process via CreateProcess
Kernel has to:
fork()
• Allocate ProcessID
• Create & initialize PCB in the kernel
• Create and initialize a new address space
• Load the program into the address space
• Copy arguments into memory in address space
• Initialize the address space with a copy of the entire
contents of the address space of the parent
• Initialize h/w context to start execution at “start”
• Inherit execution context of parent (e.g., open files)
• Inform scheduler that new process is ready to run
[UNIX] 13
Creating and Managing Processes
Create a child process as a clone of the current
fork process. Returns to both parent and child. Returns
child pid to parent process, 0 to child process.
[UNIX] 14
Fork + Exec
Process 1
Program A
PC child_pid = fork();
if (child_pid==0)
exec(B);
else
wait(child_pid);
child_pid ?
[UNIX] 15
Fork + Exec fork returns
Process 1
Program A twice!
child_pid = fork();
PC if (child_pid==0)
exec(B);
else
wait(child_pid);
child_pid 42
Process 42
Program A
child_pid = fork();
PC if (child_pid==0)
exec(B);
else
wait(child_pid);
child_pid 0
[UNIX] 16
Fork + Exec
Process 1
Program A
child_pid = fork();
if (child_pid==0)
exec(B);
else
PC wait(child_pid); Waits until child exits.
child_pid 42
Process 42
Program A
child_pid = fork();
PC if (child_pid==0)
exec(B);
else
wait(child_pid);
child_pid 0
[UNIX] 17
Fork + Exec
Process 1
Program A
child_pid = fork();
if (child_pid==0)
exec(B);
else
PC wait(child_pid);
child_pid 42
if and else
Process 42 both executed!
Program A
child_pid = fork();
if (child_pid==0)
PC exec(B);
else
wait(child_pid);
child_pid 0
[UNIX] 18
Fork + Exec
Process 1
Program A
child_pid = fork();
if (child_pid==0)
exec(B);
else
PC wait(child_pid);
child_pid 42
Process 42
Program B
PC main() {
...
[UNIX] 19
Fork + Exec
Process 1
Program A
child_pid = fork();
if (child_pid==0)
exec(B);
else
PC wait(child_pid);
child_pid 42
[UNIX] 20
Signals (virtualized interrupt)
Allo applications to behave like operating systems.
w
ID Name Default Action Corresponding Event
Interrupt
2 SIGINT Terminate
(e.g., ctrl-c from keyboard)
Kill program
9 SIGKILL Terminate
(cannot override or ignore)
14 SIGALRM Terminate Timer signal
22
Receiving a Signal
A destination process receives a signal when
it is forced by the kernel to react in some way
to the delivery of the signal.
Emacs
Place concurrent
computations in the Stack 2
same address space! Stack 1
Heap Apache
Data
Insns
Kernel
Physical address space PCBs
Each process’ address space by color
(shown contiguous to look nicer) 0x00000000 29
Process vs. Thread
Process:
• Privilege Level
• Address Space Code,
• Data, Heap Shared
• I/O resources
• One or more Threads:
• Stack
• Registers
• PC, SP
30
Thread Memory Layout
Thread 1 Stack 1
SP
PC Stack 2
Thread 2 Stack 3
SP
PC
Data
Thread 3
SP
PC Insns
Virtual
Address
(Heap subdivided, shared, & not shown.) Space 31
Processes and Threads
Process abstraction combines two concepts
• Concurrency: each process is a sequential
execution stream of instructions
• Protection: Each process has own address
space
Threads decouple concurrency & protection
• A thread represents a sequential execution
stream of instructions.
• Aprocess defines the address space that may
be shared by multiple threads
• Threads must be mutually trusting. Why?
32
Thread: abstraction for concurrency
A single-execution stream of instructions;
represents a separately schedulable task
• OS can run, suspend, resume it at any time
• bound to a process
• execution speed is unspecified, non-zero
Virtualizes the processor
• programs run on machine with an infinite
number of processors (hint: not true!)
33
Why Threads?
Performance: exploiting multiple processors
Do threads make sense on a single core?
Encourages natural program structure
• Expressing logically concurrent tasks
• update screen, fetching data, receive user input
Responsiveness
• splitting commands, spawn threads to do work
in the background
Mask long latency of I/O devices
• do useful work while waiting 34
Some Thread Examples
for (k = 0; k < n; k++) {
a[k] = b[k] × c[k] + d[k] × e[k]
}
Web server:
1. get network message (URL) from client
2. get URL data from disk
3. compose response
4. send response
35
Processes vs. Threads
• Have data/code/heap/stack • Have own stack
• Have at least one thread • 1+ threads live in a process
• Process dies → resources • Thread dies → its stack
reclaimed, its threads die reclaimed
• Interprocess communication • Inter-thread communication
via OS and data copying via memory
• Have own address space, • Have own stack and regs,
isolated from other but no isolation from other
processes’ threads in the same process
• Expensive creation and • Inexpensive creation and
context switch context switch
37
0xFFFFFFFF
#1: Kernel-Level Threads Mail
Stack 2
• Threads share virtual
Stack 1
address space Heap Apache
• Separate PCB (TCB) Data
for each thread Insns
• PCBs have: Kernel
• same: page table base reg. PCBs
Init Finished
Ready Running
Waiting
Init Finished
Ready Running
Waiting
Init Finished
Admitted to
Run Queue
Ready Running
Waiting
Init Finished
Admitted to
Run Queue
dispatch
Ready Running
Waiting
Admitted to
Run Queue
dispatch
Ready Running
Waiting
Admitted to
Run Queue
dispatch
Ready Running
Waiting
Admitted to
Run Queue
dispatch
Ready Running
I/O operation
join(), wait()
Waiting
Admitted to
Run Queue
dispatch
Ready Running
Admitted to
Run Queue
dispatch
Ready Running
Admitted to done,
Run Queue thread_exit()
dispatch
Ready Running
Synchronization
Matters!
50