Title: Processes and threads
1Processes and threads
- A process is like a person
- It has consciousness and possessions
- Once started it is like a large group of people,
coming and going and doing their thing - Some resources are system-wide
- Others are part of the process
2More on processes
- What objects are global, local, and shared
- Global
- The file system
- I/O devices and their handlers
- Physical memory
- Network interfaces
- Local
- Transactions on a file
- The memory space of a process
- The currently running program
- Shared
- Shared memory regions
- Anything commonly owned by a process group
3Processes and threads
- Thread
- The stream of consciousness includes
- Program counter and instruction register
- Other register contents
- Memory regions (stack) reserved to this thread
- It exists on a one processor
- Process
- The possessions include
- Memory regions
- Open files
- Network connections
- Possessions can be available to several threads
and several processors - This possession set is often called user context.
- Switching user context is slow, a thread switch
is fast
4User and kernel-level threads
- Relationship between processes and their threads
- Originally processes were all single-threaded
- User-level threads are controlled by the
underlying processKernel is unaware of them it
schedules processesprocess starts and schedules
its own threads - Kernel-level threads are known to and scheduled
by the kernelIn Linux, they, not processes are
the unit of schedulingThreads inside a process
are just those that share parts or most of their
environment - Threads are to processes as individual family
members are to processes - A third variety is the kernel thread
- This is known only inside the kernel
- It has no user context, so starting it does not
cause a context switch - In Linux, most kernel work is done by kernel
threads
5Concurrency
- The theoretical core of OS
- An OS creates a set of processes that run
concurrently - A process is like a person - it has
- a stream of consciousness
- possessions - memory, files, data
- is created, reproduces, dies
- interacts with other processes
- In actuality, it is a program in execution
6Concurrency and mutual exclusion
- Concurrent process misbehaviors
- Race conditions - unpredictable results because
processes simultaneously modify data or devices - Deadlocks - Each has what the other wants
- Starvation - some processes go hungry - others
eat well - Mutual exclusion (ME)
- Motivation is practical some resources dont
function if modified simultaneously by more than
one process - A satisfactory ME mechanism must satisfy
- Mutual exclusion
- No preemption
- Bounded waiting
- Progress
-
7Structure of a process
- In any OS
- Consciousness
- Program counter and IP
- Register contents
- Link to possessions
- Possessions
- Memory regions
- Allocated resources
- Open files (shared with other processes)
- Individual file (exclusive during operations)
- Interprocess communication instances (shared with
other processes) - note these are used to control use of other
shared resources
8Context and context switching
- Process context
- The consciousness (PC, IP, registers) of a
process - The possessions / especially memory regions and
resources - Context switch
- The act of suspending one process and activating
another - Requires saving old register context, restoring
new - Requires remapping user memory
- Relatively slow operation (typically 1 ms)
- Kernel context
- Remains the same (not remapped at context switch
time) - Only usable context during interrupts
9Thread and kernel switching
- Thread
- Stream of consciousness only
- Uses (and shares) possessions of some process
environment - May be of several types
- kernel-level threads (KLTs) known to and
scheduled by OS - user-level threads (ULTs) not known to OS,
scheduled inside their user process - Kernel threads - known to and scheduled by OS,
only kernel context is used, so no context switch
is used to handle them. - Thread advantages
- Thread switching within process or kernel is much
faster than full context switch - Multiple processes using same resource set are a
common happening
10Memory space of a process
- Who allocates these
- Compiler, linker and loader
- User code
- Static data
- reserved space for kernel memory
- Run-time support
- Heap and stack
- Heap is allocated by new
- stack growth area by default after initial
allocation -
Kernel memory
Stack
Unallocated Growth area For stack and heap
Dynamic data (the heap)
Static data
User code
11Some operating system ideas - also review
- Everything is a file - even devices
- This allows a program to work with human input,
or a device, or a temporary or permanent file - Interrupt-driven behavior
- Example - windows - process is simultaneously
sensitive to events from mouse, keyboard, process
subsystem, and window manager - Caching - Foreground and background copies
- Used in
- Cache memory
- Swapping
- The disk block cache - recent transactions are in
memory - Networking utilities
12Processes and their states
- The process state includes the following
- Running/ready/blocked (waiting on an
event/events) - Priority
- Kernel/User mode
- In-memory/swapped
- Process ID
- Priority
13The process owns
- U-area (but user cant examine/modify it)
- Entries in the system open-file table
- Its own file control blocks (linked to the system
table) - Its own memory space
14The system process table
- Used for scheduling and control of processes
- Scheduler acts on timer interrupt or a process
state change - It schedules the highest-priority process that is
ready - It causes a context switch (gives the process
control) - This mechanism
- Gives system and I/O processes high priority
- Keeps the CPU puercos from monopolizing
- Interrupts are outside process country
- This avoids make a context switch for each
interrupt - It means the interrupt service routines use only
the kernels memory
15Mutual Exclusion - software solutions
- Unsuccessful solutions (on separate slides)
- Strict alternation
- Algorithms where mutual exclusion fails
- Petersons algorithm
- Description of Bakery Algorithm
- Just like Baskin-Robbins - incoming process takes
a number - Since two users can grab same number,
lowest-number process gets priority
16Avoiding busy waiting - Semaphores and messages
- Classical semaphores
- The semaphore is an ADT with a counter, a process
queue, and two operations - The down operation sleeps enqueued if the counter
is zero, and then decrements the counter - The up operation increments the counter and
awakens a sleeper if the counter was 0. - The down operation enters a critical section, the
up leaves it. - Messages
- Reliable messages to an administrator work like a
down or up - Messages and semaphores are equivalent
- Thats why
17Semaphores and messages - continued
- Mistakes are easy
- Deadlock
- Gateway-controlling and resource-controlling
semaphores invoked in wrong order - deadlocks and
slow performance
18Language Mechanisms - Monitors
- Monitor is a class with mutual exclusion it has
- Member functions
- Data members
- Condition variables (user processes wait on
these) - Two ways to enter
- Normal
- Returning from wait on a condition variable
- Two ways to leave
- Normal (return from a member function)
- Waiting on a condition variable
- Monitors can be implemented by
- Preprocessor that generates semaphore code
- Compiler modification
19Classical IPC problems
- Producer-consumer
- Scenario
- Producer produces information
- Consumer consumes it
- Both must wait on buffer availability
- Problem resembler disk I/O and print servers
- Code is in book - both C and monitor
- Readers-Writers
- Scenario
- Readers can write whenever no writer is active
- Writers must have solitude
- Problem resembles database
- Code in book - both C and monitor
20Classical IPC problems (continued)
- Dining Philosphers
- Scenario
- N philosophers think, wait for chopsticks, and
eat - Problem is useless but illustrative
- Code - both C and monitor