You are on page 1of 26

Operating System

A program that acts as an intermediary between a user of a computer


and the computer hardware
An Operating System is a layer of systems software that:
• has privileged access to the underlying hardware
• hides the hardware complexity
• Manages the hardware on the behalf of one or more running
applications
• ensures that applications are isolated and protected from one another.
• Controls execution of programs to prevent errors and improper use of
the computer
Computer System Structure
Computer System Structure

Computer system can be divided into four components:


• Hardware – provides basic computing resources
-> CPU, memory, I/O devices
• Operating system
-> Controls and coordinates use of hardware among various
applications and users
• Application programs – define the ways in which the system resources are
used to solve the computing problems of the users
-> Word processors, compilers, web browsers, database systems,
video games
• Users
-> People, machines, other computers
OS Elements

• Abstractions
corresponds to applications that OS executes
process, thread, file, socket, memory page
• Mechanisms
on top of Abstractions
create, schedule, open, write, allocate
• Policies
how mechanisms are used to manage underlying hardware
Least Recently Used (LRU)
OS services

• process management
• file management
• device management
• memory management
• storage management
• Security, etc
User-kernel Boundary
• user-mode/unprivileged mode : where applications execute.
• kernel-mode/ privileged mode: OS Kernel executes [here hardware
access is allowed]
• Mode bit provided by hardware
• Mode bit = 0; kernel mode
• Mode bit = 1; user mode
• Mode bit provides ability to distinguish when system is running user
code or kernel code
• Some instructions designated as privileged, only executable in kernel
mode
• System call changes mode to kernel, return from call resets it to user
System Calls
System Calls
• It provides programming interface to the services provided by the OS
• Typically written in a high-level language (C or C++)
• Mostly accessed by programs via a high-level Application Programming
Interface (API) rather than direct system call use
• Three most common APIs are Win32 API for Windows, POSIX API for
POSIX-based systems (including virtually all versions of UNIX, Linux, and
Mac OS X), and Java API for the Java virtual machine (JVM)
Types of System Calls
• Process control - create process, terminate process, load, execute, get
process attributes, set process attributes, wait for time etc.
• File management- create file, delete file, open, close file,read, write, etc.
• Device management-request device, release device etc
Example
• System call sequence to copy the contents of one file to another file
System call Implementation

• Typically, a number associated with each system call


• System-call interface maintains a table indexed according to these
numbers
• The system call interface invokes the intended system call in OS
kernel and returns status of the system call and any return values
• 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)
• Monolithic Kernel- Every Possible service that any one of the
application can demand is already part of the Operating System
• Modular OS – OS has basic services already part of it, but
everything can be added as a module. OS specifies certain
interfaces that any module must implement to be part of it.
• Microkernel OS – OS includes that contains the near-minimum
amount of functions and features required to implement an
operating system. It requires high degree of IPC.
Threads

• is an active entity
• An executing unit of a process
• Process creation is heavy-weight while thread creation is light-weight
• Threads of same process share address space, code, data, files
• Each thread will have separate execution context- registers, stack,
value of program counter.
• PCB for multi-threaded application contains information – shared
among all threads and thread specific information.
• User threads - management done by user-level threads library
• Kernel threads - Supported by the Kernel
Single and Multithreaded Processes
PCB for multi-threaded application
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
• User level thread management library for
scheduling, synchronization, etc
• Few systems currently use this model
• Examples:
• Solaris Green Threads
• GNU Portable Threads
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
• User level threads directly benefit from thread
support available in kernel.
• Examples
• Windows
• Linux
• Solaris 9 and later
Many-to-Many Model

• 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
Thread Libraries
• Thread library provides programmer with API for creating and managing threads
• Two primary ways of implementing
• Library entirely in user space
• Kernel-level library supported by the OS
Pthreads
• May be provided either as user-level or kernel-level
• A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
• Specification, not implementation
• API specifies behavior of the thread library, implementation is up to development of
the library
• Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Concurrency control and coordination

• Data Race- when multiple threads access shared data and try to
manipulate it at same time. So outcome depends on the order in which
data manipulation took place
• So critical section segment is piece of code where thread or process
may be changing common variables, updating table, writing file, etc
• When one process in critical section, no other may be in its critical
section.
• Each process must ask permission to enter critical section in entry
section, may follow critical section with exit section, then remainder
section
Solution to Critical Section

1. Mutual Exclusion - If process Pi is executing in its critical section,


then no other processes can be executing in their critical sections.
2. Progress - If no process is executing in its critical section and there
exist some processes that wish to enter their critical section, then the
selection of the processes that will enter the critical section next cannot
be postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and before that
request is granted
• Mutex is a lock that should be used while accessing the shared variable. When a
thread locks/holds the mutex it has an exclusive access to the shared resource .
Any other threads attempting to lock the same mutex are not going to be
successful until the previous thread releases /unlocks the mutex. So they will be
blocked on mutex.
• Condition variables
o Wait(mutex, condition)
mutex is automatically released and reacquired on wait
Producer-Consumer Problem: The consumer applies Wait until the list is full
o Signal(condition)
Notify only one thread waiting on condition
Producer-Consumer Problem: The Producer applies Signal to the Consumer
thread when the list is full
o Broadcast(condition)
Notify all waiting threads
Lock(mutex)
{
Critical Section
// only one thread can acess
this at a time
}
Unlock(mutex)

You might also like