* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Slides for week 3
Survey
Document related concepts
Transcript
Created by Another Process
Reason: modeling concurrent sub-tasks
Fetch large amount data from network and
process them
Two sub-tasks: fetching processing
Process 1: fetch data, put them in buffer
Process 2: get data from buffer, process
Problems?
System calls to create processes: fork()
in UNIX and CreateProcess() in Win
1
fork()
Create an exact clone of the calling process
After the call, two processes: parent (the
calling process) and child (the process created)
PC=100
Same: memory image, environment, opened files,
global variables, etc.
Different: PID, address spaces, PC
Child
PC=300
Parent
Parent
PC=102
OS
OS
before
after
2
A Simple Example of fork()
ret_code = fork();
If (ret_code<0)
handle_error();
else if (ret_code>0)
{
//parent code goes here
} else
{
//child code goes here
}
Process
OS
Before fork()
Parent
Process
ret_code>0
Child
Process
ret_code=0
OS
After fork()
A Simple Shell Using fork()
#define TRUE 1
while (TRUE){
type_prompt();
read_command(cmd, pmts);
}
// repeat forever
// display prompt on screen
// read input from terminal
if (fork()!=0){ // Parent code
waitpid(-1, &status, 0);
// wait for child to exit
}else{ // child code
execve(cmd, pmts, 0);
// execute command
}
4
Other Ways to Create Processes
Typing a command/clicking an icon
Submitting batch jobs to batch systems
Common properties:
A new process is created by a process
creation system call issued by a existing
process.
So how is the first process created?
5
Process Termination
Voluntary termination
Process is terminated by itself.
Normal exit (voluntary): have the job done
Error exit (voluntary): find an error
Involuntary termination
Process is terminated by another process
Error caused by process itself (involuntary)
Killed by operating system
Killed by another process (involuntary)
kill() system call
6
Process Hierarchies
UNIX case
Strong hierarchy
All processes in a tree with root init
Process group: including all the ancestors, siblings
and descendants.
Processes in the same process group can send
signals to each other.
SIGALRM, SIGKILL
Windows case
No process hierarchy
7
States of Process
Running
Ready
Actually using the CPU at that time
Able to run, temporarily stopped to let
another process run.
Blocked
Unable to run until some external event
happens
8
Transitions of States
Running
Process blocks
for input/output
1
Blocked
3
Scheduler
picks another
process
2
Scheduler picks
this process
Input becomes available
Ready
4
9
Keep Track of Processes
Process table: One entry per process
Process management
Memory management
Registers
Program counter
Program status word
Stack pointer
Process state(?)
Priority
Scheduling parameters
Process ID
Parent process
Process group ……
Pointer to text segment
Pointer to data segment
Pointer to stack segment
File
management
Root directory(?)
Working directory
File descriptors(!)
User ID
Group ID
Outline
Processes
Threads
Inter-process communication (IPC)
Classical IPC problems
Scheduling
11
Motivation: Word Processor
Writing a 800 pages book as a very large file
Scenario:
Done: changed page 1
To do: change page 600
After having changed p1, three things to be done
by program
1. Reformat page 1-800
2. Listen to user’s input, e.g.: which page to go?
3. save the updated file
One process? Multiple process?
We need multiple threads.
12
What We Need: Threads
All threads in the
same process share
same resources
Thread 2:
Reformatting pages
Thread 3:
Save the file
Thread 1:
Listen to keyboard
Kernel
13
Why Threads?
Process: grouping resources + execution
Thread: concurrent execution within a
process (sub-tasks)
In some cases, the two should be separated
Two kinds of resources: I/O and CPU resource
Listen to user interaction (I/O, foreground)
Reformatting (CPU, background)
Share the same resources
Address space, files, global variables and their
updates
14
Processes Versus Threads
User space
One process
Three threads in the
process
Threads operate in the
same address space
Kernel
Three processes
One thread/process
Processes operate in
different address spaces
User space
Kernel
15
Sharing Among Threads
No protection between threads
Impossible and unnecessary (why?)
Shared by threads
Address space
Global variables
Open files
Child processes
Pending alarms
Signals & signal handlers
Accounting info
Private to each thread
Program counter
Registers
Stack (why?)
State
16
Thread’s Stack
One frame for each procedure called
but not yet returned from
Procedure’s local variables and return
address
T1 T2 T3
User space
Kernel
17
States of Threads
Running
Ready
Blocked
Running
Blocked
Ready
18
System Calls About Threads
Create new thread: thread_create
Not necessary a system call (why?)
Parameter: procedure for the new thread
to run
Often no hierarchy, but sometimes does
Exit: thread_exit
Wait for another thread to exit:
thread_wait (synchronization)
Voluntarily give up CPU: thread_yield
19
Complications With Threads
Should a child process copy ALL threads
from its parent?
Competition on resources
Nofunction not properly
Yesconflicts
Thread 1 closes a file which thread 2 is
using (synchronization)
Memory allocation (exclusion)
Need careful thought and design
20
Implementations of Threads
In user space
In the kernel
Kernel knows nothing about threads
Kernel knows the existence of threads
Each method has its own advantages
and disadvantages
Hybrid implementation
21
Implement Threads in User Space
Good for OS not supporting threads
Thread table in process User space
Threads
Not involving kernel
Thread management
Scheduling
Customized scheduling
Better scalability
Run-time
system
Kernel
Thread
table
Process table
22
Disadvantages
One thread blocked entire process blocked!
A thread may run forever (thread_yield() )
Threads are wanted where the threads block
often!
Why we need multi-threads?
When threads block?
23
Implement Threads in Kernel
No run-time system
Make kernel calls when creating
or destroying threads.
User space
Thread table in kernel
Blocking calls as system calls
Scheduling in kernel
Considerable greater cost
Threads
Kernel
Threads
Thread table
Process table
24
Pros & Cons of Threads in Kernel
One thread blocked another thread
within the same process can still run
The costs of system calls are substantial
25
User-level Versus Kernel Level
User-level
Kernel-level
performance
Some instructions,
much faster
A full context switch,
change memory map,
invalidate cache
Blocking
A thread blocksthe
process blocks
No problem
Applicationspecific thread
scheduling
Yes
No
26
Hybrid Implementations
Multiplex user-level threads onto kernel
threads
Kernel is aware of only the kernel-level
threads.
User space
Tradeoff
Threads
Kernel
Kernel threads
27
Single-threaded Multi-threaded
Programming in multithreading is
challenging
How to convert single-threaded processes
to multithreaded ones?
Many challenges, very tricky
Global variables
Stack management
Others
28
Global Variables
Compete on global variables, e.g., errno
Thread 1
Thread 2
Access (errno set to 1)
Time
Open (errno set to 2)
errno inspected
29
Stack Management
When a process’s stack overflows
Kernel provides more spaces automatically
Multiple threads multiple stacks
If the kernel is not aware of these
stacks
Kernel does not know if a stack overflows
It cannot grow automatically
30
Summary
Threads: light-weight processes
Differences between processes and
threads
Resource sharing
Concurrent executions
Resources control
Implementation: user or kernel level?
Programming with multi-threading
Challenging
31