OS Unit 1 Notes
OS Unit 1 Notes
Operating system overview-objectives and functions, Evolution of Operating System - Computer System
Organization-Virtualization-Operating System Structure and Operations- System Calls, OS Generations,
Process Concept, Threads- Overview, Multithreading Models, Threading issues
An OS is defined as a System program that controls the execution of application programs and acts as an
interface between applications and the computer hardware.
An operating system is a program that manages a computer’s hardware. It also provides a basis for application
programs and acts as an intermediary between the computer user and the computer hardware. It can be thought
of as having three objectives:
• Convenience
• Efficiency
• Ability to evolve
The user of those applications, the end user, generally is not concerned with the details of computer hardware.
An application can be expressed in a programming language and is developed by an application programmer.
A set of system programs referred to as utilities implement frequently used functions that assist in program
creation, the management of files, and the control of I/O devices.
The most important collection of system programs comprises the OS. The OS masks the details of the
hardware from the programmer and provides the programmer with a convenient interface for using the system.
1
Briefly, the OS typically provides services in the following areas:
Program development
Program execution
Access to I/O devices
Controlled access to files
System access
Error detection and response
Accounting:
A computer is a set of resources for the movement, storage, and processing of data and for the control of these
functions. The OS is responsible for managing these resources.
The OS functions in the same way as ordinary computer software; that is, it is a program or suite of programs
executed by the processor.
The OS frequently relinquishes control and must depend on the processor to allow it to regain control.
The OS directs the processor in the use of the other system resources and in the timing of its execution of other
programs.
A portion of the OS is in main memory. This includes the kernel, or nucleus, which contains the most
frequently, used functions in the OS.The remainder of main memory contains user programs and data.
The allocation of this resource (main memory) is controlled jointly by the OS and memory management
hardware in the processor.
The OS decides when an I/O device can be used by a program in execution and controls access to and use of
files.
2
The processor itself is a resource, and the OS must determine how much processor time is to be devoted to the
execution of a particular user program. In the case of a multiple-processor system, this decision must span all
of the processors.
Process management
Memory management
File management
I/O management
Storage management.
Serial processing
During 1940s to the mid-1950s, the programmer interacted directly with the computer hardware; there was no
OS.
Programs in machine code were loaded via the input device (e.g., a card reader).
If an error halted the program, the error condition was indicated by the lights.
If the program proceeded to a normal completion, the output appeared on the printer.
These early systems presented two main problems:
i) Scheduling: Most installations used a hardcopy sign-up sheet to reserve computer time. A user might sign up for an
hour and finish in 45 minutes; this would result in wasted computer processing time. On the other hand, the user might
run into problems, not finish in the allotted time, and be forced to stop before resolving the problem.
ii) Setup time: A single program, called a job, could involve loading the compiler plus the high-level language
program (source program) into memory, saving the compiled program (object program) and then loading and linking
together the object program and common functions. Thus, a considerable amount of time was spent just in setting up
the program to run.
This mode of operation could be termed serial processing, reflecting the fact that users have access to the computer in
series
3
Memory Layout for a Resident Monitor
The monitor controls the sequence of events. For this the monitor must always be in main memory and
available for execution. That portion is referred to as the resident monitor.
The monitor reads in jobs one at a time from the input device .As it is read in, the current job is placed in the
user program area, and control is passed to this job.
Once a job has been read in, the processor will encounter a branch instruction in the monitor that instructs the
processor to continue execution at the start of the user program. The processor will then execute the
instructions in the user program until it encounters an ending or error condition.
When the job is completed, it returns control to the monitor, which immediately reads in the next job. The
results of each job are sent to an output device, such as a printer, for delivery to the user.
The monitor performs a scheduling function: A batch of jobs is queued up, and jobs are executed as rapidly as
possible, with no intervening idle time.
With each job, instructions are included in a form of job control language (JCL) which are denoted by the
beginning $. This is a special type of programming language used to provide instructions to the monitor.
The overall format of the job is given as
The hardware features that are added as a part of simple batch systems include,
i) Memory protection
ii) Timer
iii) Privileged instructions
iv) Interrupts.
The memory protection leads to the concept of dual mode operation.
User Mode
Kernel Mode.
Thus the simple batch system improves utilization of the computer
In uniprogramming we will have a single program in the main memory. The processor spends a certain amount
of time executing, until it reaches an I/O instruction. It must then wait until that I/O instruction concludes
before proceeding. This inefficiency is not necessary.
In Multiprogramming we will have OS and more user programs. When one job needs to wait for I/O, the
processor can switch to the other job, which is likely not waiting for I/O.This approach is known as
multiprogramming, or multitasking.
5
The most notable feature that is useful for multiprogramming is the hardware that supports I/O interrupts and
DMA (direct memory access).
With interrupt-driven I/O or DMA, the processor can issue an I/O command for one job and proceed with the
execution of another job while the I/O is carried out by the device controller.
When the I/O operation is complete, the processor is interrupted and control is passed to an interrupt-handling
program in the OS.The OS will then passes control to another job.
Multiprogramming operating systems are fairly sophisticated compared to single-program, or uniprogramming,
systems. To have several jobs ready to run, they must be kept in main memory, requiring some form of
memory management.
In addition, if several jobs are ready to run, the processor must decide which one to run, this decision requires
an algorithm for scheduling.
Time-Sharing Systems:
In time sharing systems the processor time is shared among multiple users.
In a time-sharing system, multiple users simultaneously access the system through terminals, with the OS
interleaving the execution of each user program in a short burst or quantum of computation.
If there are n users actively requesting service at one time, each user will only see on the average 1/n of the
effective computer capacity.
One of the first time-sharing operating systems to be developed was the Compatible Time-Sharing System
(CTSS)
The system ran on a computer with 32,000 36-bit words of main memory, with the resident monitor consuming
5000 of that. When control was to be assigned to an interactive user, the user’s program and data were loaded
into the remaining 27,000 words of main memory.
A program was always loaded to start at the location of the 5000th word
A system clock generated interrupts at a rate of approximately one every 0.2 seconds.
At each clock interrupt, the OS regained control and could assign the processor to another user. This technique
is known as time slicing.
Example: Assume that there are four interactive users with the following memory requirements, in words:
JOB1: 15,000
JOB2: 20,000
JOB3: 5000
JOB4: 10,000
6
i) Initially, the monitor loads JOB1 and transfers control to it.
ii) Later, the monitor decides to transfer control to JOB2. Because JOB2 requires more memory than JOB1,
JOB1 must be written out first, and then JOB2 can be loaded.
iii) Next, JOB3 is loaded in to be run. However, because JOB3 is smaller than JOB2, a portion of
JOB2 can remain in memory, reducing disk write time.
iv) Later, the monitor decides to transfer control back to JOB1.An additional portion of JOB2 must be written
out when JOB1 is loaded back into memory.
v) When JOB4 is loaded, part of JOB1 and the portion of JOB2 remaining in memory are retained.
vi) At this point, if either JOB1 or JOB2 is activated, only a partial load will be required. In this example, it
is JOB2 that runs next. This requires that JOB4 and the remaining resident portion of JOB1 be written out and
that the missing portion of JOB2 be read in.
The bootstrap loader It initializes all aspects of the system, from CPU registers to device controllers to memory
contents.
The bootstrap program loads the operating system and start executing that system.
Once the kernel is loaded and executing, it can start providing services to the system and its users. When is the
system is booted it waits for some event to occur.
The occurrence of an event is usually signaled by an interrupt from either the hardware or the software.
When the CPU is interrupted, it stops what it is doing and immediately transfers execution to a fixed location.
That contains the starting address of the service routine for the interrupt.
The interrupt service routine executes; on completion, the CPU resumes the interrupted computation.
Storage structure:
The CPU can load instructions only from memory, so any programs to run must be stored in main memory.
Main memory commonly is implemented in a semiconductor technology called dynamic random-access
memory
ROM is a read only memory that is used to store the static programs such as bootstrap loader.
All forms of memory provide an array of bytes. Each byte has its own address. The operations are done
through load or store instructions.
The load instruction moves a byte or word from main memory to an internal register within the CPU, whereas
the store instruction moves the content of a register to main memory.
Ideally, we want the programs and data to reside in main memory permanently.
This arrangement usually is not possible for the following two reasons
i) Main memory is usually too small to store all needed programs and data permanently
ii) Main memory is a volatile storage device that loses its contents when power is turned off or otherwise lost.
Most computer systems provide secondary storage as an extension of main memory. The main requirement
for secondary storage is that it be able to hold large quantities of data permanently.
The wide variety of storage systems can be organized in a hierarchy according to speed and cost.
The higher levels are expensive, but they are fast. As we move down the hierarchy, the cost per bit generally
decreases, whereas the access time generally increases
Volatile storage loses its contents when the power to the device is removed so that the data must be written to
nonvolatile storage for safekeeping.
Caches can be installed to improve performance where a large difference in access time or transfer rate exists
between two components.
7
I/O Structure:
A large portion of operating system code is dedicated to managing I/O, both because of its importance
to the reliabilityand performance of a system.
8
VIRTUALIZATION
Virtualization is a technology that allows operating systems to run as applications within other
operating systems.
With virtualization, in contrast, an operating system that is natively compiled for a particular
CPU architecture runs within another operating system also native to that CPU.
Virtualization first came about on IBM mainframes as a method for multiple users to run tasks
concurrently.
The virtual machine manager (VMM) (also known as a hypervisor) creates and runs virtual
machines by providing an interface that is identical to the host. Hypervisors make virtualization
9
possible by translating requests between the physical and virtual resources.
Virtual machines allow to run an operating system on your desktop that behaves like a full,
separate computer.
Running multiple virtual machines allowed many users to run tasks on a system designed for a
single user.
Later, VMware created a new virtualization technology in the form of an application that ran on
XP.
That application ran one or more guest copies of Windows or other native x86 operating
systems, each running its own applications.
Windows was the host operating system, and the VMware application was the virtual machine
manager VMM.
The VMM runs the guest operating systems, manages their resource use, and protects each guest
from the others.
On laptops and desktops, a VMM allows the user to install multiple operating systems for
exploration or to run applications written for operating systems other than the native host.
Companies writing software for multiple operating systems can use virtualization to run all of
those operating systems on a single physical server for development, testing, and debugging.
Within data centers,
Virtualization has become a common method of executing and managing computing
environments.
Virtualization Requirements
Fidelity - A VMM provides an environment for programs that is essentially identical to the
original machine.
Performance - Programs running within that environment show only minor performance
decreases.
Safety - The VMM is in complete control of system resources.
Features and Benefits
The ability to share the same hardware yet run several different execution environments (that is,
different operating systems) concurrently.
The ability to freeze, or suspend, a running virtual machine.
The host system is protected from the virtual machines, just as the virtual machines are
protected from each other.
Running multiple OS allows for rapid porting and testing of programs in varying environments.
resource optimization - since many lightly used systems can be combined to create one more
heavily used system.
Virtualization can improve not only resource utilization but also resource management.
Cloud computing, is made possible by virtualization
Building blocks
VMMs use several techniques to implement virtualization, including trap-and-emulate and
binary translation.
Trap-and-emulate
When the kernel in the guest attempts to execute a privileged instruction, that is an error
10
(because the system is in user mode) and causes a trap to the VMM in the real machine. The
VMM gains control and executes (or “emulates”) the action that was attempted by the guest
kernel on the part of the guest. It then returns control to the virtual machine. This is called the
trap-and-emulate method
Privileged instructions create extra overhead, however, causing the guest to run more slowly
than it would natively.
Binary Translation
This technique uses a combination of binary translation for handling privileged and sensitive
instructions, and direct execution techniques for user-level instructions.
The basic steps are as follows:
i) If the guest Virtual CPU is in user mode, the guest can run its instructions natively on a
physical CPU.
ii)If the guest Virtual CPU is in kernel mode, then the guest believes that it is running in kernel
mode. The VMM examines every instruction the guest executes in virtual kernel mode by
reading the next few instructions that the guest is going to execute, based on the guest’s
program counter. Instructions other than special instructions are run natively. Special
instructions are translated into a new set of instructions that perform the equivalent task—for
example changing the flags in the VCPU.
Hardware Assistance
Without some level of hardware support, virtualization would be impossible. The more
hardware support available within a system, the more feature-rich and stable the virtual
machines can be and the better they can perform.
Virtual machine control structures (VMCSs) are provided to manage guest and host state.
I/O and interrupts are another areas improved by hardware assistance.
Techniques used to implement virtualization
Type 0 Hypervisor
It is implemented in the hardware.
The VMM itself is encoded in the firmware and loaded at boot time. In turn, it loads the guest
images to run in each partition.
A type 0 hypervisor can run multiple guest operating systems (one in each hardware partition).
Type 1 Hypervisor
Data-center operating system.
They are special-purpose operating systems that run natively on the hardware, they create, run,
and manage guest operating systems.
In addition to running on standard hardware, they can run on type 0 hypervisors, but not on other
type 1 hypervisors.
Type 2 Hypervisor
This type of VMM is simply another process run and managed by the host, and even the host
does not know virtualization is happening within the VMM.
They run on a variety of general-purpose operating systems, and running them requires no
changes to the host operating system.
Paravirtualization
11
Paravirtualization presents the guest with a system that is similar but not identical to the guest’s
preferred system.
The guest must be modified to run on the paravirtualized virtual hardware.
The gain for this extra work is more efficient use of resources and a smaller virtualization layer.
Programming-Environment Virtualization
Programming language is designed to run within a custom-built virtualized environment.
We can define a virtual environment, based on APIs, that provides a set of features that we want
to have available for a particular language and programs written in that language.
Emulation
Emulation is useful when the host system has one system architecture and the guest system was
compiled for a different architecture.
Emulation is used when the source CPU type is different from the target CPU type.. Emulation
comes at a heavy price, however. Every machine-level instruction that runs natively on the
source system must be translated to the equivalent function on the target system, frequently
resulting in several target instructions.
Simple structure:
The Simple structured operating systems do not have a well-defined structure. These systems will be
simple, small andlimited systems.
Example: MS-DOS.
In MS-DOS, the interfaces and levels of functionality are not well separated.
In MS-DOS application programs are able to access the basic I/O routines. This causes the entire
systems to becrashed when user programs fail.
12
Example: Traditional UNIX OS
It consists of two separable parts: the kernel and the system programs.
The kernel is further separated into a series of interfaces and device drivers
The kernel provides the file system, CPU scheduling, memory management, and other
operating-systemfunctions through system calls.
Layered approach:
A system can be made modular in many ways. One method is the layered approach, in which
the operating system is broken into a number of layers (levels). The bottom layer (layer 0) is
the hardware; the highest (layer N) is the user interface.
13
Microkernels:
In the mid-1980s, researchers at Carnegie Mellon University developed an operating system called
Mach thatmodularized the kernel using the microkernel approach.
This method structures the operating system by removing all nonessential components from the
kernel andimplementing them as system and user-level programs.
Modules:
The best current methodology for operating-system design involves using loadable kernel
modules
The kernel has a set of core components and links in additional services via modules, either
at boot time or during run time.
The kernel provides core services while other services are implemented dynamically, as the
kernel is running.
Linking services dynamically is more comfortable than adding new features directly to
the kernel, whichwould require recompiling the kernel every time a change was made.
Example: Solaris OS
The Solaris operating system structure is organized around a core kernel with seven types of loadable
kernel modules:
Scheduling classes
File systems
Loadable system calls
14
Executable formats
STREAMS modules
Miscellaneous
Device and bus drivers
Hybrid Systems:
The Operating System combines different structures, resulting in hybrid systems that
address performance, security, and usability issues.
They are monolithic, because having the operating system in a single address space
provides very efficientperformance.
However, they are also modular, so that new functionality can be dynamically added to the
kernel.
Example: Linux and Solaris are monolithic (simple) and also modular, IOS.
Apple IOS Structure
A bit, called the mode bit, is added to the hardware of the computer to indicate the current
mode: kernel (0) or user (1)
The mode bit, can distinguish between a task that is executed on behalf of the operating
system and one that is executed on behalf of the user.
15
When the computer system is executing on behalf of a user application, the system is in user
mode and when a user application requests a service from the operating system the system
must make a transition from user to kernel mode
At system boot time, the hardware starts in kernel mode. The operating system is then
loaded and starts userapplications in user mode.
Whenever a trap or interrupt occurs, the hardware switches from user mode to kernel mode
(that is, changesthe state of the mode bit to 0).
The dual mode of operation provides us with the means for protecting the operating system
from errant users—and errant users from one another
The hardware allows privileged instructions to be executed only in kernel mode. If an attempt
is made to execute a privileged instruction in user mode, the hardware does not execute the
instruction but rather treats it as illegal and traps it to the operating system
System calls provide the means for a user program to ask the operating system to perform
tasks reserved for the operating system on the user program’s behalf.
The lack of a hardware-supported dual mode can cause serious shortcomings in an operating
system.MS-DOS was written for the Intel 8088 architecture, which has no mode bit and
therefore no dual mode
Once hardware protection is in place, it detects errors that violate modes. These errors are
normally handled bythe operating system.
Timer:
The operating system maintains control over the CPU. We cannot allow a user program to
get stuck in aninfinite loop or to fail to call system services and never return control to the
operating system.
A timer can be set to interrupt the computer after a specified period. A variable timer is
generallyimplemented by a fixed-rate clock and a counter.
The operating system sets the counter. Every time the clock ticks, the counter is
decremented. When thecounter reaches 0, an interrupt occurs
SYSTEM CALLS:
Systems execute thousands of system calls per second. Application developers design
programs according toan application programming interface (API).
For most programming languages, the Application Program Interface provides a system call
interface thatserves as the link to system calls made available by the operating system
The system-call interface intercepts function calls in the API and invokes the necessary
16
system calls within theOperating system.
Example: System calls for writing a simple program to read data from one file and copy them to
another file
The caller of the system call need know nothing about how the system call is implemented
or what it does during execution.
The caller need only obey the API and understand what the operating system will do as a
result of the execution of that system call.
Three general methods are used to pass parameters to the operating system
17
parameters are generally stored in a block, or table, in memory, and the address of the
block is passed as a parameter in a register
Parameters also can be placed, or pushed, onto the stack by the program and popped off
the stack by theoperating system.
PROCESS CONTROL:
A Running program needs to be able to halt its execution either normally (end ()) or abnormally
(abort()).
Under either normal or abnormal circumstances, the operating system must transfer control to
the invoking command interpreter.
A process or job executing one program may want to load() and execute() another program.
This feature allows the command interpreter to execute a program as directed by, for example,
a user command, the click of a mouse, or a batch command.
If we create a new job or process, or perhaps even a set of jobs or processes, we should be able
to control its execution that requires to determine and reset the attributes of a job or process,
including the job’s priority, its maximum allowable execution time, and so on (get process
attributes() and set process attributes()).
We may also want to terminate a job or process that we created (terminate process()) if we
find that it is incorrect or is no longer needed.
The System calls associated with process control includes
end, abort
load, execute
18
create process, terminate process
get process attributes, set process attributes
Wait for time
wait event, signal event
allocate and free memory
When a process has been created We may want to wait for a certain amount of time to pass
(wait time()) or wewill want to wait for a specific event to occur (wait event()).
The jobs or processes should then signal when that event has occurred (signal event()
To start a new process, the shell executes a fork() system call. Then, the selected
program is loaded intomemory via an exec() system call, and the program is executed
When the process is done, it executes an exit() system call to terminate, returning to the
invoking process a status code of 0 or a nonzero error code.
FILE MANAGEMENT:
In order to work with files We first need to be able to create () and delete () files. Either
system call requires the name of the file and perhaps some of the file’s attributes. Once the file
is created, we need to open() it and to use it.
We may also read (), write (), or reposition ().Finally, we need to close () the file,
indicating that we are nolonger using it.
In addition, for either files or directories, we need to be able to determine the values of
various attributes andperhaps to reset them if necessary.
File attributes include the file name, file type, protection codes, accounting information, and so
on. At least twosystem calls, get file attributes () and set file attributes (), are required for this
function.
The System calls associated with File management includes
File management
create file, delete file
open, close
read, write, reposition
DEVICE MANAGEMENT:
A process may need several resources to execute—main memory, disk drives, access to files,
and so on. If the resources are available, they can be granted, and control can be returned to the
user process. Otherwise, the process will have to wait until sufficient resources are available.
A system with multiple users may require us to first request() a device, to ensure exclusive use
of it.
After we are finished with the device, we release() it. These functions are similar to the
open() and close()system calls for files.
Once the device has been requested (and allocated to us), we can read(), write(), and (possibly)
reposition() thedevice, just as we can with files.
I/O devices are identified by special file names, directory placement, or file attributes.
The System calls associated with Device management includes
request device, release device
read, write, reposition
get device attributes, set device attributes
logically attach or detach devices
INFORMATION MAINTENANCE:
Many system calls exist simply for the purpose of transferring information between the user
program and theoperating system.
19
Example, most systems have a system call to return the current time() and date().
Other system calls may return information about the system, such as the number of current
users, the versionnumber of the operating system, the amount of free memory or disk space, and
so on.
Many systems provide system calls to dump() memory. This provision is useful for debugging.
Many operating systems provide a time profile of a program to indicate the amount of time
that the programexecutes at a particular location or set of locations.
The operating system keeps information about all its processes, and system calls are used to access
this information.
Generally, calls are also used to reset the process information (get process attributes() and set process
attributes()).
COMMUNICATION:
There are two common models of Interprocess communication: the message passing model
and the shared- memory model.
In the message-passing model, the communicating processes exchange messages with one
another to transfer information.
Messages can be exchanged between the processes either directly or indirectly through a
common mailbox.
Each process has a process name, and this name is translated into an identifier by which the
operating systemcan refer to the process. The get hostid() and get processid() system calls do
this translation.
The recipient process usually must give its permission for communication to take place
with an acceptconnection () call.
The source of the communication, known as the client, and the receiving daemon, known
as a server, thenexchange messages by using read message() and write message() system
calls.
The close connection() call terminates the communication
In the shared-memory model, processes use shared memory create() and shared memory
attach() system callsto create and gain access to regions of memory owned by other processes.
The system calls associated with communication includes,
create, delete communication connection
send, receive messages
20
with a variety ofperipheral configurations.
The Computer system must then be configured or generated for each specific computer
site, a processsometimes known as system generation SYSGEN.
The operating system is normally distributed on disk, on CD-ROM or DVD-ROM, or as
an ―ISO‖ image, which is a file in the format of a CD-ROM or DVD-ROM.
To generate a system, the special program called SYSGEN program reads from a given
file, or asks theoperator of the system for information concerning the specific configuration
of the hardware system.
The following kinds of information must be determined.
What CPU is to be used?
How will the boot disk be formatted?
How much memory is available?
What devices are available?
What operating-system options are desired, or what parameter values are to be used?
A system administrator can use this information to modify a copy of the source code ofthe
operating system. The operating system then is completely compiled.
The system description can lead to the creation of tables and the selection of modules
from a precompiledlibrary. These modules are linked together to form the generated operating
system
It is also possible to construct a system that is completely table driven. All the code is
always part of thesystem, and selection occurs at execution time, rather than at compile or
link time.
SYSTEM BOOT:
The procedure of starting a computer by loading the kernel is known as booting the system.
A small piece of code known as the bootstrap program or bootstrap loader locates the
kernel,loads it intomain memory, and starts its execution.
In some computer systems system booting is a two step process.
First a simple bootstrap loader fetches a more complex boot program from disk
A complex boot program loads the OS
The bootstrap program can perform a variety of tasks. Usually, one task is to run diagnostics
to determine thestate of the machine.
It can also initialize all aspects of the system, from CPU registers to device controllers and the
contents of mainmemory and then it starts the Operating system.
All forms of ROM are also known as firmware, since their characteristics fall somewhere
between those of hardware and those of software.
A problem with firmware in general is that executing code there is slower than executing code in
RAM.
Some systems store the operating system in firmware and copy it to RAM for fast execution.
A final issue with firmware is that it is relatively expensive, so usually only small amounts are
available.
For large operating systems the bootstrap loader is stored in firmware, and the operating system
is on disk.
The Bootstrap program has a piece of code that can read a single block at a fixed
location from disk intomemory and execute the code from that Boot block.
The program stored in the boot block may be sophisticated enough to load the entire
21
operating system intomemory and begin its execution.
A disk that has a Boot partition is called as a Boot Disk.
GRUB is an example of an open-source bootstrap program for Linux systems.
PROCESS CONCEPT
A process can be thought of as a program in execution.
A process is the unit of work in a modern time-sharing system.
A process generally includes the process stack, which contains temporary
data (such as method parameters, return addresses, and local variables),
and a data section, which contains global variables.
Difference between program and process
A program is a passive entity, such as the contents of a file stored on
disk, whereas a process is an active entity, with a program counter
specifying the next instruction to execute and a set of associated resources.
Process States:
As a process executes, it changes state.
The state of a process is defined in part by the current activity of that process.
Each process may be in one of the following states:
1. New: The process is being created.
2. Running: Instructions are being executed.
3. Waiting: The process is waiting for some event to occur
(such as an I/Ocompletion or reception of a signal).
4. Ready: The process is waiting to be assigned to a processor.
5. Terminated: The process has finished execution.
22
It contains the entire information about a process.
Some of the information a PCB contains are:
1. Process state: The state may be new, ready, running,
waiting or terminated.
2. Program counter: The counter indicates the address of the
next instruction to be executed for this process.
3. CPU registers: The registers vary in number and type,
depending on the computer architecture.
4. CPU-scheduling information: This information includes a
process priority, pointers to scheduling queues, and any
other scheduling parameters.
5. Memory-management information: This information
may include such information as the value of the base and
limit registers, the page tables, or the segment tables,
depending on the memory system used by the operating
system.
6. Accounting information: This information includes the
amount of CPU and real time used, time limits, account
numbers, job or process numbers, and so on.
7. Status information: The information includes the list of
I/O devices allocated to this process, a list of open files, and
so on.
THREADS - OVERVIEW
A thread is the basic unit of CPU utilization.
It is sometimes called as a lightweight process.
It consists of a thread ID ,a program counter, a register set and a stack.
It shares with other threads belonging to the same process its code section ,
23
data section, andresources such as open files and signals.
24
Tru64 UNIXsupport kernel threads.
MULTITHREADING MODELS
1. Many-to-One
2. One-to-One
3. Many-to-Many
1. Many-to-One:
Many user-level threads mapped to single kernel thread.
Used on systems that does not support kernel threads.
Many-to-One Model
2.One-to-One:
Each user-level thread maps to a kernel thread.
Examples
- Windows 95/98/NT/2000
- OS/2
One-to-one Model
3.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 2
Windows NT/2000
Many-to-Many Model
25
Threading Issues:
1. fork() and exec() system calls.
A fork() system call may duplicate all threads or duplicate only the
thread that invokedfork().
If a thread invoke exec() system call ,the program specified in the
parameter to exec willreplace the entire process.
2. Thread cancellation.
It is the task of terminating a thread before it
has completed. A thread that is to be
cancelled is called a target thread.
There are two types of cancellation namely
1. Asynchronous Cancellation – One thread immediately terminates the target
thread.
2. Deferred Cancellation – The target thread can periodically check if
it should terminate, and does so in an orderly fashion.
3. Signal handling
1. A signal is a used to notify a process that a particular event has occurred.
2. A generated signal is delivered to the process.
a. Deliver the signal to the thread to which the signal applies.
b. Deliver the signal to every thread in the process.
c. Deliver the signal to certain threads in the process.
d. Assign a specific thread to receive all signals for the process.
3. Once delivered the signal must be handled.
a. Signal is handled by
i. A default signal handler
ii. A user defined signal handler
4. Thread pools
Creation of unlimited threads exhausts system resources such as CPU
time or memory. Hence we use a thread pool.
In a thread pool, a number of threads are created at process startup and placed
in the pool. When there is a need for a thread the process will pick a thread
from the pool and assign it atask. After completion of the task, the thread is
returned to the pool.
5. Thread specific data
26
Threads belonging to a process share the data of the process. However each
thread might need its own copy of certain data known as thread-specific data.
MULTI-CORE PROGRAMMING
A concurrent system supports more than one task by allowing all the tasks to make
progress.
A system is parallel if it can perform more than one task simultaneously.
Before the advent of SMP and multi-core architectures, most computer
systems had only asingle processor.
CPU schedulers were designed to provide the illusion of parallelism by
rapidly switching between processes. Such processes were running
concurrently but not in parallel.
Types of Parallelism
In general, there are two types of parallelism: Data parallelism and Task parallelism.
Data parallelism focuses on distributing subsets of the same data across
multiple computingcores and performing the same operation on each core.
27
Example: In a dual core system, summing the contents of an array of size N
.On a single-core system, one thread would simply sum the elements [0] . . . [N
− 1]. On a dual-core system, however, thread A, running on core 0, could sum
the elements [0] . . . [N/2 − 1] while thread B, running on core 1, could sum the
elements [N/2] ...[N − 1]. The two threads would be running in parallel on
separate computing cores.
Task parallelism involves distributing not data but tasks (threads) across
multiple computing cores. Each thread is performing a unique operation.
Different threads may be operating on the same data, or they may be operating
on different data.
Windows Threads:
Windows implements the Windows API, which is the primary API for
the family ofMicrosoft operating systems (Windows 98, NT, 2000, and XP,
as well as Windows 7).
A Windows application runs as a separate process, and each process may
contain one ormore threads.
The general components of a thread include:
28
thread is running in kernel mode) and a pointer to the TEB.
The ETHREAD and the KTHREAD exist entirely in kernel space; this means
that only the kernel can access them. The TEB is a user-space data structure
that is accessed when the thread is running in user mode. Among other fields,
the TEB contains the thread identifier, a user-mode stack, and an array for
thread-local storage.
29