Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Uniprogramming Multiprogramming
Only one process resident at a time Multiple processes resident at a time (or,
multitasking)
Examples:
First systems and DOS for PCs Multiprocessing: Systems with multiple
processors
Advantages:
Runs on all hardware Examples: Unix variants, WindowsNT
Mechanism Policy
Question 1: Question 2:
How does dispatcher gain control? What execution context must be saved
and restored?
Direct execution
Direct execution
• Allow user process to run directly on hardware
• OS creates process and transfers control to starting point (i.e., main())
Problems with direct execution?
1. Process could do something restricted
Could read/write other process data (disk or memory)
2. Process could run forever (slow, buggy, or malicious)
OS needs to be able to switch between processes
3. Process could do something slow (like I/O)
OS wants to use resources efficiently and switch CPU to other process
Q1: Hardware for Multiprogramming
Need:
Must differentiate application process and OS
Hardware support:
Bit in status word designates whether currently running in user or system mode
Usage
Applications run in user mode
OS runs in system mode
Q1: Entering system mode
How does OS get control?
Synchronous interrupts, or traps Asynchronous interrupts
Event internal to a process that gives control Events external to a process, generated
to OS by hardware
Examples:
System calls, page faults (access page not in Examples:
main memory), or errors (illegal instruction or Characters typed, or completion of a
divide by zero) disk transfer
Examples:
System call, page fault (access page not in main memory), or error (illegal instruction or divide by
zero)
Disadvantages:
Processes can misbehave
By avoiding all traps and performing no I/O, can take over entire machine
Only solution: Reboot!
Not performed in modern operating systems
Q1: How does
Dispatcher run?
• The purpose of fork() is to create a new process, which becomes the child process of the
caller.
• After a new child process is created, both processes will execute the next instruction
following the fork() system call.
• If fork() returns a negative value, the creation of a child process was unsuccessful.
• fork() returns a zero to the newly created child process.
• fork() returns a positive value, the process ID of the child process, to the parent.
Process Creation
246 246
67 68
pid pid
=24668 =0
Process Creation wait()
prompt> ./p1 hello world (pid:29146)
hello, I am child
parent(pid:29147)
of 29147 (pid:29146)
hello, I am parent of 29147 (pid:29146)
child (pid:29147) prompt>
prompt>
Process Creation
prompt> ./p2 hello world (pid:29266)
hello, I am child (pid:29267)
hello, I am parent of 29267 (wc:29267)
(pid:29266) prompt>
The system call wait() is easy. This function blocks the calling process until
one of its child processes exits or a signal is received.
Process Creation
Execute a Program: the execvp() System Call
The created child process does not have to run the same program as the parent process
does.
The exec type system calls allow a process to run any program files, which include a binary
executable or a shell script.
fork vs exec
• fork starts a new process which is a copy of the one that calls it, while exec
replaces the current process image with another (different) one.