Os Unit I
Os Unit I
2. System View:
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and fair resource
use
OS is a control program
Controls execution of programs to prevent errors and improper use
of the computer
“The one program running at all times on the computer” is the kernel.
Everything else is either
a system program (ships with the operating system) , or
an application program.
Operating-System Structure
6. CPU Scheduling:
- Ensures efficient execution by deciding which job to run first when
multiple jobs are ready.
9. Resource Protection:
- Protects resources from unauthorized use and ensures jobs do not
interfere with each other.
The caller need know nothing about how the system call is
implemented
Just needs to obey API and understand what OS will do as a result
call
Most details of OS interface hidden from programmer by API
Managed by run-time support library (set of functions built into
libraries included with compiler)
System Calls
Long Term
Scheduler
Interrupt Handler
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Solution is correct, but can only use BUFFER_SIZE-1 elements
Interprocess Communication
Bounded-Buffer – Producer:
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Interprocess Communication
Bounded Buffer – Consumer:
while (true) {
while (in == out)
; // do nothing -- nothing to consume
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified
who the receiver was.
Interprocess Communication
2.2 Synchronization:
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send -- the sender is blocked until the message is received
Blocking receive -- the receiver is blocked until a message is
available
Non-blocking is considered asynchronous
Non-blocking send -- the sender sends the message and continue
Non-blocking receive -- the receiver receives:
A valid message, or
Null message
Different combinations possible
If both send and receive are blocking, we have a rendezvous
Interprocess Communication
Producer-consumer becomes trivial
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
Types of parallelism
Data parallelism – distributes subsets of the same data across multiple
cores, same operation on each core.
Task parallelism – distributing threads across cores, each thread
performing unique operation
Multi-Threading Models
Many-to-One
One-to-One
Many-to-Many
Multi-Threading Models
Many-to-One:
Many user-level threads mapped to
single kernel thread
One thread blocking causes all to
block
Multiple threads may not run in
parallel on muticore system because
only one may be in kernel at a time
Few systems currently use this
model
Examples:
Solaris Green Threads
GNU Portable Threads
Multi-Threading Models
One-to-One:
Each user-level thread maps to
kernel thread
Creating a user-level thread creates
a kernel thread
More concurrency than many-to-one
Number of threads per process
sometimes restricted due to
overhead
Examples
Windows
Linux
Solaris 9 and later
Multi-Threading Models
Many-to-Many:
Allows many user level threads to be
mapped to many kernel threads
Allows the operating system to
create a sufficient number of kernel
threads
Solaris prior to version 9
Windows with the ThreadFiber
package
Threading Issues
Semantics of fork() and exec() system calls
Signal handling
Synchronous and asynchronous
Thread cancellation of target thread
Asynchronous or deferred
Thread-local storage
Scheduler Activations
Threading Issues
1. Semantics of fork() and exec():
Does fork()duplicate only the calling thread or all threads?
Some UNIXes have two versions of fork
exec() usually works as normal – replace the running process including
all threads
Threading Issues
2. Signal Handlin():
Signals are used in UNIX systems to notify a process that a particular
event has occurred.
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. default
2. user-defined
Every signal has default handler that kernel runs when handling signal
User-defined signal handler can override default
For single-threaded, signal delivered to process
Threading Issues
2. Signal Handlin():
Where should a signal be delivered for multi-threaded?
Deliver the signal to the thread to which the signal applies
Deliver the signal to every thread in the process
Deliver the signal to certain threads in the process
Assign a specific thread to receive all signals for the process
Threading Issues
3. Thread Cancellation:
Terminating a thread before it has finished
Thread to be canceled is target thread
Two general approaches:
Asynchronous cancellation terminates the target thread immediately
Deferred cancellation allows the target thread to periodically check if it
should be cancelled
Pthread code to create and cancel a thread:
Threading Issues
3. Thread Cancellation:
Invoking thread cancellation requests cancellation, but actual cancellation
depends on thread state