Title: System Programming Lecture 3 Processes
1System ProgrammingLecture 3Processes System
Calls
2Review How can we give the illusion of multiple
processors?
- How do we provide the illusion of multiple
processors? - Multiplex in time!
- Each virtual CPU needs a structure to hold
- Program Counter (PC), Stack Pointer (SP)
- Registers (Integer, Floating point, others?)
- How switch from one CPU to the next?
- Save PC, SP, and registers in current state block
- Load PC, SP, and registers from new state block
- What triggers switch?
- Timer, voluntary yield, I/O, other things
3Review How do we multiplex processes?
- The current state of process held in a process
control block (PCB) - This is a snapshot of the execution and
protection environment - Only one PCB active at a time
- Give out CPU time to different processes
(Scheduling) - Only one process running at a time
- Give more time to important processes
- Give pieces of resources to different processes
(Protection) - Controlled access to non-CPU resources
- Sample mechanisms
- Memory Mapping Give each process their own
address space - Kernel/User duality Arbitrary multiplexing of
I/O through system calls
4Review Multiple Processes Collaborate on a Task
Proc 1
Proc 2
Proc 3
- High Creation/memory Overhead
- (Relatively) High Context-Switch Overhead
- Need Communication mechanism
- Separate Address Spaces Isolates Processes
- Shared-Memory Mapping
- Accomplished by mapping addresses to common DRAM
- Read and Write through memory
- Message Passing
- send() and receive() messages
- Works across network
5Shared Memory Communication
Data 2
Stack 1
Heap 1
Code 1
Stack 2
Data 1
Prog 2 Virtual Address Space 2
Prog 1 Virtual Address Space 1
Heap 2
Code 2
Shared
- Communication occurs by simply reading/writing
to shared address page - Really low overhead communication
- Introduces complex synchronization problems
6Goals for Today
- Process scheduling concepts
- Process creation and termination
- Process management in UNIX/Linux system calls
fork, exec, wait, exit - Sample codes
- The wait and exec system calls and sample code
7Schedulers
- Long term scheduler
- Short term scheduler
- Medium term scheduler
8Long Term Scheduler
- Long-term scheduler (or job scheduler) selects
processes from the job pool to be brought into
the ready queue. - Long-term scheduler is invoked very infrequently
(seconds, minutes) ? (may be slow). - The long-term scheduler controls the degree of
multiprogramming. - More processes, smaller percentage of time each
process is executed
9Short Term Scheduler
- Short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates it the CPU through the dispatcher. - Short-term scheduler is invoked very frequently
(milliseconds) ? (must be fast). - Invoked when following events occur
- CPU slice of the current process finishes
- Current process needs to wait for an event
- Clock interrupt
- I/O interrupt
- System call
- Signal
10Medium Term Scheduler
- Also known as swapper
- Selects an in-memory process and swaps it out to
the disk temporarily - Swapping decision is based on several factors
- Arrival of a higher priority process but no
memory available - Poor mix of jobs
- Memory request of a process cannot be met
11Addition of Medium Term Scheduling
12Context Switch
- When CPU switches to another process, the system
must save the state (context) of the current
(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 typically in
microseconds
13Process Creation
- Parent process create children processes, which,
in turn create other processes, forming a tree of
processes. - Resource sharing
- Parent and children share all resources.
- Children share a subset of parents resources.
- Parent and child share no resources.
- Execution
- Parent and children execute concurrently.
- Parent waits until children terminate.
14Process Creation.
- Address space
- Child duplicate of parent.
- Child has a program loaded onto it.
- UNIX examples
- fork system call creates a new process
- exec system call used after a fork to replace the
process memory image with a new executable.
15Processes Tree on a UNIX System
16Process Termination
- Process executes the last statement and requests
the operating system to terminate it (exit). - Output data from child to parent (via wait).
- Process resources are deallocated by the
operating system, to be recycled later. - Parent may terminate execution of children
processes (abort). - Child has exceeded allocated resources (main
memory, execution time, etc.). - Parent needs to create another child but has
reached its maximum children limit - Task performed by the child is no longer
required. - Parent exits.
- Operating system does not allow child to continue
if its parent terminates. - Cascaded termination
17Break
18System Calls
- User processes must not be given open access to
the kernel code - The system call interface layer contains entry
point in the kernel code - Any user or application request that involves
access to any system resource must be handled by
the kernel code
19Types Of System Calls
- Process Control
- File Management
- Device Management
- Information maintenance
- Communications
20System Call Execution
- The user program makes a call to a library
function. - Library routine puts appropriate parameters at a
well-known place (registers, stack, or a table in
memory). - The trap instruction is executed to change mode
from user to kernel. - Control goes to operating system.
- Operating system determines which system call is
to be carried out.
21Semantics of System Call Execution
- Kernel indexes the dispatch table, which contains
pointers to service routines for system calls. - Service routine is executed and return parameter
or error code placed at well-known places
(usually a CPU register). - Control given back to user program.
- Library function executes the instruction
following trap.
22System Call
23Process Management in UNIX/Linux
- Important process-related UNIX/Linux system
calls - fork
- wait
- exec
- exit
24fork()
- When the fork system call is executed, a new
process is created which consists of a copy of
the address space of the parent. - This mechanism allows the
parent process to communicate
easily with the child process.
SYNOPSIS include ltsys/types.hgt include ltunistd.hgt pid_t fork(void)
25fork() ...
- The return code for fork is zero for the child
process and the process identifier of child is
returned to the parent process. - On success, both processes continue execution at
the instruction after the fork call. - On failure, -1 is returned to the parent process
and error is set appropriately to indicate the
reason of failure no child is created
26fork()Sample Code
main() int pid ... pid fork()
if (pid 0) / Code for child /
... else / Code for parent /
... ...
27fork()Inherits from the Parent
- The child process inherits the following
attributes from the parent - Environment
- Open file descriptor table
- Signal handling settings
- Current working directory
- Root directory
- File mode creation mask
- Etc.
28fork()Child Differs from the Parent
- The child process differs from the parent
process - Different process ID (PID)
- Different parent process ID (PPID)
- Child has its own copy of parents file
descriptors - Etc.
29fork()Reasons for Failure
- Maximum number of processes allowed to execute
under one user has exceeded - Maximum number of processes allowed on the system
has exceeded - Not enough swap space
30wait()
- The wait system call suspends the calling process
until one of its immediate children terminates,
or until a child that is being traced stops
because it has hit an event of interest. - wait returns prematurely if a signal is received.
If all children processes stopped or terminated
prior to the call on wait, return is immediate.
31Synopsis of wait()
include ltsys/types.hgt include ltsys/wait.hgt pid_t wait(int stat_loc) ltsys/types.hgt /usr/include/sys/types.h
32wait() ...
- If the call is successful, the process ID of the
terminating child is returned. - If parent terminates all its children have
assigned as their new parent, the init process.
Thus the children still have a parent to collect
their status and execution statistics.
33wait() ...
- Zombie processa process that has terminated but
whose exit status has not yet been received by
its parent process or by init.
34Break
- Assignment 3
- Question Explain 50 different system calls of
Linux operating system.
35Sample Codefork
include ltstdio.hgt void main() int pid, status pid fork() if(pid -1) printf(fork failed\n) exit(1)
36Sample Codefork
if(pid 0) / Child / printf(Child here!\n) exit(0) else / Parent / wait(status) printf(Well done kid!\n) exit(0)
37Semantics of fork
38exec()
- Typically the exec system call is used after a
fork system call by one of the two processes to
replace the process memory space with a new
executable program. - The new process image is constructed from an
ordinary, executable file.
39exec()
- There can be no return from a successful exec
because the calling process image is overlaid by
the new process image
40Synopsis of exec()
include ltunistd.hgt int execlp (const char file, const char arg0, ..., const char argn, (char )0)
41Sample Codefork and exec
include ltstdio.hgt void main() int pid, status pid fork() if(pid -1) printf(fork failed\n) exit(1)
42Sample Codefork and exec
if(pid 0) / Child / if (execlp(/bin/ls, ls, NULL)lt 0) printf(exec failed\n) exit(1) else / Parent / wait(status) printf(Well done kid!\n) exit(0)
43Semantics of fork
44Quiz ------ Next Class