Osunit 1
Osunit 1
OS provide environments in which programs run, and services for the users of the system,
including:
User Interfaces - Means by which users can issue commands to the system. Depending
on the system these may be a command-line interface ( e.g. sh, csh, ksh, tcsh, etc. ), a
GUI interface ( e.g. Windows, X-Windows, KDE, Gnome, etc. ), or a batch command
systems. The latter are generally older systems using punch cards of job-control language,
JCL, but may still be used today for specialty systems designed for a single purpose.
Program Execution - The OS must be able to load a program into RAM, run the program,
and terminate the program, either normally or abnormally.
I/O Operations - The OS is responsible for transferring data to and from I/O devices,
including keyboards, terminals, printers, and storage devices.
File-System Manipulation - In addition to raw data storage, the OS is also responsible for
maintaining directory and subdirectory structures, mapping file names to specific blocks of
data storage, and providing tools for navigating and utilizing the file system.
Communications - Inter-process communications, IPC, either between processes running
on the same processor, or between processes running on separate processors or separate
machines. May be implemented as either shared memory or message passing, ( or some
systems may offer both. )
Error Detection - Both hardware and software errors must be detected and handled
appropriately, with a minimum of harmful repercussions. Some systems may include
complex error avoidance or recovery systems, including backups, RAID drives, and other
redundant systems. Debugging and diagnostic tools aid users and administrators in tracing
down the cause of problems.
Resource Allocation - E.g. CPU cycles, main memory, storage space, and peripheral
devices. Some resources are managed with generic systems and others with very carefully
designed and specially tuned systems, customized for a particular resource and operating
environment.
Accounting - Keeping track of system activity and resource usage, either for billing
purposes or for statistical record keeping that can be used to optimize future performance.
Protection and Security - Preventing harm to the system and to resources, either
through wayward internal processes or malicious outsiders. Authentication,
ownership, and restricted access are obvious parts of this system. Highly secure
systems may log all process activity down to excruciating detail, and security
regulation dictate the storage of those records on permanent non-erasable medium
for extended times in secure ( off-site ) facilities.
In the 1970s, Batch processing was very popular. In this technique, similar types of jobs were
batched together and executed in time. People were used to having a single computer which was
called a mainframe.
In Batch operating system, access is given to more than one person; they submit their respective
jobs to the system for the execution.
The system put all of the jobs in a queue on the basis of first come first serve and then executes
the jobs one by one. The users collect their respective output when all the jobs get executed.
The purpose of this operating system was mainly to transfer control from one job to another as
soon as the job was completed. It contained a small set of programs called the resident monitor
that always resided in one part of the main memory. The remaining part is used for servicing
jobs.
Advantages of Batch OS
o The use of a resident monitor improves computer efficiency as it eliminates CPU time
between two jobs.
Disadvantages of Batch OS
1. Starvation
For Example:
There are five jobs J1, J2, J3, J4, and J5, present in the batch. If the execution time of J1 is very
high, then the other four jobs will never be executed, or they will have to wait for a very long time.
Hence the other processes get starved.
2. Not Interactive
Batch Processing is not suitable for jobs that are dependent on the user's input. If a job requires
the input of two numbers from the console, then it will never get it in the batch processing
scenario since the user is not present at the time of execution.
Multiprogramming is an extension to batch processing where the CPU is always kept busy. Each
process needs two types of system time: CPU time and IO time.
In a multiprogramming environment, when a process does its I/O, The CPU can start the
execution of other processes. Therefore, multiprogramming improves the efficiency of the
system.
Advantages of Multiprogramming OS
o Throughout the system, it increased as the CPU always had one program to execute.
o Response time can also be reduced.
Disadvantages of Multiprogramming OS
In Multiprocessing, Parallel computing is achieved. There are more than one processors present
in the system which can execute more than one process at the same time. This will increase the
throughput of the system.
In Multiprocessing, Parallel computing is achieved. More than one processor present in the
system can execute more than one process simultaneously, which will increase the throughput of
the system.
i) Symmetric multiprocessing
ii) asymmetric multiprocessing
Symmetric multiprocessing refers to the use of all processors to conduct OS tasks. It doesn't
have a master-slave relationship like asymmetric multiprocessing, and the shared memory is
used to communicate by all processors here. The processors start processes from the standard
ready queue, and every CPU might have its private queue of ready-to-execute programs. The
scheduler must ensure that no two CPUs run the same task simultaneously.
Symmetric Multiprocessing provides proper load balancing, improved fault tolerance, and
decreases the possibility of a CPU bottleneck. It is complicated since the entire CPUs share a
memory, and a processor failure in Symmetric Multiprocessing reduces computing capacity.
Asymmetric multiprocessing
If a master processor fails, one of the slave processors assumes control of the execution. If one
slave processor fails, the other slave processor takes over. It is simple since the data structure
and a single processor controls all system actions. Assume there are four CPUs named C1, C2,
C3, and C4. C4 is the master processor and allocates tasks to the other CPUs. If C1 is assigned
process P1, C2 is assigned process P2, and C3 is assigned process P3. Each processor will only
work on the processes that have been assigned to them.
1. Preemptive Multitasking
2. Cooperative Multitasking
Preemptive Multitasking
Preemptive multitasking is used in desktop operating systems. Unix was the first operating
system to use this method of multitasking. Windows NT and Windows 95 were the first versions
of Windows that use preemptive multitasking.
Cooperative Multitasking
The term 'Non-Preemptive Multitasking' refers to cooperative multitasking. The main purpose
of cooperative multitasking is to run the present task while releasing the CPU to allow another
process to run. This task is carried out by using taskYIELD (). When the taskYIELD() function is
called, context-switch is executed.
An Operating system, which includes software and associated protocols to communicate with
other computers via a network conveniently and cost-effectively, is called Network Operating
System.
Peer To Peer networks are the network resources in which each system has the same
capabilities and responsibilities, i.e., none of the systems in this architecture is superior to the
others in terms of functionality.
There is no master-slave relationship among the systems, i.e., every node is equal in a Peer to
Peer Network Operating System. All the nodes at the Network have an equal relationship with
others and have a similar type of software that helps the sharing of resources.
A Peer to Peer Network Operating System allows two or more computers to share their
resources, along with printers, scanners, CD-ROM, etc., to be accessible from each computer.
These networks are best suitable for smaller environments with 25 or fewer workstations.
To establish a Peer Peer Network, you need network adapter cards, properly installed network
cabling to connect them, and a network hub or switch to interconnect the computers.
Peer to Peer Networks is organized, simply a group of computers that can share resources. Each
computer in a workstation keeps track of its user accounts and security settings, so no single
computer is in charge of the workgroup. Workgroups have little security, and there is no central
login process. Any user can use any shared resources once he logs into a peer on the Network.
As there is no central security, sharing resources can be controlled by a password, or the user
may stop the accessibility of certain files or folders by making them not shared.
o Peer to Peer networks are usually less secure because they commonly use share-level
security.
o This failure of any node in a system affects the whole system.
o Its performance degrades as the Network grows.
o Peer to Peer networks cannot differentiate among network users who are accessing a
resource.
o In Peer to Peer Network, each shared resource you wish to control must have its
password. These multiple passwords may be difficult to remember.
o Lack of central control over the Network.
The client requests offerings which include printing and document storage, and servers satisfy
their requests. Normally all community offerings like digital mail, printing are routed through the
server.
Server computers systems are commonly greater effective than client computer systems. This
association calls for software programs for the customers and servers. The software program
walking at the server is known as the Network Operating System, which offers a community of
surroundings for server and client.
Client-Server Network was developed to deal with the environment when many PC printers and
servers are connected via a network. The fundamental concept changed to outline a specialized
server with unique functionality.
o This Network is more secure than the Peer Peer Network system due to centralized data
security.
o Network traffic reduces due to the division of work among clients and the server.
o The area covered is quite large, so it is valuable to large and modern organizations
because it distributes storage and processing.
o The server can be accessed remotely and across multiple platforms in the Client-Server
Network system.
o In this type of operating system, network traffic reduces due to the division between clients
and the server.
o This type of system is less expensive to set up and maintain.
o In this type of operating system, the failure of any node in a system affects the whole
system.
o Security and performance are important issues. So trained network administrators are
required for network administration.
In Real-Time Systems, each job carries a certain deadline within which the job is supposed to be
completed, otherwise, the huge loss will be there, or even if the result is produced, it will be
completely useless.
The Application of a Real-Time system exists in the case of military applications, if you want to
drop a missile, then the missile is supposed to be dropped with a certain precision.
Soft RTOS accepts a few delays via the means of the Operating system. In this kind of RTOS,
there may be a closing date assigned for a particular job, but a delay for a small amount of time
is acceptable. So, cut off dates are treated softly via means of this kind of RTOS.
In Firm RTOS additionally want to observe the deadlines. However, lacking a closing date might
not have a massive effect, however may want to purposely undesired effects, like a massive
discount within the fine of a product.
o Easy to layout, develop and execute real-time applications under the real-time operating
system.
o In a Real-time operating system, the maximum utilization of devices and systems.
In the Time Sharing operating system, computer resources are allocated in a time-dependent
fashion to several programs simultaneously. Thus it helps to provide a large number of user's
direct access to the main computer. It is a logical extension of multiprogramming. In time-sharing,
the CPU is switched among multiple programs given by different users on a scheduled basis.
o The time-sharing operating system provides effective utilization and sharing of resources.
o This system reduces CPU idle and response time.
The Distributed Operating system is not installed on a single machine, it is divided into parts, and
these parts are loaded on different machines. A part of the distributed Operating system is
installed on each machine to make their communication possible. Distributed Operating systems
are much more complex, large, and sophisticated than Network operating systems because they
also have to take care of varying networking protocols.
Bootstrap program is the first code that is executed when the computer system is started. The
entire operating system depends on the bootstrap program to work correctly as it loads the
operating system.
- One or more CPUs, device controllers connect through common bus providing access to
shared memory
- Device controller informs CPU that it has finished its operation by causing an interrupt
Interrupt transfers control to the interrupt service routine generally, through the interrupt
vector, which contains the addresses of all the service routines
Interrupt architecture must save the address of the interrupted instruction
The operating system preserves the state of the CPU by storing registers and the program
counter
Polling
The process in which the CPU constantly checks the status of the device- to see if it needs
the CPU's attention.
It is a protocol.
In this protocol, the CPU services the device.
In a computer, a vectored interrupt is an I/O interrupt that tells the part of the computer that
handles I/O interrupts at the hardware level that a request for attention from an I/O device has
been received and and also identifies the device that sent the request.
A vectored interrupt is an alternative to a polled interrupt , which requires that the interrupt
handler poll or send a signal to each device in turn in order to find out which one sent the
interrupt request
1.4 I/O Structure consists of Programmed I/O, Interrupt driven I/O, DMS, CPU, Memory,
External devices, these are all connected with the help of Peripheral I/O Buses and General I/O
Buses.
Different types of I/O Present inside the system are shown below −
Programmed I/O
In the programmed I/O when we write the input then the device should be ready to take the data
otherwise the program should wait for some time so that the device or buffer will be free then it
can take the input.
Once the input is taken then it will be checked whether the output device or output buffer is free
then it will be printed. This process is continued every time in transferring of the data.
I/O Interrupts
To initiate any I / O operation, the CPU first loads the registers to the device controller. Then the
device controller checks the contents of the registers to determine what operation to perform.
There are two possibilities if I / O operations want to be executed. These are as follows −
Synchronous I / O − The control is returned to the user process after the I/O process is
completed.
Asynchronous I/O − The control is returned to the user process without waiting for the I/O
process to finish. Here, I/O process and the user process run simultaneously.
DMA Structure
Direct Memory Access (DMA) is a method of handling I / O. Here the device controller directly
communicates with memory without CPU involvement.
After setting the resources of I/O devices like buffers, pointers, and counters, the device
controller transfers blocks of data directly to storage without CPU intervention.
DMA is generally used for high speed I / O devices.
In Memory Hierarchy the cost of memory, capacity is inversely proportional to speed. Here the
devices are arranged in a manner Fast to slow, that is from register to Tertiary memory.
Level-0 − Registers
The registers are present inside the CPU. As they are present inside the CPU, they have least
access time. Registers are most expensive and smallest in size generally in kilobytes. They are
implemented by using Flip-Flops.
Level-1 − Cache
Cache memory is used to store the segments of a program that are frequently accessed by the
processor. It is expensive and smaller in size generally in Megabytes and is implemented by
using static RAM.
Level-2 − Primary or Main Memory
It directly communicates with the CPU and with auxiliary memory devices through an I/O
processor. Main memory is less expensive than cache memory and larger in size generally in
Gigabytes. This memory is implemented by using dynamic RAM.
Level-3 − Secondary storage
Secondary storage devices like Magnetic Disk are present at level 3. They are used as backup
storage. They are cheaper than main memory and larger in size generally in a few TB.
Level-4 − Tertiary storage
Tertiary storage devices like magnetic tape are present at level 4. They are used to store
removable files and are the cheapest and largest in size (1-20 TB).
Memory levels in terms of size, access time, bandwidth is as follows.
Level Register Cache Primary Secondary
memory memory
Bandwidth 4k to 800 to 5k 400 to 2k 4 to 32
32k MB/sec MB/sec MB/sec
MB/sec
Size Less Less Less than Greater
than than 4MB 2 GB than 2 GB
1KB
Access 2 to 3 to 10 80 to 400 5ms
time 5nsec nsec nsec
Managed Compiler Hardware Operating OS or
by system user
Why memory Hierarchy is used in systems?
Memory hierarchy is arranging different kinds of storage present on a computing device based
on speed of access. At the very top, the highest performing storage is CPU registers which are
the fastest to read and write to. Next is cache memory followed by conventional DRAM memory,
followed by disk storage with different levels of performance including SSD, optical and magnetic
disk drives.
To bridge the processor memory performance gap, hardware designers are increasingly relying
on memory at the top of the memory hierarchy to close / reduce the performance gap. This is
done through increasingly larger cache hierarchies reducing the dependency on main memory
which is slower.
1.6 Common System Components
There are various components of an Operating System to perform well defined tasks. Though
most of the Operating Systems differ in structure but logically they have similar components.
Each component must be a well-defined portion of a system that appropriately describes the
functions, inputs, and outputs.
There are following 8-components of an Operating System:
1. Process Management
2. I/O Device Management
3. File Management
4. Network Management
5. Main Memory Management
6. Secondary Storage Management
7. Security Management
8. Command Interpreter System
The operating system is responsible for the following activities in connection with process
management:
File Management
File management is one of the most visible services of an operating system. Computers can
store information in several different physical forms; magnetic tape, disk, and drum are the most
common forms.
A file is defined as a set of correlated information and it is defined by the creator of the file.
Mostly files represent data, source and object forms, and programs. Data files can be of any type
like alphabetic, numeric, and alphanumeric.
A files is a sequence of bits, bytes, lines or records whose meaning is defined by its creator and
user.
The operating system implements the abstract concept of the file by managing mass storage
device, such as types and disks. Also files are normally organized into directories to ease their
use. These directories may contain files and other directories and so on.
The operating system is responsible for the following activities in connection with file
management:
Network Management
The definition of network management is often broad, as network management involves several
different components. Network management is the process of managing and administering a
computer network. A computer network is a collection of various types of computers connected
with each other.
Network management comprises fault analysis, maintaining the quality of service, provisioning of
networks, and performance management.
Network management is the process of keeping your network healthy for an efficient
communication between different computers.
Network administration
Network maintenance
Network operation
Network provisioning
Network security
Main Memory Management
Memory is a large array of words or bytes, each with its own address. It is a repository of quickly
accessible data shared by the CPU and I/O devices.
Main memory is a volatile storage device which means it loses its contents in the case of system
failure or as soon as system power goes down.
The main motivation behind Memory Management is to maximize memory utilization on the
computer system.
The operating system is responsible for the following activities in connections with memory
management:
Keep track of which parts of memory are currently being used and by whom.
Decide which processes to load when memory space becomes available.
Allocate and deallocate memory space as needed.
Security Management
The operating system is primarily responsible for all task and activities happen in the computer
system. The various processes in an operating system must be protected from each other’s
activities. For that purpose, various mechanisms which can be used to ensure that the files,
memory segment, cpu and other resources can be operated on only by those processes that
have gained proper authorization from the operating system.
Security Management refers to a mechanism for controlling the access of programs, processes,
or users to the resources defined by a computer controls to be imposed, together with some
means of enforcement.
For example, memory addressing hardware ensure that a process can only execute within its
own address space. The timer ensure that no process can gain control of the CPU without
relinquishing it. Finally, no process is allowed to do it’s own I/O, to protect the integrity of the
various peripheral devices.
Command Interpreter System
One of the most important component of an operating system is its command interpreter. The
command interpreter is the primary interface between the user and the rest of the system.
Command Interpreter System executes a user command by calling one or more number of
underlying system programs or system calls.
Command Interpreter System allows human users to interact with the Operating System and
provides convenient programming environment to the users.
Many commands are given to the operating system by control statements. A program which
reads and interprets control statements is automatically executed. This program is called the
shell and few examples are Windows DOS command window, Bash of Unix/Linux or C-Shell of
Unix/Linux.
Other Important Activities
An Operating System is a complex Software System. Apart from the above mentioned
components and responsibilities, there are many other activities performed by the Operating
System. Few of them are listed below:
Security − By means of password and similar other techniques, it prevents unauthorized
access to programs and data.
Control over system performance − Recording delays between request for a service and
response from the system.
Job accounting − Keeping track of time and resources used by various jobs and users.
Error detecting aids − Production of dumps, traces, error messages, and other debugging
and error detecting aids.
Coordination between other softwares and users − Coordination and assignment of
compilers, interpreters, assemblers and other software to the various users of the computer
systems.
The interface between a process and an operating system is provided by system calls. In
general, system calls are available as assembly language instructions. They are also included in
the manuals used by the assembly level programmers. System calls are usually made when a
process in user mode requires access to a resource. Then it requests the kernel to provide the
resource via a system call.
A figure representing the execution of the system call is given as follows −
As can be seen from this diagram, the processes execute normally in the user mode until a
system call interrupts this. Then the system call is executed on a priority basis in the kernel
mode. After the execution of the system call, the control returns to the user mode and execution
of user processes can be resumed.
In general, system calls are required in the following situations −
If a file system requires the creation or deletion of files. Reading and writing from files also
require a system call.
Creation and management of new processes.
Network connections also require system calls. This includes sending and receiving packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call.
Process
CreateProcess()ExitProcess()WaitForSingleObject() fork()exit()wait()
Control
File
CreateFile()ReadFile()WriteFile()CloseHandle() open()read()write()close()
Management
Device
SetConsoleMode()ReadConsole()WriteConsole() ioctl()read()write()
Management
Information
GetCurrentProcessID()SetTimer()Sleep() getpid()alarm()sleep()
Maintenance
There are many different system calls as shown above. Details of some of those system calls are
as follows −
open()
The open() system call is used to provide access to a file in a file system. This system call
allocates resources to the file and provides a handle that the process uses to refer to the file. A
file can be opened by multiple processes at the same time or be restricted to one process. It all
depends on the file organisation and file system.
read()
The read() system call is used to access data from a file that is stored in the file system. The file
to read can be identified by its file descriptor and it should be opened using open() before it can
be read. In general, the read() system calls takes three arguments i.e. the file descriptor, buffer
which stores read data and number of bytes to be read from the file.
write()
The write() system calls writes the data from a user buffer into a device such as a file. This
system call is one of the ways to output data from a program. In general, the write system calls
takes three arguments i.e. file descriptor, pointer to the buffer where data is stored and number
of bytes to write from the buffer.
close()
The close() system call is used to terminate access to a file system. Using this system call
means that the file is no longer required by the program and so the buffers are flushed, the file
metadata is updated and the file resources are de-allocated.
1.8 System Programs
In an operating system a user is able to use different types of system programs and the system
program is responsible for all the application software performance of the computer.
The system programs are responsible for the development and execution of a program and they
can be used by the help of system calls because system calls define different types of system
programs for different tasks.
File management − These programs create, delete, copy, rename, print, exit and generally
manipulate the files and directory.
Status information − It is the information regarding input, output process, storage and the
CPU utilization time how the process will be calculated in how much memory required to
perform a task is known by status information.
Programming language supports − compiler, assembler, interrupt are programming
language support used in the operating system for a particular purpose in the computer.
Programming Loading and execution − The needs to enter the program and after the
loading of a program it needs to execute the output of the programs and this task is also
performed by system calls by the help of system programs.
Communication − These services are provided by the user because by using this number of
devices communicates with each other by the help of device or wireless and communication is
necessary for the operating system.
Background services − There are different types of services available on the operating
system for communication and background service is used to change the background of your
window and it also works for scanning and detecting viruses in the computer.
1.8.1 Purpose of using system program
System programs communicate and coordinate the activities and functions of hardware and
software of a system and also controls the operations of the hardware. An operating system is
one of the examples of system software. Operating system controls the computer hardware and
acts like an interface between the application software’s.
1.8.2 Types of System programs
The types of system programs are as follows −
i)Utility program
It manages, maintains and controls various computer resources. Utility programs are
comparatively technical and are targeted for the users with solid technical knowledge.
Example - antivirus software, backup software and disk tools.
ii)Device drivers
It controls the particular device connected to a computer system. Device drivers basically act as
a translator between the operating system and device connected to the system.
Example − printer driver, scanner driver, storage device driver etc.
iii)Directory reporting tools
These tools are required in an operation system to have some software to facilitate the
navigation through the computer system.
Example − dir, ls, Windows Explorer etc.
1.9 Operating System Design and Implementation
An operating system is a construct that allows the user application programs to interact with the
system hardware. Operating system by itself does not provide any function but it provides an
atmosphere in which different applications and programs can do useful work.
There are many problems that can occur while designing and implementing an operating system.
These are covered in operating system design and implementation.
1.9.1 Operating System Design Goals
It is quite complicated to define all the goals and specifications of the operating system while
designing it.The design changes depending on the type of the operating system i.e if it is batch
system, time shared system, single user system, multi user system, distributed system etc.
There are basically two types of goals while designing an operating system. These are −
User Goals
The operating system should be convenient, easy to use, reliable, safe and fast according to the
users. However, these specifications are not very useful as there is no set method to achieve
these goals.
System Goals
The operating system should be easy to design, implement and maintain. These are
specifications required by those who create, maintain and operate the operating system. But
there is not specific method to achieve these goals as well.
1.9.2 Operating System Mechanisms and Policies
There is no specific way to design an operating system as it is a highly creative task. However,
there are general software principles that are applicable to all operating systems.
A subtle difference between mechanism and policy is that mechanism shows how to do
something and policy shows what to do. Policies may change over time and this would lead to
changes in mechanism. So, it is better to have a general mechanism that would require few
changes even when a policy change occurs.
For example - If the mechanism and policy are independent, then few changes are required in
mechanism if policy changes. If a policy favours I/O intensive processes over CPU intensive
processes, then a policy change to preference of CPU intensive processes will not change the
mechanism.
1.9.3 Operating System Implementation
The operating system needs to be implemented after it is designed. Earlier they were written in
assembly language but now higher level languages are used.
Advantages of Higher Level Language
There are multiple advantages to implementing an operating system using a higher level
language such as: the code is written more fast, it is compact and also easier to debug and
understand. Also, the operating system can be easily moved from one hardware to another if it is
written in a high level language.
Disadvantages of Higher Level Language
Using high level language for implementing an operating system leads to a loss in speed and
increase in storage requirements. However in modern systems only a small amount of code is
needed for high performance, such as the CPU scheduler and memory manager. Also, the
bottleneck routines in the system can be replaced by assembly language equivalents if required.
Process memory is divided into four sections as shown in Figure 1.10 below:
o The text section comprises the compiled program code, read in from non-
volatile storage when the program is launched.
o The data section stores global and static variables, allocated and initialized
prior to executing main.
o The heap is used for dynamic memory allocation, and is managed via calls to
new, delete, malloc, free, etc.
o The stack is used for local variables. Space on the stack is reserved for local
variables when they are declared , and the space is freed up when the
variables go out of scope. Note that the stack is also used for function return
values, and the exact mechanisms of stack management may be language
specific.
o Note that the stack and the heap start at opposite ends of the process's free
space and grow towards each other. If they should ever meet, then either a
stack overflow error will occur, or else a call to new or malloc will fail due to
insufficient memory available.
When processes are swapped out of memory and later restored, additional
information must also be stored and restored. Key among them are the program
counter and the value of all program registers.
For each process there is a Process Control Block, PCB, which stores the following ( types of )
process-specific information, as illustrated in Figure 1.12 .
The two main objectives of the process scheduling system are to keep the CPU busy at all
times and to deliver "acceptable" response times for all programs, particularly for interactive
ones.
The process scheduler must meet these objectives by implementing suitable policies for
swapping processes in and out of the CPU.
1.11.2 Schedulers
A process migrates between the various scheduling queues throughout its lifetime. The
operating system must select , for scheduling purposes, process from these queues in some
fashion. The selection process is carried out by the appropriate scheduler.
Whenever an interrupt arrives, the CPU must do a state-save of the currently running
process, then switch into kernel mode to handle the interrupt, and then do a state-
restore of the interrupted process.This is known as context switch .
Similarly, a context switch occurs when the time slice for one process has expired
and a new process is to be loaded from the ready queue. This will be instigated by a
timer interrupt, which will then cause the current process's state to be saved and the
new process's state to be restored.
Saving and restoring states involves saving and restoring all of the registers and
program counter(s), as well as the process control blocks described above.
Context switching happens VERY VERY frequently, and the overhead of doing the
switching is just lost CPU time, so context switches ( state saves & restores ) need to
be as fast as possible. Some hardware has special provisions for speeding this up,
such as a single machine instruction for saving or restoring all registers at once.
1.12 Operations on Processes
Processes may create other processes through appropriate system calls, such
as fork or spawn. The process which does the creating is termed the parent of the
other process, which is termed its child.
Each process is given an integer identifier, termed its process identifier, or PID. The
parent PID ( PPID ) is also stored for each process.
On typical UNIX systems the process scheduler is termed sched, and is given PID 0.
The first thing it does at system startup time is to launch init, which gives that
process PID 1. Init then launches all system daemons and user logins, and becomes
the ultimate parent of all other processes. Figure 1.12.1 shows a typical process tree
for a Linux system, and other systems will have similar though not identical trees:
Processes may request their own termination by making the exit( ) system call,
typically returning an int. This int is passed along to the parent if it is doing a wait( ),
and is typically zero on successful completion and some non-zero code in the event
of problems.
processes may also be terminated by the system for a variety of reasons, including:
o The inability of the system to deliver necessary system resources.
o In response to a KILL command, or other un handled process interrupt.
o A parent may kill its children if the task assigned to them is no longer needed.
If the parent exits, the system may or may not allow the child to continue without a
parent.
When a process terminates, all of its system resources are freed up, open files
flushed and closed, etc. The process termination status and execution times are
returned to the parent if the parent is waiting for the child to terminate, or eventually
returned to init if the process becomes an orphan.
Independent Processes operating concurrently on a systems are those that can neither
affect other processes or be affected by other processes.
Cooperating Processes are those that can affect or be affected by other processes.
o Information Sharing - There may be several processes which need access to the
same file for example. ( e.g. pipelines. )
o Computation speedup - Often a solution to a problem can be solved faster if the
problem can be broken down into sub-tasks to be solved simultaneously ( particularly
when multiple processors are involved. )
o Modularity - The most efficient architecture may be to break a system down into
cooperating modules. ( E.g. databases with a client-server architecture. )
o Convenience - Even a single user may be multi-tasking, such as editing, compiling,
printing, and running the same code in different windows.
Cooperating processes require some type of inter-process communication, which is most
commonly one of two types: Shared Memory systems or Message Passing systems. Figure
1.17 illustrates the difference between the two systems:
Figure 1.17 - Communications models: (a) Message passing. (b) Shared memory.
Shared Memory is faster once it is set up, because no system calls are required and
access occurs at normal memory speeds. However it is more complicated to set up, and
doesn't work as well across multiple computers. Shared memory is generally preferable
when large amounts of information must be shared quickly on the same computer.
Message Passing requires system calls for every message transfer, and is therefore
slower, but it is simpler to set up and works well across multiple computers. Message
passing is generally preferable when the amount and/or frequency of data transfers is
small, or when multiple computers are involved.
This is a classic example, in which one process is producing data and another
process is consuming the data. ( In this example in the order in which it is produced,
although that could vary. )
The data is passed via an intermediary buffer, which may be either unbounded or
bounded. With a bounded buffer the producer may have to wait until there is space
available in the buffer, but with an unbounded buffer the producer will never need to
wait. The consumer may need to wait in either case until there is data available.
This example uses shared memory and a circular queue. Note in the code below that
only the producer changes "in", and only the consumer changes "out", and that they
can never be accessing the same array location at the same time.
First the following data is set up in the shared memory area:
#define BUFFER_SIZE 10
typedef struct {
...
} item;
Then the producer process. Note that the buffer is full when "in" is one less than "out"
in a circular sense:
item nextProduced;
while( true ) {
Then the consumer process. Note that the buffer is empty when "in" is equal to "out":
item nextConsumed;
while( true ) {
}
1.13.2 Message-Passing Systems
Message passing systems must support at a minimum system calls for "send
message" and "receive message".
A communication link must be established between the cooperating processes before
messages can be sent.
There are three key issues to be resolved in message passing systems:
o Direct or indirect communication ( naming )
o Synchronous or asynchronous communication
o Automatic or explicit buffering.
1.13.2.1 Naming
With direct communication the sender must know the name of the receiver to
which it wishes to send a message.
o There is a one-to-one link between every sender-receiver pair.
o For symmetric communication, the receiver must also know the specific
name of the sender from which it wishes to receive messages.
For asymmetric communications, this is not necessary.
Indirect communication uses shared mailboxes, or ports.
o Multiple processes can share the same mailbox or boxes.
o Only one process can read any given message in a mailbox. Initially the
process that creates the mailbox is the owner, and is the only one allowed
to read mail in the mailbox, although this privilege may be transferred.
o The OS must provide system calls to create and delete mailboxes, and to
send and receive messages to/from mailboxes.
1.13.2.2 Synchronization
1.13.2.3 Buffering
Messages are passed via queues, which may have one of three capacity
configurations:
1. Zero capacity - Messages cannot be stored in the queue, so senders
must block until receivers accept the messages.
2. Bounded capacity- There is a certain pre-determined finite capacity in
the queue. Senders must block if the queue is full, until space becomes
available in the queue, but may be either blocking or non-blocking
otherwise.
3. Unbounded capacity - The queue has a theoretical infinite capacity, so
senders are never forced to block.
1.14.1 Sockets
Port numbers below 1024 are considered to be well-known, and are generally
reserved for common Internet services. For example, telnet servers listen to port 23,
ftp servers to port 21, and web servers to port 80.
General purpose user sockets are assigned unused ports over 1024 by the operating
system in response to system calls such as socket( ) or soctkepair( ).
Communication channels via sockets may be of one of two major forms:
o Connection-oriented ( TCP, Transmission Control Protocol ) connections
emulate a telephone connection. All packets sent down the connection are
guaranteed to arrive in good condition at the other end, and to be delivered to
the receiving process in the order in which they were sent. The TCP layer of the
network protocol takes steps to verify all packets sent, re-send packets if
necessary, and arrange the received packets in the proper order before
delivering them to the receiving process. There is a certain amount of overhead
involved in this procedure, and if one packet is missing or delayed, then any
packets which follow will have to wait until the errant packet is delivered before
they can continue their journey.
o Connectionless ( UDP, User Datagram Protocol ) emulate individual
telegrams. There is no guarantee that any particular packet will get through
undamaged ( or at all ), and no guarantee that the packets will get delivered in
any particular order. There may even be duplicate packets delivered,
depending on how the intermediary connections are configured. UDP
transmissions are much faster than TCP, but applications must implement their
own error checking and recovery procedures.
1.14.2 Pipes
Pipes are one of the earliest and simplest channels of communications between (
UNIX ) processes.
There are four key considerations in implementing pipes:
1. Unidirectional or Bidirectional communication?
2. Is bidirectional communication half-duplex or full-duplex?
3. Must a relationship such as parent-child exist between the processes?
4. Can pipes communicate over a network, or only on the same machine?
The following sections examine these issues on UNIX and Windows
Ordinary pipes are uni-directional, with a reading end and a writing end. ( If
bidirectional communications are needed, then a second pipe is required. )
In UNIX ordinary pipes are created with the system call "int pipe( int fd [ ] )".
o The return value is 0 on success, -1 if an error occurs.
o The int array must be allocated before the call, and the values are filled in by
the pipe system call:
fd[ 0 ] is filled in with a file descriptor for the reading end of the pipe
fd[ 1 ] is filled in with a file descriptor for the writing end of the pipe
o UNIX pipes are accessible as files, using standard read( ) and write( ) system
calls.
o Ordinary pipes are only accessible within the process that created them.
Typically a parent creates the pipe before forking off a child.
When the child inherits open files from its parent, including the pipe file(s),
a channel of communication is established.
Each process ( parent and child ) should first close the ends of the pipe
that they are not using. For example, if the parent is writing to the pipe
and the child is reading, then the parent should close the reading end of
its pipe after the fork and the child should close the writing end.
Figure 3.22 shows an ordinary pipe in UNIX.
1.14.2.2 Named Pipes
1.15 Threads
1.15.1 Motivation
Threads are very useful in modern programming whenever a process has multiple
tasks to perform independently of the others.
This is particularly true when one of the tasks may block, and it is desired to allow the
other tasks to proceed without blocking.
For example in a word processor, a background thread may check spelling and
grammar while a foreground thread processes user input ( keystrokes ), while yet a
third thread loads images from the hard drive, and a fourth does periodic automatic
backups of the file being edited.
1.15.2 Benefits
There are two types of threads to be managed in a modern system: User threads and
kernel threads.
User threads are supported above the kernel, without kernel support. These are the
threads that application programmers would put into their programs.
Kernel threads are supported within the kernel of the OS itself. All modern OSes support
kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to
service multiple kernel system calls simultaneously.
In a specific implementation, the user threads must be mapped to kernel threads, using
one of the following strategies.
In the many-to-one model, many user-level threads are all mapped onto a single
kernel thread.
Thread management is handled by the thread library in user space, which is very
efficient.
However, if a blocking system call is made, then the entire process blocks, even if the
other user threads would otherwise be able to continue.
Because a single kernel thread can operate only on a single CPU, the many-to-one
model does not allow individual processes to be split across multiple CPUs.
The one-to-one model creates a separate kernel thread to handle each user thread.
One-to-one model overcomes the problems listed above involving blocking system
calls and the splitting of processes across multiple CPUs.
However the overhead of managing the one-to-one model is more significant,
involving more overhead and slowing down the system.
Most implementations of this model place a limit on how many threads can be
created.
Linux and Windows from 95 to XP implement the one-to-one model for threads.
The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads, combining the best features of the one-to-one and
many-to-one models.
Users have no restrictions on the number of threads created.
Blocking kernel system calls do not block the entire process.
Processes can be split across multiple processors.
Individual processes may be allocated variable numbers of kernel threads, depending
on the number of CPUs present and other factors.