lecture10-sep19
Download
Report
Transcript lecture10-sep19
Operating Systems
CMPSC 473
Processes (4)
September 19 2008 - Lecture 10
Instructor: Bhuvan Urgaonkar
•
Overview of ProcessHow a process is born
related Topics
– Parent/child relationship
– fork, clone, …
• How it leads its life
– Loaded: Later in the course
– Executed
• CPU scheduling
• Context switching
• Where a process “lives”: Address space
– OS maintains some info. for each process: PCB
– Process = Address Space + PCB
• How processes request services from the OS
– System calls
• How processes communicate
• Some variants of processes: LWPs and threads
• How processes die
Done
Partially done
Kernel Mode Stack
•
Stack
esp
thread_info
structure
PCB
(task_struct)
task
curent
thread_info
Since KM stacks make little use of the
stack, only a few thousand bytes suffice
– An example of “Design for the most
common case”, we’ll see more
– Linux: 8KB , thread_info 52 bytes
Kernel Mode Stack
•
Stack
esp
thread_info
structure
PCB
(task_struct)
task
curent
thread_info
union thread_union {
struct thread_info
thread_info;
unsigned long stack[2048];
};
Since KM stacks make little use of the
stack, only a few thousand bytes suffice
– An example of “Design for the most
common case”, we’ll see more
– Linux: 8KB
•
Why combine KM stack and
thread_info into a union?
Kernel Mode Stack
•
Stack
esp
thread_info
structure
PCB
(task_struct)
task
curent
thread_info
union thread_union {
struct thread_info
thread_info;
unsigned long stack[2048];
};
Since KM stacks make little use of the stack,
only a few thousand bytes suffice
– An example of “Design for the most
common case”, we’ll see more
– Linux: KM Stack 8KB, thread_info 52 bytes
•
Why combine KM stack and thread_info into
a union?
– You might think spatial locality
– The kernel can easily obtain the address of
the thread_info structure of the process
currently running on the CPU from the value
of the esp register
– task field is at offset 0
– Other benefits apply to multi-processors:
makes it easy to efficiently find the current
process on each processor
• Earlier approach: Have an array of
current pointers
Resource Limits
• Kernel imposes limits on the amount of resources a
process may have
– E.g., address space size, open files
• Linux: For each resource, for each process
struct rlimit {
unsigned long rlim_cur;
unsigned long rlim_max;
}
• System calls: getrlimit (), setrlimit () to increase upto
some max allowed by kernel
Relationships among
processes
• Several relatives of P recorded in its PCB
– real_parent
• Process that called fork to create P
• Or init (process 1)
– parent
• Usually real_parent
– Kernel signals this parent process when child exits
• Or process that issues ptrace () to monitor P
• Pop quiz: If you run a background process and exit the shell, who is the
parent of the process?
– children
– siblings
• Why maintain these?
Process Switch
• Suspend the current process and resume a
previously suspended process
– Also called context switch or task switch
• What does the kernel need to save when suspending
a process?
– Hint: The entire address space is already saved (either in
memory or on swap space). What else would the process
need when it has to be resumed?
Process Switch
• Suspend the current process and resume a
previously suspended process
– Also called context switch or task switch
• What does the kernel need to save when suspending
a process?
– Hint: The entire address space is already saved (either in
memory or on swap space). What else would the process
need when it has to be resumed?
– CPU registers
• This is called the hardware context of the process
• Linux: Part of h/w context saved within PCB, rest on
kernel mode stack (why?)
Process Switch
• So process switch involves
– Saving h/w context of currently running process
– Restoring h/w context of process to resume
• Who decides which process to resume?
Process Switch
• So process switch involves
– Saving h/w context of currently running process
– Restoring h/w context of process to resume
• Who decides which process to resume?
– The CPU scheduler
– Generic code for a process switch
next = schedule (prev);
switch_to (next, prev); ------> GORY! SKIPPING!!
blah blah blah
• Note: next starts executing not blah. How?
• Project 1 will give you a taste of this :)
Creating Processes
• fork ()
• Take 1: Used in older kernels
– Create a copy of the entire address space of the
parent
– Create a new PCB for the new process
– Update parent, children, sibling pointers
– Place the new process in the ready queue
• S.L.O.W.
Creating Processes
• fork ()
• Problems with Take 1
– Child rarely needs to read/modify ALL the
resources inherited from the parent
– Often, it immediately issues an execve() rendering
all the effort that went into copying the address
space useless!
Creating Processes
• fork ()
• What modern kernels do to avoid this waste
of precious CPU time
Something in the way
she moos …
COW!!
Creating Processes: COW
• fork ()
• What modern kernels do to avoid this waste of
precious CPU time
– Use COW!
– Copy-On-Write
– Basic idea: Postpone work
till the last minute
– Sounds familiar?
Think assignments, quizzes, …