Lecture notes
Lecture notes
UNIT – 1
Operating System
An OS is a program that acts as an intermediary between
o computer-user and
o computer-hardware.
It also provides a basis for application-programs
Goals of OS:
o To execute programs.
o To make solving user-problems easier.
o To make the computer convenient to use.
The OS (also called kernel) is the one program running at all times on the computer.
Different types of OS:
o Mainframe OS is designed to optimize utilization of hardware.
o Personal computer (PC) OS supports complex game, business application.
o Handheld computer OS is designed to provide an environment in which a user can easily
o interface with the computer to execute programs.
System View
• An OS as a resource allocator
o Resources used to solve a computing-problem:
CPU time
memory-space
file-storage space and
I/0 devices.
o The OS manages and allocates the above resources to programs and the users.
• An OS is a control program
o The OS is needed to control:
operations of I/0 devices and
execution of user-programs to prevent errors.
Computer System Organization
• A computer consists of
o one or more CPUs and
o no. of device-controllers (Figure 1.2).
• Controller is in charge of a specific type of device (for ex: audio devices).
• CPU and controllers can execute concurrently.
• A memory-controller is used to synchronize access to the shared-memory.
• Following events occur for a computer to start running:
o Bootstrap program is an initial program that runs when computer is powered-up.
o Bootstrap program
initializes all the system from registers to memory-contents and
loads OS into memory.
o Then, OS
starts executing the first process (such as "init") and
waits for some event to occur.
o The occurrence of an event is signaled by an interrupt from either the hardware or the
software (Figure 1.3).
o Hardware may trigger an interrupt by sending a signal to the CPU. ii)
Software may trigger an interrupt by executing a system-call.
o When CPU is interrupted, the CPU
stops current computation and
transfers control to ISR (interrupt service routine).
o Finally, the ISR executes; on completion, the CPU resumes the interrupted computation.
Common Functions of Interrupts
• Interrupt transfers control to the ISR generally, through the interrupt-vector, which contains the
addresses of all the service routines.
• Interrupt architecture must save the address of the interrupted-instruction.
• Incoming interrupts are disabled while another interrupt is being processed to prevent a lost interrupt.
• A trap is a software-generated interrupt caused either by an error or a user request.
• A modern OS is interrupt-driven.
Figure 1.3 Interrupt time line for a single process doing output
Storage Structure
• Programs must be in main-memory (also called RAM) to be executed.
• Interaction with memory is done through a series of load or store instructions.
o Load Instruction
o Moves a word from main-memory to an internal register within the CPU.
o 2. Store Instruction
o Moves the content of a register to main-memory.
• Also, the CPU automatically loads instructions from main-memory for execution.
• Ideally, we want the programs & data to reside in main-memory permanently. This is not possible for
2 reasons:
Main-memory is small.
Main-memory is volatile i.e. it loses its contents when powered-off.
• Most computers provide secondary-storage as an extension of main-memory.
o For ex: magnetic disk.
• Main requirement:
o The secondary-storage must hold large amount of data permanently.
• The wide range of storage-systems can be organized in a hierarchy (Figure 1.4).
• The higher levels are expensive, but they are fast.
o The lower levels are inexpensive, but they are slow.
Multiprocessor Systems
• These systems have two or more processors which can share:
o → bus → clock → memory/peripheral devices
• Advantages:
• Increased Throughput
• By increasing no. of processors, we expect to get more work done in less time.
• Economy of Scale
• These systems are cheaper because they can share
→ peripherals → mass-storage → power-supply.
o If many programs operate on same data, they will be stored on one disk & all processors can
share them.
• Increased Reliability
o The failure of one processor will not halt the system.
• Two types of multiple-processor systems:
o Asymmetric multiprocessing(AMP) and 2) Symmetric multiprocessing(SMP)
• Asymmetric Multiprocessing
• This uses master-slave relationship (Figure 1.6).
• Each processor is assigned a specific task.
• A master-processor controls the system.
o The other processors look to the master for instruction.
• The master-processor schedules and allocates work to the slave-processors.
• Symmetric Multiprocessing
• Each processor runs an identical copy of OS.
• All processors are peers; no master-slave relationship exists between processors.
• Advantages:
o Many processes can run simultaneously.
o Processes and resources are shared dynamically among the various processors.
• Disadvantage:
o Since CPUs are separate, one CPU may be sitting idle while another CPU is overloaded. This
results in inefficiencies.
Batch Systems
• Early computers were physically enormous machines run from a console.
• The common input devices were card readers and tape drives.
• The common output devices were line printers, tape drives, and card punches.
• The user
o prepared a job which consisted of the program, the data, and control information
o submitted the job to the computer-operator.
• The job was usually in the form of punch cards.
• At some later time (after minutes, hours, or days), the output appeared.
• To speed up processing, operators batched together jobs with similar needs and ran them through the
computer as a group.
• Disadvantage:
o The CPU is often idle, because the speeds of the mechanical I/O devices.
Multiprogrammed Systems
• Multiprogramming increases CPU utilization by organizing jobs so that the CPU always has one to
execute.
• The idea is as follows:
o OS keeps several jobs in memory simultaneously (Figure 1.8).
o OS picks and begins to execute one of the jobs in the memory. Eventually, the job may have
to wait for some task, such as an I/O operation, to complete.
o OS simply switches to, and executes, another job.
o When that job needs to wait, the CPU is switched to another job, and so on.
o As long as at least one job needs to execute, the CPU is never idle.
• If several jobs are ready to be brought into memory, and if there is not enough room for all of them,
then the system must choose among them. Making this decision is job scheduling.
• If several jobs are ready to run at the same time, the system must choose among them. Making this
decision is CPU scheduling.
• Working principle:
o At system boot time, the hardware starts in kernel-mode.
o The OS is then loaded and starts user applications in user-mode.
o Whenever a trap or interrupt occurs, the hardware switches from user-mode to kernel-mode
(that is, changes the state of the mode bit to 0).
o The system always switches to user-mode (by setting the mode bit to 1) before passing control
to a user-program.
• Dual mode protects
o OS from errant users and
o errant users from one another.
• Privileged instruction is executed only in kernel-mode.
• If an attempt is made to execute a privileged instruction in user-mode, the hardware treats it as illegal
and traps it to the OS.
• A system calls are called by user-program to ask the OS to perform the tasks on behalf of the user
program.
• Timer
• Problem: We cannot allow a user-program to get stuck in an infinite loop and never return control to
the OS.
• Solution: We can use a timer.
• A timer can be set to interrupt the computer after a specific period.
• The period may be
o fixed (for ex: 1/60 second) or
o variable (for ex: from 1ns to 1ms).
• A variable timer is implemented by
o a fixed-rate clock and
o a counter.
• Working procedure:
o The OS sets the counter.
o Every time the clock ticks, the counter is decremented.
o When the counter reaches 0, an interrupt occurs.
• The instructions that modify the content of the timer are privileged instructions.
Process Management
• The OS is responsible for the following activities:
o Creating and deleting both user and system processes
o Suspending and resuming processes
o Providing mechanisms for process synchronization
o Providing mechanisms for process communication
o Providing mechanisms for deadlock handling
• A process needs following resources to do a task:
o CPU
o memory and
o files.
• The resources are allocated to process
o when the process is created or
o while the process is running.
• When the process terminates, the OS reclaims all the reusable resources.
• A program by itself is not a process;
o A program is a passive entity (such as the contents of a file stored on disk).
o A process is an active entity.
• Two types of process:
o Single-threaded process has one PC(program counter) which specifies location of the next
instruction to be executed.
o Multi-threaded process has one PC per thread which specifies location of next instruction to
execute in each thread
Memory Management
• The OS is responsible for the following activities:
o Keeping track of which parts of memory are currently being used and by whom
o Deciding which processes are to be loaded into memory when memory space becomes
available
o Allocating and de-allocating memory space as needed.
• Main memory is the array of bytes ranging from hundreds to billions.
• Each byte has its own address.
• The CPU
o reads instructions from main memory during the instruction-fetch cycle.
o reads/writes data from/to main-memory during the data-fetch cycle.
• To execute a program:
o The program will be
loaded into memory and
mapped to absolute addresses.
o Then, program accesses instructions & data from memory by generating absolute addresses.
o Finally, when program terminates, its memory-space is freed.
• To improve CPU utilization, keep several programs will be kept in memory
• Selection of a memory-management scheme depends on hardware-design of the system.
Storage Management
• File-System Management
• Mass-Storage Management
• Caching
Caching
• Caching is an important principle of computer systems.
• Information is normally kept in some storage system (such as main memory).
• As it is used, it is copied into a faster storage system called as the cache on a temporary basis.
• When we need a particular piece of information:
o We first check whether the information is in the cache.
o If information is in cache, we use the information directly from the cache.
o If information is not in cache, we use the information from the source, putting a copy in the
cache under the assumption that we will need it again soon.
• In addition, internal programmable registers, such as index registers, provide high-speed cache for
main memory.
• The compiler implements the register-allocation and register-replacement algorithms to decide which
information to keep in registers and which to keep in main memory.
• Most systems have an instruction cache to hold the instructions expected to be executed next.
• Most systems have one or more high-speed data caches in the memory hierarchy
• Because caches have limited size, cache management is an important design problem
• Careful selection of cache size & of a replacement policy can result in greatly increased performance
I/O Systems
• The OS must hide peculiarities of hardware devices from users.
• In UNIX, the peculiarities of I/O devices are hidden from the bulk of the OS itself by the I/O
subsystem.
• The I/O subsystem consists of
o A memory-management component that includes buffering, caching, and spooling.
o A general device-driver interface.
o Drivers for specific hardware devices.
• Only the device driver knows the peculiarities of the specific device to which it is assigned.
Distributed System
• This is a collection of physically separate, possibly heterogeneous computer-systems.
• The computer-systems are networked to provide the users with access to the various resources.
• Access to a shared resource increases
o computation speed
o functionality
o data availability and
o reliability
• A network is a communication path between two or more systems.
• Networks vary by the
o protocols used
o distances between nodes and
o transport media.
• Common network protocol are
o TCP/IP
o ATM.
• Networks are characterized based on the distances between their nodes.
o A local-area network (LAN) connects computers within a building.
o A wide-area network (WAN) usually links buildings, cities, or countries.
o A metropolitan-area network (MAN) could link buildings within a city.
• The media to carry networks are equally varied. They include
o copper wires,
o fiber strands, and
o wireless transmissions.
Multimedia Systems
• Multimedia data consist of audio and video files as well as conventional files.
• These data differ from conventional data in that multimedia data must be delivered(streamed)
according to certain time restrictions.
• Multimedia describes a wide range of applications. These include
o audio files such as MP3
o DVD movies
o video conferencing
o live webcasts of speeches
Handheld Systems
• Handheld systems include
o PDAs and
o cellular telephones.
• Main challenge faced by developers of handheld systems: Limited size of devices.
• Because of small size, most handheld devices have a
o small amount of memory,
o slow processors, and
o small display screens.
Computing Environments
• Traditional Computing
• Client-Server Computing
• Peer-to-Peer Computing
• Web-Based Computing
Traditional Computing
• Used in office environment:
o PCs connected to a network, with servers providing file and print services.
• Used in home networks:
o At home, most users had a single computer with a slow modem.
o Some homes have firewalls to protect their networks from security breaches.
• Web technologies are stretching the boundaries of traditional computing.
o Companies establish portals, which provide web accessibility to their internal servers.
o Network computers are terminals that understand web computing.
o Handheld PDAs can connect to wireless networks to use company's web portal.
• Systems were either batch or interactive.
• Batch system processed jobs in bulk, with predetermined input.
• Interactive systems waited for input from users.
Client-Server Computing
• Servers can be broadly categorized as (Figure
1.10): 1. Compute servers and 2. File servers
• Compute-server system provides an interface to which a client can send a request to perform an
action (for example, read data).
o In response, the server executes the action and sends back results to the client. File-server
system provides a file-system interface where clients can create, read, and delete files.
o For example: web server that delivers files to clients running web browsers.
Communications
• In some situations, one process needs to communicate with another process.
• Communications may be implemented via
o Shared memory or
o Message passing
• In message passing, packets of information are moved between processes by OS.
Error Detection
• Errors may occur in
CPU & memory-hardware (ex: power failure)
I/O devices (ex: lack of paper in the printer) and
user program (ex: arithmetic overflow)
o For each type of error, OS should take appropriate action to ensure correct & consistent
computing.
• Common functions for efficient operation of the system are:
Resource Allocation
• When multiple users are logged on the system at the same time, resources must be allocated to each of
them.
• The OS manages different types of resources.
• Some resources (say CPU cycles) may have special allocation code.
Other resources (say I/O devices) may have general request & release code.
Accounting
• We want to keep track of
o which users use how many resources and
o which kinds of resources.
• This record keeping may be used for
o accounting (so that users can be billed) or
o gathering usage-statistics.
Protection
• When several separate processes execute concurrently, it should not be possible for one process
to interfere with the others or with the OS itself.
• Protection involves ensuring that all access to resources is controlled.
• Security starts with each user having authenticated to the system by means of a password.
System Calls
• These provide an interface to the OS services.
• These are available as routines written in C and C++.
• The programmers design programs according to an
API. (API=application programming interface).
• The API
o defines a set of functions that are available to the programmer (Figure 1.15).
o includes the parameters passed to functions and the return values.
• The functions that make up an API invoke the actual system-calls on behalf of the programmer.
• Benefits of API:
o Program portability.
o Actual system-calls are more detailed (and difficult) to work with than the API available to the
programmer.
• Three general methods are used to pass parameters to the OS:
o 1, via registers.
• Using a table in memory & the address is passed as a parameter in a register (Figure 1.16).
• The use of a stack is also possible where parameters are pushed onto a stack and popped off the stack
by the OS.
• Using a stack where parameters are
o pushed onto a stack and
o popped off the stack.
• Process Control
• System calls used:
o end, abort
o load, execute
o create process, terminate process
o get process attributes, set process attributes
o wait for time
o wait event, signal event
o allocate and free memory
• A running program needs to be able to halt its execution either normally (end) or abnormally (abort).
• If program runs into a problem, error message may be generated and dumped into a file.
o This file can be examined by a debugger to determine the cause of the problem.
• The OS must transfer control to the next invoking command interpreter.
o Command interpreter then reads next command.
o In interactive system, the command interpreter simply continues with next command.
o In GUI system, a pop-up window will request action from user.
• How to deal with new process?
• A process executing one program can load and execute another program.
• Where to return control when the loaded program
terminates? The answer depends on the existing program:
o If control returns to the existing program when the new program terminates, we must save the
memory image of the existing program. (Thus, we have effectively created a mechanism for
one program to call another program).
o If both programs continue concurrently, we created a new process to be multiprogrammed.
• We should be able to control the execution of a process. i.e. we should be able to determine and reset
the attributes of a process such as:
o → job's priority or
o maximum execution time
• We may also want to terminate process that we created if we find that it
o is incorrect or
o is no longer needed.
• We may need to wait for processes to finish their execution.
o We may want to wait for a specific event to occur.
• The processes should then signal when that event has occurred.
File Management
• System calls used:
o create file, delete file
o open, close
o read, write, reposition
o get file attributes, set file attributes
• Working procedure:
o We need to create and delete files.
o Once the file is created,
we need to open it and to use it.
we may also read or write.
o Finally, we need to close the file.
• We need to be able to
o determine the values of file-attributes and
o reset the file-attributes if necessary.
• File attributes include
o file name
o file type
o protection codes and
o accounting information.
Device Management
• System calls used:
o request device, release device;
o read, write, reposition;
o get device attributes, set device attributes;
o logically attach or detach devices.
• A program may need additional resources to execute.
• Additional resources may be
o memory
o tape drives or
o files.
• If the resources are available, they can be granted, and control can be returned to the user program;
o If the resources are unavailable, the program may have to wait until sufficient resources are
available.
• Files can be thought of as virtual devices. Thus, many of the system calls used for files are also used
for devices.
• In multi-user environment,
o We must first request the device, to ensure exclusive use of it.
o After we are finished with the device, we must release it.
• Once the device has been requested (and allocated), we can read and write the device.
• Due to lot of similarity between I/O devices and files, OS (like UNIX) merges the two into a
combined file-device structure.
• UNIX merges I/O devices and files into a combined file-device structure.
• Information Maintenance
• System calls used:
o get time or date, set time or date
o get system data, set system data
o get process, file, or device attributes
o set process, file, or device attributes
• Many system calls exist simply for the purpose of transferring information between the user program
and the OS.
o For ex,
Most systems have a system call to return
current time and
current date.
Other system calls may return information about the system, such as
number of current users
version number of the OS
amount of free memory or disk space.
The OS keeps information about all its processes, and there are system calls to access
this information.
Communication
• System calls used:
o create, delete communication connection
o send, receive messages
o transfer status information
o attach or detach remote devices
• Two models of communication.
o Message-passing model and 2) Shared Memory Model
• Message Passing Model
• Information is exchanged through an IPC provided by OS.
o (IPC=inter process communication).
• Steps for communication:
o Firstly, a connection must be opened using open connection system-call.
o Each computer has a host-name, such as an IP name.
o Similarly, each process has a process-name, which is translated into an equivalent identifier.
The get hostid & get processid system-calls do this translation.
o Then, identifiers are passed to the open and close system-calls.
o The recipient-process must give its permission for communication to take place with an
accept connection system-call.
o (The processes that will be receiving connections are called daemons processes).
o Daemon processes
execute a wait for connection system-call and
are awakened when a connection is made.
o Then, client & server exchange messages by read message and write message system calls.
o Finally, the close connection system-call terminates the communication.
• Advantages:
o Useful when smaller numbers of data need to be exchanged.
o It is also easier to implement than is shared memory.
Shared Memory Model
• Processes use map memory system-calls to gain access to regions of memory owned by other
processes.
• Several processes exchange information by reading and writing data in the shared memory.
• The shared memory
o is determined by the processes and
o are not under the control of OS.
• The processes are also responsible for ensuring that they are not writing to the same location
simultaneously.
• Advantage:
o Shared memory allows maximum speed and convenience of communication,
• Disadvantage:
o Problems exist in the areas of protection and synchronization.
System Programs
• They provide a convenient environment for program development and execution.
o (System programs also known as system utilities).
• They can be divided into these categories:
• Six categories of system-programs:
o File Management
• These programs manipulate files i.e. create, delete, copy, and rename files.
o Status Information
• Some programs ask the system for
o date (or time)
o amount of memory(or disk space) or
o no. of users.
• These information is then printed to the terminal (or output-device or file).
• File Modification
• Text editors can be used to create and modify the content of files stored on disk.
• Programming Language Support
• Compilers, assemblers, and interpreters for common programming-languages (such as C,
o C++) are provided to the user.
• Program Loading & Execution
o The system may provide
o absolute loaders
o relocatable loaders
o linkage editors and
o overlay loaders.
• Debugging-systems are also needed.
• Communications
o These programs are used for creating virtual connections between
processes
users and
computer-systems.
o They allow users to
browse web-pages
send email or
log-in remotely.
• Most OSs are supplied with programs that
• solve common problems or
• perform common operations. Such programs include
o web-browsers
o word-processors
o spreadsheets and
o games.
o These programs are known as application programs.
Implementation
• OS's are nowadays written in higher-level languages like C/C++
• Advantages of higher-level languages:
o Faster development and
o OS is easier to port.
• Disadvantages of higher-level languages:
o Reduced speed and
o Increased storage requirements.
• Simple Structure
• These OSs are small, simple, and limited system.
• For example: MS-DOS and UNIX.
o MS-DOS
was written to provide the most functionality in the least space.
Disadvantages:
It was not divided into modules carefully (Figure 1.17).
The interfaces and levels of functionality are not well separated.
Figure 1.22 System models, (a) Nonvirtual machine, (b) Virtual machine.
• Problem: Virtual-machine software itself will need substantial disk space to provide virtual
memory. Solution: provide virtual disks that are identical in all respects except size.
• Advantages:
Complete protection of the various system resources.
It is a perfect vehicle for OS’s R&D.
• Disadvantage:
o Difficult to implement due to effort required to provide an exact duplicate to underlying
machine.
System Boot
Booting means starting a computer by loading the kernel.
Bootstrap program is a code stored in ROM.
The bootstrap program
o locates the kernel
o loads the kernel into main memory and
o starts execution of kernel.
OS must be made available to hardware so hardware can start it.