Operating Systems
Sebnem Baydere
Lecture 2:Processes and Threads
Main Points
What is concurrency?
What are processes and threads?
How threads are related to processes and address spaces?
2.1 O/Ss have two general functions:
1. Coordinator: allow several things to work together in efficient and fair ways (examples: concurrency, memory protection, file systems, networking etc)
2. Standard services: provide standard facilities that everyone needs (examples: libraries, window system)
2.2 O/S as a coordinator: Concurrency
O/S has to coordinate all the activities on a machine – multiple users , I/O interrupts etc.
A batch system executes jobs
A time sharing system executes user programs or tasks
Multiprogramming – CPU switches between programs
How can it deal with it?
Answer: O/S needs a model that makes this pseudo parallelism easier to deal with. – PROCESS model
2.3 Processes
What is a process?
1. Operating system abstraction of a running program; that is everything needed to run a single program (traditional UNIX definition)
2. Sequential stream of execution in its own address space (formal definition)
Processes are the active programs in the system.
2.3.1 Two properties of a process:
1. sequential execution: there is no concurrency within a single process. Each statement of the program is executed sequentially.
2. process state: besides the program part everything that is needed to uniquely identify a process within the system. In other words everything that interacts with a process.
2.3.2 Process =? Program
A program is C statements or code for commands such as (ls, vi)
main(){ main(){ heap
......
} }
A() { A(){ stack
......
} } A
main
registers, PC
PROGRAM PROCESS
1. More to a process that just a program:
Program is just part of a process state.
I run ls; you run ls ---- same program, different processes.
2. Less to a process that a program:
A program can invoke more than one process to get a task done
cc starts up cpp,cc1, cc2 as each are separate programs themselves
example: program vs process
Computer Scientist baking a birthday cake
He has a recipe & a kitchen of stocks on the worktop (flour, eggs, sugar etc)
program (algorithm expressed in a subtle notation)
Comp. Scientist ---- processor
Cake ingredients ---- data
THE PROCESS : activity of him reading the recipe & fetching the ingredients baking
the cake
In the middle his son comes out crying stung by bee. Scientist records where he was in the recipe and puts all ingredients into the cupboard as he needs the worktop for another task(state of the current process saved), getout first aid book and first aid tools on to the worktop and begins following the directions in it.
Processor switched from baking to a higher priority process (administrating medical care) each process having a different program. (recipe, first aid book)
PROCESS IS AN ACTIVITY OF SOME KIND IN THE SYSTEM.
2.3.3 Definitions
Uniprogramming: one process at a time (ex: MS/DOS)
Easier for O/S builder : no concurrency. For personal computers originally the
idea was one user does only one thing at a time.
Harder for user: can’t work while waiting for printer
Multiprogramming: more than one process at atime (UNIX, Win95)
(often called multitasking – be careful sometimes multitasking may have
different meaning. We will see it later)
2.4 Threads
Thread: a sequential stream within a process (concurrency)
Address space: all the addresses that can be touched by the program. State needed to run a program. It provides illusion that program is runniing on its own machine (protection)
2.4.1 Why separate these concepts?
1. We can discuss the ‘thread’ part of a process, separately from the ‘address space ‘ part of a process.
2. Some systems allow multiple threads per address space. (new versions of UNIX)
Multithreading: a single program made up of a number of different concurrent activities (sometimes called multitasking as in Ada — do not confuse)
2.4.2 Examples of multithreaded programs
Robot control: single program, multiple concurrent operations.
Network server: single program, must handle concurrent requests from multiple users (web server)
Windowing system: one thread per window
Airline reservations: one thread per customer
Parallel programming: split program into multiple threads to make it faster. (Multiprocessing)
Multiprogramming = multiple jobs or processes
Multiprocessing = multiple CPUs
Some multiprocessors are infact uniprogrammed – multiple threads in one address space, but only run one program at a time.
2.4.3 What does thread have?
Some values are shared by the threads of a process: global variables, heap, files some are private to each thread: program counter, registers, execution stack.
Execution stack: parameters, temporary variables and return PC kept while a called procedure is executing. Example: where A’s variables kept while B,C are executing?
A(int tmp){
B(); A; tmp=2
printf();
} C
B(){ B
C();
}
A; tmp=1
C(){
A(2);
} Execution stack
Threads encapsulate concurrency; address space encapsulate protection – keep a buggy program from trashing everything else on the system. Address space contains contents of main memory and open files.
Address state is passive ; thread is active
2.5 Classification
Real O/Ss have either
One or many address spaces
One or many threads per address space
# of address space: / One / Many# of threads per address space
One / MS/DOS,Machintosh / Traditional UNIX, VMS, Mach, OS/2
Many / Pilot / Windows NT, Solaris, HP-UX
Examples:
1. MS/DOS –one thread, one process
2. Traditional UNIX – one thread per process, many processes
3. Mach, Windows NT, new UNIX – many threads per process, many processes
4. Pilot (the operating system on the first personal computer ever built) – many threads, one process (idea was – no need for protection if single user)
2.5 Summary
Processes have two parts: threads and address space.
Book talk about processes:
when this concerns concurrency, really talking about thread portion of a process;
when this concerns protection, talking about the address space portion of process