Os-Module I: What Is An Operating System?
Os-Module I: What Is An Operating System?
A program that acts as an intermediary between a user of a computer and the computer hardware
Process Management
4
A process is a program in execution. It is a unit of work within the system. Program is a passive
entity, process is an active entity.
Process needs resources to accomplish its task
CPU, memory, I/O, files
Initialization data
Process termination requires reclaim of any reusable resources
Single-threaded process has one program counter specifying location of next instruction to
execute
Process executes instructions sequentially, one at a time, until completion
Multi-threaded process has one program counter per thread
Typically system has many processes, some user, some operating system running concurrently on
one or more CPUs
Concurrency by multiplexing the CPUs among the processes / threads
The operating system is responsible for the following activities in connection with process
management:
Creating and deleting both user and system processes
Suspending and resuming processes
Providing mechanisms for process synchronization
Providing mechanisms for process communication
Providing mechanisms for deadlock handling
Memory Management
OS activities include
5
Mapping files onto secondary storage
Backup files onto stable (non-volatile) storage media
Mass-Storage Management
Usually disks used to store data that does not fit in main memory or data that must be kept for a
“long” period of time
Proper management is of central importance
Entire speed of computer operation hinges on disk subsystem and its algorithms
Free-space management
Storage allocation
Disk scheduling
Some storage need not be fast
Tertiary storage includes optical storage, magnetic tape
Still must be managed
Varies between WORM (write-once, read-many-times) and RW (read-write)
Many commercial systems do not have well-defined structures. Frequently, such operating systems started
as small, simple, and limited systems and then grew beyond their original scope. MS-DOS is an example
of such a system.
It was written to provide the most functionality in the least space, so it was not divided into modules
carefully. In MS-DOS, the interfaces and levels of functionality are not well separated. For instance,
application programs are able to access the basic I/O routines to write directly to the display and disk drives.
Such freedom leaves MS-DOS vulnerable to errant (or malicious) programs, causing entire system crashes
when user programs fail. Of course, MS-DOS was also limited by the hardware of its era. Another example
of limited structuring is the original UNIX operating system. UNIX is another system that initially was
limited by hardware functionality.
6
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, which have been added and expanded over the years as UNIX has
evolved.
Layered Approach
The operating system can then retain much greater control over the computer and over the applications that
make use of that computer. Implementers have more freedom in changing the inner workings of the system
and in creating modular operating systems. Under the top down approach, the overall functionality and
features are determined and are separated into components. Information hiding is also important, because
it leaves programmers free to implement the low-level routines as they see fit, provided that the external
interface of the routine stays unchanged and that the routine itself performs the advertised task.
A system can be made modular in many ways. One method is the layered approach, in which the
operating system is broken up into a number of layers (levels). The bottom layer (layer 0) is the hardware;
the highest (layer N) is the user interface.
An operating-system layer is an implementation of an abstract object made up of data and the operations
that can manipulate those data. A typical operating-system layer—say, layer M—consists of data structures
and a set of routines that can be invoked by higher-level layers. Layer M, in turn, can invoke operations on
lower-level layers.
The main advantage of the layered approach is simplicity of construction and debugging. The layers
are selected so that each uses functions (operations) and services of only lower-level layers. This
7
Approach simplifies debugging and system verification. The first layer can be debugged without any
concern for the rest of the system, because, by definition, it uses only the basic hardware (which is assumed
correct) to implement its functions. Once the first layer is debugged, its correct functioning can be assumed
while the second layer is debugged, and so on. If an error is found during the debugging of a particular
layer, the error must be on that layer, because the layers below it are already debugged. Thus, the design
and implementation of the system is simplified.
Each layer is implemented with only those operations provided by lower level layers. A layer does
not need to know how these operations are implemented; it needs to know only what these operations do.
Hence, each layer hides the existence of certain data structures, operations, and hardware from higher-level
layers.
The major difficulty with the layered approach involves appropriately defining the various layers.
The backing-store driver would normally be above the CPU scheduler, because the driver may need to wait
for I/O and the CPU can be rescheduled during this time. A final problem with layered implementations is
that they tend to be less efficient than other types. For instance, when a user program executes an I/O
operation, it executes a system call that is trapped to the I/O layer, which calls the memory-management
layer, which in turn calls the CPU-scheduling layer, which is then passed to the hardware.
Micro kernels
The kernel became large and difficult to manage. In the mid-1980s, researchers at Carnegie Mellon
University developed an operating system called Mach that modularized the kernel using the microkernel
approach. This method structures the operating system by removing all nonessential components from the
kernel and implementing them as system and user-level programs. The result is a smaller kernel. Micro
kernels provide minimal process and memory management, in addition to a communication facility.
The main function of the microkernel is to provide a communication facility between the client
program and the various services that are also running in user space. One benefit of the microkernel
approach is ease of extending the operating system. All new services are added to user space and
consequently do not require modification of the kernel. When the kernel does have to be modified, the
changes tend to be fewer, because the microkernel is a smaller kernel.
The resulting operating system is easier to port from one hardware design to another. The
microkernel also provides more security and reliability, since most services are running as user rather than
kernel processes. If a service fails, the rest of the operating system remains untouched.
Modules
The best current methodology for operating-system design involves using object-oriented programming
techniques to create a modular kernel. Here, the kernel has a set of core components and dynamically links
in additional services either during boot time or during run time. Such a strategy uses dynamically loadable
modules and is common in modern implementations of UNIX, such as Solaris, Linux, and Mac OS X.
1. Scheduling classes
2. File systems
3. Loadable system calls
4. Executable formats
5. STREAMS modules
8
6. Miscellaneous
7. Device and bus drivers
Such a design allows the kernel to provide core services yet also allows certain features to be implemented
dynamically. The overall result resembles a layered system in that each kernel section has defined, protected
interfaces; but it is more flexible than a layered system in that any module can call any other module. The
approach is like the microkernel approach in that the primary module has only core functions and
knowledge of how to load and communicate with other modules; but it is more efficient, because modules
do not need to invoke message passing in order to communicate.
The Apple Macintosh Mac OS X operating system uses a hybrid structure. Mac OS X (also known as
Danvin) structures the operating system using a layered technique where one layer consists of the Mach
microkernel. The top layers include application environments and a set of services providing a graphical
interface to applications. Below these layers is the kernel environment, which consists primarily of the
Mach microkernel and the BSD kernel. Mach provides memory management; support for remote procedure
calls (RPCs) and inter process communication (IPC) facilities, including message passing; and thread
scheduling. The BSD component provides a BSD command line interface, support for networking and file
systems, and an implementation of POSIX APIs, including Pthreads.
9
Dual-Mode Operation
Timer to prevent infinite loop / process hogging resources Set interrupt after specific period
Operating system decrements counter
When counter zero generate an interrupt
Set up before scheduling process to regain control or terminate program that exceeds allotted time
If a computer system has multiple users and allows the concurrent execution of multiple processes, then
access to data must be regulated. For that purpose, mechanisms ensure that files, memory segments, CPU,
and other resources can be operated on by only those processes that have gained proper authorization from
the operating system.
1.1.4 Protection and security
Protection is any mechanism for controlling the access of processes or users to the resources defined by a
computer system. This mechanism must provide means for specification of the controls to be imposed and
means for enforcement.
Protection can improve reliability by detecting latent errors at the interfaces between component
subsystems. Early detection of interface errors can often prevent contamination of a healthy subsystem by
another subsystem that is malfunctioning. An unprotected resource cannot defend against use (or misuse)
by an unauthorized or incompetent user. A protection-oriented system provides a means to distinguish
between authorized and unauthorized usage, A system can have adequate protection but still be prone to
failure and allow inappropriate access.
It is the job of security to defend a system from external and internal attacks. Such attacks spread
across a huge range and include viruses and worms, denial-of service attacks Protection and security require
the system to be able to distinguish among all its users. Most operating systems maintain a list of user names
and associated user identifiers (user IDs).
User ID then associated with all files, processes of that user to determine access control
10
Group identifier (group ID) allows set of users to be defined and controls managed, then also
associated with each process, file Privilege escalation allows user to change to effective ID with
more rights
Traditional Computing
As computing matures, the lines separating many of the traditional computing environments are
blurring. this environment consisted of PCs connected to a network, with servers providing file and print
services. Terminals attached to mainframes were prevalent at many companies as well, with even fewer
remote access and portability options.
The current trend is toward providing more ways to access these computing environments. Web
technologies are stretching the boundaries of traditional computing. Companies establish portals, which
provide web accessibility to their internal servers. Network computers are essentially terminals that
understand web-based computing. Handheld computers can synchronize with PCs to allow very portable
use of company information. Handheld PDAs can also connect to wireless networks to use the company's
web portal.
Batch system processed jobs in bulk, with predetermined input. Interactive systems waited for input
from users. To optimize the use of the computing resources, multiple users shared time on these systems.
Time-sharing systems used a timer and scheduling algorithms to rapidly cycle processes through the CPU,
giving each user a share of the resources.
Client-Server Computing
Designers have shifted away from centralized system architecture. Terminals connected to centralized
systems are now being supplanted by PCs. Correspondingly, user interface functionality once handled
directly by the centralized systems is increasingly being handled by the PCs. As a result, many of today’s
systems acts as server systems to satisfy requests generated by client systems Server systems can be
broadly categorized as compute servers and file servers:
• The compute-server system provides an interface to which a client can send a request to perform an
action (for example, read data); in response, the server executes the action and sends back results to the
11
client. A server running a database that responds to client requests for data is an example of such a system.
The file-server system provides a file-system interface where clients can create, update, read, and delete
files. An example of such a system is a web server that delivers files to clients running web browsers.
Peer-to-Peer Computing
In this model, clients and servers are not distinguished from one another; instead, all nodes within
the system are considered peers, and each may act as either a client or a server, depending on whether it is
requesting or providing a service. Peer-to-peer systems offer an advantage over traditional client-server
systems. In a client-server system, the server is a bottleneck; but in a peer-to-peer system, services can be
provided by several nodes distributed throughout the network.
To participate in a peer-to-peer system, a node must first join the network of peers. Once a node
has joined the network, it can begin providing services to—and requesting services from—other nodes in
the network.
Determining what services are available is accomplished in one of two general ways:
• When a node joins a network, it registers its service with a centralized lookup service on the
network. Any node desiring a specific service first contacts this centralized lookup service to determine
which node provides the service. The remainder of the communication takes place between the client and
the service provider.
• A peer acting as a client must first discover what node provides a desired service by broadcasting
a request for the service to all other nodes in the network. The node (or nodes) providing that service
responds to the peer making the request. To support this approach, a discovery protocol must be provided
that allows peers to discover services provided by other peers in the network.
Web-Based Computing
The Web has become ubiquitous, leading to more access by a wider variety of devices than was dreamt of
a few years ago. Web computing has increased the emphasis on networking. Devices that were not
previously networked now include wired or wireless access. Devices that were networked now have faster
network connectivity, provided by either improved networking technology, optimized network
implementation code, or both.
The implementation of web-based computing has given rise to new categories of devices, such as
load balancers, which distribute network connections among a pool of similar servers. Operating systems
like Windows 95, which acted as web clients, have evolved into Linux and Windows XP, which can act as
web servers as well as clients. Generally, the Web has increased the complexity of devices, because their
users require them to be web-enabled.
1.1.7 Open-Source Operating Systems
Operating systems made available in source-code format rather than just binary closed-source
Counter to the copy protection and Digital Rights Management (DRM) movement
12
Started by Free Software Foundation (FSF), which has “copy left” GNU Public License (GPL)
Examples include GNU/Linux, BSD UNIX (including core of Mac OS X), and Sun Solaris
One set of operating-system services provides functions that are helpful to the user
Communications – Processes may exchange information, on the same computer or between
computers over a network.
Communications may be via shared memory or through message passing (packets moved by the
OS)
Error detection – OS needs to be constantly aware of possible errors may occur in the CPU and
memory hardware, in I/O devices, in user program
For each type of error, OS should take the appropriate action to ensure correct and consistent
computing.
Debugging facilities can greatly enhance the user’s and programmer’s abilities to efficiently use
the system.
Another set of OS functions exists for ensuring the efficient operation of the system itself via
resource sharing
Resource allocation - When multiple users or multiple jobs running concurrently, resources must
be allocated to each of them
Many types of resources - Some (such as CPU cycles, main memory, and file storage) may have
special allocation code, others (such as I/O devices) may have general request and release code
Accounting - To keep track of which users use how much and what kinds of computer resources
Protection and security - The owners of information stored in a multiuser or networked computer
system may want to control use of that information, concurrent processes should not interfere with
each other.
Protection involves ensuring that all access to system resources is controlled.
Security of the system from outsiders requires user authentication, extends to defending external
I/O devices from invalid access attempts.
If a system is to be protected and secure, precautions must be instituted throughout it. A chain is
only as strong as its weakest link.
1.2.2 User and Operating System Interface - CLI
Command Line Interface (CLI) or command interpreter allows direct command entry Sometimes
implemented in kernel, sometimes by systems program
o Sometimes multiple flavors implemented – shells
o Primarily fetches a command from user and executes it
Sometimes commands built-in, sometimes just names of programs If the latter, adding new
features doesn’t require shell modification
13
Invented at Xerox PARC
Many systems now include both CLI and GUI interfaces
Microsoft Windows is GUI with CLI “command” shell
Apple Mac OS X as “Aqua” GUI interface with UNIX kernel underneath and shells available
Solaris is CLI with optional GUI interfaces (Java Desktop, KDE)
14
Why use APIs rather than system calls?(Note that the system-call names used throughout this text
are generic)
15
System Call Parameter Passing
Often, more information is required than simply identity of desired system call
Exact type and amount of information vary according to OS and call
Three general methods used to pass parameters to the OS
Simplest: pass the parameters in registers
In some cases, may be more parameters than registers
Parameters stored in a block, or table, in memory, and address of block passed as a parameter in a
register
Parameters placed, or pushed, onto the stack by the program and popped off the stack by the
operating system
Block and stack methods do not limit the number or length of parameters being passed
16
1.2.4 Types of System Calls
Process control
File management
Device management
Information maintenance
Communications
Protection
Process Control
A running program needs to be able to halt its execution either normally (end) or abnormally (abort). If a
system call is made to terminate the currently running program abnormally, or if the program runs into a
problem and causes an error trap, a dump of memory is sometimes taken and an error message generated.
The dump is written to disk and may be examined by a debugger—a system program designed to aid the
programmer in finding and correcting bugs-—to determine the cause of the problem. Under either normal
or abnormal circumstances, the operating system must transfer control to the invoking command interpreter.
The command interpreter then reads the next command. In an interactive system, the command interpreter
simply continues with the next command; it is assumed that the user will issue an appropriate
command to respond to any error.
File Management
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 (rewinding or skipping to the end of the file, for example). Finally, we need
to close the file, indicating that we are no longer using it. We may need these same sets of operations for
directories if we have a directory structure for organizing files in the file system. In addition, for either files
or directories, we need to be able to determine the values of various attributes and perhaps to reset them if
necessary. File attributes include the file name, a file type, protection codes, accounting information, and
so on.
At least two system calls, get file attribute and set file attribute, are required for this function.
Some operating systems provide many more calls, such as calls for file move and copy.
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. The various resources
controlled by the operating system can be thought of as devices. Some of these devices are physical devices
(for example, tapes), while others can be thought of as abstract or virtual devices (for example, files). If
there are multiple users of the system, the system may require us to first request the 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
17
close system calls for files.
Information Maintenance
Many system calls exist simply for the purpose of transferring information between the user
program and the operating system. For example, most systems have a system call to return the current t I
m e and date . Other system calls may return information about the system, such as the number of current
users, the version number of the operating system, the amount of free memory or disk space, and so on.
In addition, 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 inter process 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. Before communication can take place, a connection
must be opened. The name of the other communicator must be known, be it another process on the same
system or a process on another computer connected by a communications network. Each computer in a
network has a host name by which it is commonly known. A host also has a network identifier, such as an
IP address. Similarly, each process has a process name, and this name is translated into an identifier by
which the operating system can refer to the process. The get host id and get processid system calls do this
translation. The identifiers are then passed to the general purpose open and close calls provided by the file
system or to specific open connection and close connection system calls, depending on the system's model
of communication.
In the shared-memory model, processes use shared memory creates and shared memory attaches
system calls to create and gain access to regions of memory owned by other processes. Recall that, normally,
the operating system tries to prevent one process from accessing another process's memory. Shared memory
requires that two or more processes agree to remove this restriction.
They can then exchange information by reading and writing data in the shared areas. The form of
the data and the location are determined by the processes and are not under the operating system's control.
The processes are also responsible for ensuring that they are not writing to the same location
simultaneously.
At the lowest level is hardware. Next are the operating system, then the system programs, and finally the
application programs. System programs provide a convenient environment for program development and
execution. Some of them are simply user interfaces to system calls; others are considerably more complex.
They can be divided into these categories:
• File management. These programs create, delete, copy, rename, print, dump, list, and generally
manipulate files and directories.
• Status information. Some programs simply ask the system for the date, time, amount of available
memory or disk space, number of users, or similar status information. Others are more complex, providing
detailed
18
performance, logging, and debugging information. Typically, these programs format and print the output
to the terminal or other output devices or files or display it in a window of the GUI. Some systems also
support a registry, which is used to store and retrieve configuration information.
• File modification. Several text editors may be available to create and modify the content of files stored
on disk or other storage devices. There may also be special commands to search contents of files or perform
transformations of the text.
• Programming-language support. Compilers, assemblers, debuggers and interpreters for common
programming languages (such as C, C++, Java, Visual Basic, and PERL) are often provided to the user
with the operating system.
• Program loading and execution. Once a program is assembled or compiled, it must be loaded into
memory to be executed. The system may provide absolute loaders, relocatable loaders, linkage editors, and
overlay loaders. Debugging systems for either higher-level languages or machine language are needed as
well.
• Communications. These programs provide the mechanism for creating virtual connections among
processes, users, and computer systems. They allow users to send messages to one another's screens, to
browse web pages, to send electronic-mail messages, to log in remotely, or to transfer files from one
machine to another.
In addition to systems programs, most In addition to systems programs, most operating systems are
supplied with programs that are useful in solving common problems or performing common operations.
Such programs include web browsers, word processors and text formatters, spreadsheets, database systems,
compilers, plotting and statistical-analysis packages, and games. These programs are known as system
utilities or application programs.
19
The bootstrap program can perform a variety of tasks. Usually, one task is to run diagnostics to
determine the state of the machine. If the diagnostics pass, the program can continue with the booting steps.
It can also initialize all aspects of the system, from CPU registers to device controllers and the contents of
main memory. Sooner or later, it starts the operating system.
Some systems—such as cellular phones, PDAs, and game consoles—store the entire operating
system in ROM. Storing the operating system in ROM is suitable for small operating systems, simple
supporting hardware, and rugged operation. A problem with this approach is that changing the bootstrap
code requires changing the ROM hardware chips. Some systems resolve this problem by using erasable
programmable read-only memory (EPROM), which is read only except when explicitly given a
command to become writable. 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 (including most general-purpose operating systems like Windows, Mac
OS X, and UNIX) or for systems that change frequently, the bootstrap loader are stored in firmware, and
the operating system is on disk. In this case, the bootstrap runs diagnostics and has a bit of code that can
read a single block at a fixed location (say block zero) from disk into memory and execute the code from
that boot block. The program stored in the boot block may be sophisticated enough to load the entire
operating system into memory and begin its execution. More typically, it is simple code (as it fits in a single
disk block) and only knows the address on disk and length of the remainder of the bootstrap program. All
of the disk-bound bootstrap, and the operating system itself, can be easily changed by writing new versions
to disk.
1.3 PROCESSES
Process : A process is a program in execution. A process is more than the program code, which is
sometimes known as the text section. It also includes the current activity, as represented by the value of
the program counter and the contents of the processor's registers. A process generally also includes the
process stack, which contains temporary data (such as function parameters, return addresses, and local
variables), and a data section, which contains global variables. A process may also include a heap, which
is memory that is dynamically allocated during process run time.
Structure of a process
20
We emphasize that a program by itself is not a process; a program is a passive entity, such as a file
containing a list of instructions stored on disk (often called an executable file), whereas a process is an
active entity, with a program counter specifying the next instruction to execute and a set of associated
resources. A program becomes a process when an executable file is loaded into memory.
Two common techniques for loading executable files are double-clicking an icon representing the
executable file and entering the name of the executable file on the command line (as in prog. exe or a. out.)
Process State
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:
• New. The process is being created.
• Running. Instructions are being executed.
• Waiting. The process is waiting for some event to occur (such as an
I/O completion or reception of a signal).
• Ready. The process is waiting to be assigned to a processor.
• Terminated. The process has finished execution.
These names are arbitrary, and they vary across operating systems. The states that they represent are fotind
on all systems, however. Certain operating systems also more finely delineate process states. It is important
to realize that only one process can be running on any processor at any instant.
21
Each process is represented in the operating system by a process control block (PCB)—also called a task
control block.
Process state. The state may be new, ready, running, and waiting, halted, and so on.
Program counter-The counter indicates the address of the next instruction to be executed for this
process.
• CPU registers- The registers vary in number and type, depending on the computer architecture. They
include accumulators, index registers, stack pointers, and general-purpose registers, plus any condition-
code information.
CPU-scheduling information- This information includes a process priority, pointers to scheduling queues,
and any other scheduling parameters.
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
Accounting information-This information includes the amount of CPU and real time used, time limits,
account members, job or process numbers, and so on.
I/O status information-This information includes the list of I/O devices allocated to the process, a list of
open files, and so on.
The process scheduler selects an available process (possibly from a set of several available processes) for
program execution on the CPU.
As processes enter the system, they are put into a job queue, which consists of all processes in the system.
The processes that are residing in main memory and are ready and waiting to execute are kept on a list
called the ready queue.
This queue is generally stored as a linked list. A ready-queue header contains pointers to the first and final
PCBs in the list. Each PCB includes a pointer field that points to the next PCB in the ready queue.
22
Each rectangular box represents a queue. Two types of queues are present: the ready queue and a set of
device queues. The circles represent the resources that serve the queues, and the arrows indicate the flow
of processes in the system.
A new process is initially put in the ready queue. It waits there till it is selected for execution, or is
dispatched. Once the process is allocated the CPU and is executing, one of several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new sub process and wait for the sub process’s termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the
ready queue.
Schedulers
A process migrates among the various scheduling queues throughout its lifetime. The operating system
must select, for scheduling purposes, processes from these queues in some fashion.
23
The selection process is carried out by the appropriate scheduler. The long-term scheduler, or job
scheduler, selects processes from this pool and loads them into memory for execution. The short-term
scheduler, or CPU scheduler, selects from among the processes that are ready to execute and allocates the
CPU to one of them.
Process Creation
A process may create several new processes, via a create-process system call, during the course of
execution. The creating process is called a parent process, and the new processes are called the children
of that process. Each of these new processes may in turn create other processes, forming a tree of processes.
Most operating systems identify processes according to a unique process identifier (or pid), which
is typically an integer number. These processes are responsible for managing memory and file systems. The
sched process also creates the init process, which serves as the root parent process for all user processes.
When a process creates a new process, two possibilities exist in terms of execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
There are also two possibilities in terms of the address space of the new process:
1. The child process is a duplicate of the parent process (it has the same program and data as the parent).
2. The child process has a new program loaded into it.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid-t pid;
/* fork a child process */
pid = fork();
if (pid < 0) {/* error occurred */
fprintf(stderr, "Fork Failed");
exit (-1) ;
}
else if (pid == 0} {/* child process */
execlpf"/bin/Is","Is",NULL);
}
else {/* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
exit (0) ;
24
In UNIX, as we've seen, each process is identified by its process identifier, which is a unique integer. A
new process is created by the fork() system call. The new process consists of a copy of the address space
of the original process. This mechanism allows the parent process to communicate easily with its child
process. Both processes (the parent and the child) continue execution at the instruction after the f ork(),
with one difference: The return code for the fork() is zero for the new (child) process, whereas the (nonzero)
process identifier of the child is returned to the parent. the exec() system call is used after a fork() system
call by one of the two processes to replace the process's memory space with a new program. The exec ()
system call loads a binary file into memory (destroying the memory image of the program containing the
execO system call) and starts its execution.
Process Termination
A process terminates when it finishes executing its final statement and asks the operating system to delete
it by using the exit () system call. At that point, the process may return a status value (typically an integer)
to its parent process (via the wait() system call). All the resources of the process—including physical and
virtual memory, open files, and I/O buffers—are deal located by the operating system.
Termination can occur in other circumstances as well. A process can cause the termination of
another process via an appropriate system call (for example, TerminateProcessO in Win32). Usually, such
a system call can be invoked only by the parent of the process that is to be terminated.
A parent may terminate the execution of one of its children for a variety of reasons, such as these:
• The child has exceeded its usage of some of the resources that it has been allocated.
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system does not allow a child to
continue if its parent terminates.
Consider that, in UNIX, we can terminate a process by using the exit() system call; its parent
process may wait for the termination of a child process by using the wait() system call. The wait () system
call returns the process identifier of a terminated child so that the parent can tell which of its possibly many
children has terminated.
If the parent terminates, however, all its children have assigned as their new parent the init
process.
Processes executing concurrently in the operating system may be either independent processes or
cooperating processes. A process is independent if it cannot affect or be affected by the other processes
executing in the system.
Any process that does not share data with any other process is independent. A process is cooperating if it
can affect or be affected by the other processes executing in the system.
There are several reasons for providing an environment that allows process cooperation:
• Information sharing. Since several users may be interested in the same piece of information (for
instance, a shared file), we must provide an environment to allow concurrent access to such information.
25
• Computation speedup. If we want a particular task to run faster, we must break it into subtasks, each of
which will be executing in parallel with the others. Notice that such a speedup can be achieved only if the
computer has multiple processing elements (such as CPUs or I/O channels).
• Modularity. We may want to construct the system in a modular fashion, dividing the system functions
into separate processes or threads. • Convenience. Even an individual user may work on many tasks at the
same time. For instance, a user may be editing, printing, and compiling in parallel.
Cooperating processes require an interprocess communication (IPC) mechanism that will allow
them to exchange data and information. There are two fundamental models of interprocess communication:
(1) shared memory and (2) message passing. In the shared-memory model, a region of memory
that is shared by cooperating processes is established. Processes can then exchange information by reading
and writing data to the shared region. In the message passing model, communication takes place by means
of messages exchanged between the cooperating processes.
Message passing is useful for exchanging smaller amounts of data, because no conflicts need be
avoided. Message passing is also easier to implement than is shared memory for intercomputer
communication. Shared memory allows maximum speed and convenience of communication, as it can be
done at memory speeds when within a computer.
Shared memory is faster than message passing, as message-passing systems are typically
implemented using system calls and thus require the more time consuming task of kernel intervention.
26
actions without sharing the same address space and is particularly useful in a distributed environment,
where the communicating processes may reside on different computers connected by a network.
Naming
Processes that want to communicate must have a way to refer to each other. They can use either direct or
indirect communication.
Under direct communication, each process that wants to communicate must explicitly name the recipient
or sender of the communication. In this scheme, the send.0 and receive() primitives are defined as:
• send(P, message)—Send a message to process P.
• receive (Q, message)—Receive a message from process Q.
• A link is established automatically between every pair of processes that want to communicate. The
processes need to know only each other's identity to communicate.
• A link is associated with exactly two processes.
• Between each pair of processes, there exists exactly one link.
The disadvantage in both of these schemes (symmetric and asymmetric) is the limited modularity of the
resulting process definitions. Changing the identifier of a process may necessitate examining all other
process definitions.
Synchronization
Communication between processes takes place through calls to send() and receive () primitives. There are
different design options for implementing each primitive. Message passing may be either blocking or
nonblocking— also known as synchronous and asynchronous.
• Blocking send- The sending process is blocked until the message is received by the receiving process or
by the mailbox.
• Nonblocking send- The sending process sends the message and resumes operation.
• Blocking receive- The receiver blocks until a message is available.
• Nonblocking receive- The receiver retrieves either a valid message or a null.
Buffering
27
• Unbounded capacity- The queues length is potentially infinite; thus, any number of messages can wait
in it. The sender never blocks.
Several IPC mechanisms are available for POSIX systems, including shared memory and message passing.
A process must first create a shared memory segment using the shmget () system call (shmget () is derived
from SHared Memory GET).
This first parameter specifies the key (or identifier) of the shared-memory segment. If this is set to IPC-
PRIVATE, a new shared-memory segment is created. The second parameter specifies the size (in bytes) of
the shared memory segment. Finally, the third parameter identifies the mode, which indicates how the
shared-memory segment is to be used—that is, for reading, writing, or both. By setting the mode to SJRUSR
| SJVVUSR, we are indicating that the owner may read or write to the shared memory segment.
Processes that wish to access a shared-memory segment must attach it to their address space using
the shmat () (SHared Memory ATtach) system call.
The call to shmat () expects three parameters as well. The first is the integer identifier of the shared-
memory segment being attached, and the second is a pointer location in memory indicating where the shared
memory will be attached. If we pass a value of NULL, the operating system selects the location on the
user's behalf. The third parameter identifies a flag that allows the shared memory region to be attached in
read-only or read-write mode; by passing a parameter of 0, we allow both reads and writes to the shared
region.
The third parameter identifies a mode flag. If set, the mode flag allows the shared-memory region
to be attached in read-only mode; if set to 0, the flag allows both reads and writes to the shared region. We
attach a region of shared memory using shmat () as follows:
shared_memory = (char *) shmat(id, NULL, 0);
If successful, shmat () returns a pointer to the beginning location in memory where the shared-memory
region has been attached.
An Example: Windows XP
The Windows XP operating system is an example of modern design that employs modularity to increase
functionality and decrease the time needed to implement new features. Windows XP provides support for
multiple operating environments, or subsystems, with which application programs communicate via a
message-passing mechanism. The application programs can be considered clients of the Windows XP
subsystem server.
The message-passing facility in Windows XP is called the local procedure call (LPC) facility. The LPC
in Windows XP communicates between two processes on the same machine. It is similar to the standard
RPC mechanism that is widely used, but it is optimized for and specific to Windows XP. Windows XP uses
a port object to establish and maintain a connection between two processes. Every client that calls a
subsystem needs a communication channel, which is provided by a port object and is never inherited.
Windows XP uses two types of ports: connection ports and communication ports. They are really the same
but are given different names according to how they are used. Connection ports are named objects and are
visible to all processes
28
• The client sends a connection request.
• The server creates two private communication ports and returns the handle to one of them to the client.
• The client and server use the corresponding port handle to send messages or callbacks and to listen for
replies.
Windows XP uses two types of message-passing techniques over a port that the client specifies when it
establishes the channel. The simplest, which is used for small messages, uses the port's message queue as
intermediate storage and copies the message from one process to the other. Under this method, messages
of up to 256 bytes can be sent. If a client needs to send a larger message, it passes the message through a
section object, which sets up a region of shared memory. The client has to
decide when it sets up the channel whether or not it will need to send a large message. If the client
determines that it does want to send large messages, it asks for a section object to be created. Similarly, if
the server decides that replies will be large, it creates a section object. So that the section object can be used,
a small message is sent that contains a pointer and size information about the section object. This method
is more complicated than the first method, but it avoids data copying. In both cases, a callback mechanism
can be used when either the client or the server cannot respond immediately to a request.
29