OS_UNIT 3
OS_UNIT 3
OS_UNIT 3
3
Four Components of a Computer System
4
OS Objectives and Functions
• An OS is a program that controls the execution of application
programs and acts as an interface between applications and the
computer hardware.
• It can be thought of as having three objectives:
• Convenience: An OS makes a computer more convenient to use.
• Efficiency: An OS allows the computer system resources to be used in an
efficient manner.
• Ability to evolve: An OS should be constructed in such a way as to permit the
effective development, testing, and introduction of new system functions
without interfering with service.
5
The Operating System as a User/Computer Interface
6
• The end user is not concerned with the details of computer hardware. Thus, the
end user views a computer system in terms of a set of applications.
• 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, or library programs are
provided to implement functions that assist in program creation, the
management of files, and the control of I/O devices.
• The OS masks the details of the hardware from the programmer and provides
the programmer with a convenient interface for using the system. It acts as
mediator, making it easier for the programmer and for application programs to
access and use those facilities and services.
7
OS FUNCTIONS
• Memory Management
• Process Management/Scheduling
• Device Management
• File Management
• Protection & Security
• Control over system performance
• Error detection and response
• Job accounting
• Co-ordination between software and users
8
Memory Management
• Memory Management refers to the management of the main or primary
memory.
• Main memory is a large array of words or bytes where each word or byte
has its own address. Main memory provides a fast storage that can be
accessed directly by the CPU. For a program to be executed, it must in the
main memory.
• When the program is completed, the memory region is released and can be
used by other programs. Therefore, there can be more than one program
present at a time. Hence, it is required to manage the memory.
9
• The operating system:
• Allocates and deallocates the memory.
• Keeps track of memory locations and checks whether it is free or allocated to
some process.
• Distributes the memory while multiprocessing.
• In multiprogramming, the operating system selects which processes acquire
memory when and how much memory they get.
• Enables processes to share memory space hence helping various programs to
reside in the same address space of main memory at different times.
• Protects the memory address space of every process so that a process cannot
access the memory of another process (es) without proper permission of the
operating system.
• Performs swapping that is moving information back and forth between
primary memory and secondary memory so that various processes can be
executed even if the main memory is small in size.
10
Process Management/ Scheduling
• Every software that runs on a computer, whether in the background
or in the frontend, is a process.
• When more than one process runs on the system the OS decides how
and when a process will use the CPU.
• Scheduling is performed by the process manager, and deals with the
removal of the current process from the CPU and selecting another
process for execution.
• The Process Scheduler of an operating system deals with process
scheduling. Process Schedulers use scheduling algorithms to choose
the best process to be scheduled hence helping in better utilization of
the CPU.
11
• An Operating System does the following activities for process
management.
1. The process scheduler chooses a process to be executed, and the process
scheduler also decides for how long the chosen process is to be executed.
2. Keeps tracks of processor and status of process. The program responsible
for this task is known as traffic controller.
3. Allocates the processor (CPU) to a process.
4. De-allocates processor when a process is no longer required.
12
Device/Input-Output Management
• Various processes require input-output operations in between the
execution, so the processes move into a waiting state for the input-output
operations.
• The Operating System must provide an interface so that the I/O becomes
convenient.
• An Operating System manages device communication via their respective
drivers. The processes may require devices for their use. This management
is done by the OS.
• The OS:
• Allocates and deallocates devices to different processes.
• Keeps records of the devices.
• Decides which process can use which device for how much time.
13
File Management
• The file management in the operating system handles the files (binary,
text, pdf, docs, audio, video, etc.) present in computer software.
• A file system is normally organized into directories for easy navigation and
usage. The file system in the operating system is capable of managing
individual as well as groups of files present in the computer system.
• An Operating System carries out the following file management activities.
• It keeps track of where information is stored, user access settings, the status of
every file, and more. These facilities are collectively known as the file system.
• An OS keeps track of information regarding the creation, deletion, transfer, copy,
and storage of files in an organized way.
• It also maintains the integrity of the data stored in these files, including the file
directory structure, by protecting against unauthorized access.
14
Protection & Security
• Ensuring the safety and protection of a computer system, its
resources, and the data it processes are essential functions of an
operating system.
• Access Control: The operating system uses access control mechanisms that
only allow authorized users or processes to access them. It enforces
authentication methods, manages user accounts, and sets permissions to
restrict access to sensitive data, networks, devices, and files.
• The operating system safeguard sensitive information from being accessed or
intercepted by unauthorized individuals using data encryption techniques.
• The operating system includes security measures to detect and prevent
malware infections.
15
Other important functions
• Control over system performance: Controlling system performance is
critical to managing computer systems and ensuring their efficient
operation. It involves various techniques and tools to monitor, optimize,
and maintain the performance of hardware, software, and networks to
meet user expectations and requirements.
• Error detection and response: A variety of errors can occur while a
computer system is running. In such case, the OS must provide a response
that clears the error condition with the least impact on running
applications. The response may range from ending the program that
caused the error, to retrying the operation, to simply reporting the error to
the application.
16
• Job Accounting: Job accounting involves tracking and recording resource
usage and performance metrics of individual jobs or tasks. It gathers
information on resource usage by different processes, applications, or
users, performance analysis, capacity planning, and optimization.
• Co-ordination between software and users: Operating systems are crucial
in coordinating and managing various software tools like interpreters,
compilers, assemblers, and other applications for different users in a
computer system. They handle resource allocation, process management,
communication, and security, enabling a seamless and efficient user
experience while executing different software tools.
17
History of Operating System
❖ The First Generation (1940's to mid 1950's)
➢ No Operating System
➢ All programming was done in absolute machine language, often by wiring up plug-
boards to control the machine’s basic functions.
❖ The Second Generation (1955-1965)
➢ First operating system was introduced in the early 1950's.It was called GMOS
➢ Created by General Motors for IBM's machine the 701.
➢ Single-stream batch processing systems
❖ The Third Generation (1965-1980)
➢ Introduction of multiprogramming
➢ Development of Minicomputer
❖ The Fourth Generation (1980-Present Day)
➢ Development of PCs
➢ Birth of Windows/MaC OS
18
Serial Processing
• With the earliest computers, from the late 1940s to the mid-1950s,
the programmer interacted directly with the computer hardware;
there was no OS.
• These computers were run from a console consisting of display
lights, toggle switches, some form of input device, and a printer.
• 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.
19
• These early systems presented two main problems:
• Scheduling:
• Most installations used a hardcopy sign-up sheet to reserve computer
time.
• A user could sign up for a block of time in multiples of a half hour or so. A
user might sign up for an hour and finish in 45 minutes; this would result in
wasted computer processing time.
• The user might run into problems, not finish in the allotted time, and be
forced to stop before resolving the problem.
• Setup time:
• A single program, called a job , could involve loading the compiler and the
source program into memory, saving the compiled program (object
program) and then loading and linking together the object program and
common functions.
• Each of these steps could lead to a considerable amount of time was spent
just in setting up the program to run.
20
Simple Batch System
• Batch processing is a technique in which an Operating System collects the
programs and data together in a batch before processing starts.
• The central idea behind the simple batch-processing scheme is the use of
a piece of software known as the monitor.
• With this type of OS, the user no longer has direct access to the processor.
Instead, the user submits the job on cards or tape to a computer operator,
who batches the jobs together sequentially and places the entire batch on
an input device, for use by the monitor.
• Each program is constructed to branch back to the monitor when it
completes processing, at which point the monitor automatically begins
loading the next program.
• The operator groups similar jobs having same requirement into batches.
• 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.
The monitor improves job setup time as well.
21
22
Multi Programmed Batch System
• In a simple batch system, the processor would sit idle when input and
output devices are in use.
• Multi Programmed Batch System increases CPU utilization by organizing
jobs so that the CPU always has one job to execute.
• If computer is required to run several programs at the same time, the
processor could be kept busy for the most of the time by switching its
attention from one program to the next.
• Additionally I/O transfer could overlap the processor activity i.e, while
one program is waiting for an I/O transfer; another program can use the
processor. So CPU never sits idle or if comes in idle state then after a very
small time it is again busy.
23
• Multi programming batch systems use the mechanism of job
scheduling and CPU scheduling.
• In multiprogramming, multiple programs of different users can be
executed simultaneously. The multiple jobs that have to be run
simultaneously must be kept in main memory and the operating
system must manage them properly.
• The CPU switches to second job and begins to execute it when one
job waits for I/O transfer. Similarly, when second job needs to wait,
the processor is switched to third job, and so on. The processor
also checks the status of previous jobs, whether they are
completed or not.
24
Time-Sharing Systems
• Multi-programmed batched systems provided an environment where the
various system resources were utilized effectively but it did not provide
for user interaction with the computer system.
• Time sharing is a logical extension of multiprogramming.
• Here, an interactive computer system provides direct communication
between the user and the system.
• The user gives instructions to the operating system using a keyboard or a
mouse and waits for immediate result. Accordingly, the response time
should be short.
• A time-shared operating system allows many users to share the
computer simultaneously. Since each action or command in a time-
shared system tends to be short, only a little CPU time is needed for each
user.
25
• As the system switches rapidly from one user to the next, each user is
given the impression that the entire computer system is dedicated to
her use, even though it is being shared among many users.
• A time-shared OS uses CPU scheduling and multiprogramming to
provide each user with a small portion of time-shared computer.
• Virtual Memory, File system, Disk management, concurrent execution
26
• Advantages of Time-Sharing OS:
➢Each task gets an equal opportunity
➢Fewer chances of duplication of software
➢CPU idle time can be reduced
• Disadvantages of Time-Sharing OS:
➢Reliability problem
➢One must have to take care of the security and integrity of user
programs and data
➢Data communication problem
• Examples of Time-Sharing Oss
Multics, Unix, etc.
27
Multiprocessor Systems/ Parallel Systems
• Multiprocessor systems have more than one processor in close
communication, sharing the computer bus, memory and
peripheral devices.
• Multiprocessor systems have three main advantages:
• Increased throughput: By increasing the number of processors more work
can be done in less time.
• Economy of scale: multiprocessor systems can save more money than
multiple single-processor system because they can share peripherals, mass
storage and power supplies.
• Increased reliability: If functions can be distributed properly among several
processes then the failure of one processor will not halt the system,
only slow it down.
28
• Symmetric multiprocessing:
• Each processor runs and identical copy of the operating system and these
copies communicate with one another as needed.
• Asymmetric multiprocessing:
• Each processor is assigned specific task.
• A master processor control system; the other processors either look to the
master for instruction or have predefined task.
• Defines a master-slave relationship; the master processor schedules and
allocates work to the slave processors
29
Distributed Systems
• Distributed systems depend on networking for their functionality.
• By being able to communicate, distributed systems are able to share computational
task and provide a rich set of features to users.
• Networks vary by the protocol used, the distance between nodes and the transport
media.
• TCP/IP is the most common network protocol.
• Various autonomous interconnected computers communicate each other using a
shared communication network.
• Independent systems possess their own memory unit and CPU. These are referred as
loosely coupled systems or distributed systems. These system’s processors differ in
size and function.
• The major benefit of working with these types of operating system is that it is always
possible that one user can access the files or software which are not actually present
on his system but on some other system connected within this network i.e., remote
access is enabled within the devices connected in that network.
30
31
Real-Time Systems
• Real time system is used when there are rigid time requirements on
the operation of a processor or flow of data.
• System that controls scientific experiments, medical imaging
systems and some display systems are real time systems.
• A real-time operating system (RTOS) is an operating system
intended to serve real-time applications that process data as it
comes in, without buffer delays.
• Real time system has well-defined, fixed time constraints.
• Processing must be done within the defined constraints are the
system will fail.
32
Disadvantages of real-time system are:
• A real time system is considered to function correctly only if it returns
the correct result within the time constraints.
• Secondary storage is limited or missing instead data is usually stored
in short term memory or ROM.
• Advanced OS features are absent.
33
Real time system is of two types such as:
• Hard real time systems: It guarantees that the critical task has been
completed on time. The sudden task takes place at a sudden instant
of time.
• Soft real time systems: It is a less restrictive type of real time system
where a critical task gets priority over other tasks and retains that
priority until it computes. These have more limited utility than hard
real time systems. Processing must be done within the time
constraint.
34
PROCESS
• A process can be defined as,
• A program in execution
• An instance of a program running on a computer
• The entity that can be assigned to and executed on a processor
• A unit of activity characterized by the execution of a sequence of instructions,
a current state, and an associated set of system resources
• 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).
• 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.
36
Process in Memory
• 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.
37
Process Control Block
• Each process is represented in the operating system by a process control
block (PCB)—also called a task control block.
• The PCB contains sufficient information required to interrupt a running
process and later resume execution as if the interruption had not occurred.
• The process control block is the key tool that enables the OS to support
multiple processes and to provide for multiprocessing.
• When a process is interrupted, the current values of the program counter
and the processor register are saved in the appropriate fields of the
corresponding process control block, and the state of the process is
changed to some other value, such as blocked or ready.
• The OS is now free to put some other process in the running state. The
program counter and context data for this process are loaded into the
processor registers and this process now begins to execute.
38
• It contains many pieces of information associated with a specific process,
including these:
• Identifier: A unique identifier associated with this process, to distinguish it from all
other processes.
• State: If the process is currently executing, it is in the running state.
• Priority: Priority level relative to other processes.
• Program counter: The address of the next instruction in the program to be executed.
• Memory pointers: Includes pointers to the program code and data associated with
this process, plus any memory blocks shared with other processes.
• Context data: These are data that are present in registers in the processor while the
process is executing.
• I/O status information: Includes outstanding I/O requests, I/O devices (e.g., disk
drives) assigned to this process, a list of files in use by the process, and so on.
• Accounting information: May include the amount of processor time and clock time
used, time limits, account numbers, and so on.
39
Process Control Block
40
Process States
A Two-State Process Model
• In two-state model, a process may be in one of two states: Running or
Not Running.
• When the OS creates a new process, it creates a PCB for the process
and enters that process into the system in the Not Running state.
• The process waits for an opportunity to execute.
• From time to time, the currently running process will be interrupted
and the dispatcher portion of the OS will select some other process to
run. The former process moves from the Running state to the Not
Running state, and one of the other processes moves to the Running
state.
41
42
A Five-state model
• The queueing discipline suggested by two-state model would be
effective if all processes were always ready to execute.
• However, some processes in the Not Running state may be blocked,
waiting for an I/O operation to complete. Thus, using a single queue,
the dispatcher could not just select the process at the oldest end of
the queue.
• The five-state model split the Not Running state into two states:
Ready and Blocked.
• The New and Exit states are useful constructs for process
management.
43
• The five states in this new diagram are:
• Running: The process that is currently
being executed.
• Ready: A process that is prepared to
execute when given the opportunity.
• Blocked/Waiting: A process that cannot
execute until some event occurs, such as
the completion of an I/O operation.
• New: A process that has just been created
but has not yet been admitted to the pool
of executable processes by the OS.
• Exit: A process that has been released
from the pool of executable processes by
the OS, either because it halted or
because it aborted for some reason.
44
The types of events that lead to each state transition for a process.
• Null -> New: A new process is created to execute a program.
• New -> Ready: The OS will move a process from the New state to the Ready
state when it is prepared to take on an additional process.
• Ready -> Running: When it is time to select a process to run, the OS
chooses one of the processes in the Ready state. This is the job of the
scheduler or dispatcher.
• Running -> Exit: The currently running process is terminated by the OS if
the process indicates that it has completed, or if it aborts.
• Running -> Ready: The most common reason for this transition is that the
running process has reached the maximum allowable time for
uninterrupted execution. Another reason can be that the Os has preempted
the process.
45
• Running ->Blocked: A process is put in the Blocked state if it requests
something for which it must wait. For example, a process may request
a service from the OS that the OS is not prepared to perform
immediately. It can request a resource, such as a file or a shared
section of virtual memory, that is not immediately available.
• Blocked -> Ready: A process in the Blocked state is moved to the
Ready state when the event for which it has been waiting occurs.
46
Queueing model for 5-state model
47
Switching of CPU from Process to Process
48
OPERATIONS ON PROCESS
• Process Creation
• Process Termination
• Process Preemption
• Process Blocking
49
Steps in Creation:
Assigning a Process ID (PID): The system assigns a unique identifier to the new process.
Allocating Resources: Memory, CPU cycles, and other resources are allocated based on system policies.
Loading Program Code: The OS loads the program's code and initializes the process's control structures.
Initializing the PCB: The Process Control Block (PCB) is prepared with details such as process state, ID, and resource pointers.
Process Creation
Adding to the Ready Queue: The process is placed in the ready queue for scheduling.
50
• In a batch environment, a process is created in response to the
submission of a job.
• In an interactive environment, a process is created when a new user
attempts to log on.
• An OS may also create a process on behalf of an application. For example,
if a user requests that a file be printed, the OS can create a process that
will manage the printing. The requesting process can thus proceed
independently of the time required to complete the printing task.
• One process can create another process. For example, a server process
(e.g., print server, file server) may generate a new process for each
request that it handles.
• When the OS creates a process at the explicit request of another process,
the action is referred to as process spawning .
• When one process spawns another, the former is referred to as the
parent process, and the spawned process is referred to as the child
process.
51
• A process may be created by another process using fork() system call.
• A child process can have only one parent but a parent process may
have many children.
• Both the parent and child processes have the same memory image,
open files, and environment strings, but, they have distinct address
spaces.
52
• Parent process create children processes, which, in turn create
other processes, forming a tree of processes
• Generally, process identified and managed via a process identifier
(pid).
• Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
• Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
A tree of processes
53
Process Preemption
• An interrupt mechanism is used in preemption that suspends the process
executing currently and the next process to execute is determined by the
scheduler.
• Preemption makes sure that all processes get some CPU time for execution.
54
Process Request: The process requests a resource or event.
OS Action: The OS places the process in the Blocked Queue.
Process Blocking Unblocking: Once the resource or event becomes available, the OS moves the proce
55
Process Termination Intervention: A parent process can terminate its child (e.g., using kill commands).
• After the process has completed the execution of its last instruction, it is
terminated. The resources held by a process are released after it is
terminated.
• A child process can be terminated by its parent process if its task is no
longer relevant.
• The child process sends its status information to the parent process before
it terminates. Also, when a parent process is terminated, its child processes
are terminated as well.
Notification to OS: The process informs the OS about its completion or failure.
Resource Deallocation: The OS reclaims memory, I/O devices, and other resources.
Updating System Tables: The process is removed from scheduling and process tables.
56
57
Process Communication
Types of Processes
• 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. Clearly, any process that shares data
with other processes is a cooperating process
58
Reason for cooperating processes:
• 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.
• 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.
• 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, listening to music, and compiling in parallel.
59
• 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:
shared memory and 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.
60
• Shared memory can be faster than
message passing, since message-
passing systems are implemented
using system calls and thus require
kernel intervention.
• In shared-memory systems, system
calls are required only to establish
shared memory regions. Once shared
memory is established, all accesses
are treated as routine memory
accesses, and no assistance from the Communication models. (a) Message Passing (b) Shared Memory
kernel is required.
61
Shared Memory Model
• Interprocess communication using shared memory requires
communicating processes to establish a region of shared memory.
• A shared-memory region resides in the address space of the process
creating the shared-memory segment. Other processes that wish to
communicate using this shared-memory segment must attach it to their
address space.
• The OS 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 processes are also responsible for ensuring that they are not writing to
the same location simultaneously.
62
Message Passing Model
• Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the
same address space.
• It is useful in a distributed environment, where the communicating
processes may reside on different computers connected by a
network.
• A message-passing facility provides two operations: send(message)
and receive(message)
• If processes P and Q want to communicate, they must send messages
to and receive messages from each other: a communication link must
exist between them.
63
• There are several methods for logically implementing a link
and the send()/receive() operations:
• Direct or indirect communication
• Synchronous or asynchronous communication
• Automatic or explicit buffering
64
Direct or indirect communication
• 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() 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.
65
• With indirect communication, the messages are sent to and received from
mailboxes, or ports.
• Each mailbox has a unique identification.
• A process can communicate with another process via a number of different
mailboxes, but two processes can communicate only if they have a shared
mailbox.
• The send() and receive() primitives are defined as follows:
• send(A, message)—Send a message to mailbox A.
• receive(A, message)—Receive a message from mailbox A.
• A link is established between a pair of processes only if both members of
the pair have a shared mailbox.
• A link may be associated with more than two processes.
• Between each pair of communicating processes, a number of different links
may exist, with each link corresponding to one mailbox.
66
Synchronous or Asynchronous Communication
• Communication between processes takes place through calls to
send() and receive() primitives.
• Message passing may be either,
• Blocking (Synchronous)
• Non-blocking (Asynchronous)
67
• Blocking send() primitive: Refers to the blocking of sending process.
The process remains blocked until it receives an acknowledgment
from the receiver side that the message has been received after the
execution of this primitive.
• Non-blocking send() primitive: Refers to the non-blocking state of
the sending process that implies after the execution of send()
statement, the process is permitted to continue further with its
execution immediately.
• Blocking receive() primitive: when the receive() call is executed, the
receiving process is halted until a message is received.
• Non-blocking receive() primitive: Implies that the receiving process is
not blocked after executing the receive() statement, control is
returned immediately after informing the kernel of the message
buffer’s location.
68
Automatic or Explicit Buffering
• Messages exchanged by communicating processes reside in a
temporary queue. Such queues can be implemented in three ways:
• Zero capacity. The queue has a maximum length of zero; thus, the link cannot
have any messages waiting in it. In this case, the sender must block until the
recipient receives the message.
• Bounded capacity. The queue has finite length n; thus, at most n messages
can reside in it. If the queue is not full when a new message is sent, the
message is placed in the queue, and the sender can continue execution
without waiting. If the link is full, the sender must block until space is available
in the queue.
• Unbounded capacity. The queue’s length is potentially infinite; thus, any
number of messages can wait in it. The sender never blocks.
69
Communication in Client-Server System
• The three strategies for communication in client–server
systems:
• Sockets
• Remote procedure calls (RPCs)
• Pipes
70
Sockets
• A socket is defined as an endpoint for communication.
• A pair of processes communicating over a network employs a pair of
sockets—one for each process.
• A socket is identified by an IP address concatenated with a port number.
• Sockets use a client–server architecture. The server waits for incoming
client requests by listening to a specified port. Once a request is received,
the server accepts a connection from the client socket to complete the
connection.
• Servers implementing specific services (such as telnet, FTP, and HTTP)
listen to well-known ports (a telnet server listens to port 23; an FTP server
listens to port 21; and a web, or HTTP, server listens to port 80).
71
• When a client process initiates a request for
a connection, it is assigned a port (number
greater than 1024) by its host computer.
• For example, if a client on host X with IP
address 146.86.5.20 wishes to establish a
connection with a web server (which is
listening on port 80) at address 161.25.19.8,
host X may be assigned port 1625. The
connection will consist of a pair of sockets:
(146.86.5.20:1625) on host X and
(161.25.19.8:80) on the web server.
• The packets traveling between the hosts are Communication using sockets
delivered to the appropriate process based
on the destination port number.
72
Communication channels via sockets may be
of one of two major forms:
• Connection-oriented ( TCP, Transmission • Connectionless ( UDP, User Datagram
Control Protocol ). All packets sent down Protocol ). There is no guarantee that any
the connection are guaranteed to arrive particular packet will get through
in good condition at the other end, and to undamaged ( or at all ), and no guarantee
be delivered to the receiving process in that the packets will get delivered in any
the order in which they were sent. particular order.
• The TCP layer of the network protocol • There may even be duplicate packets
takes steps to verify all packets sent, re- delivered, depending on how the
send packets if necessary, and arrange intermediary connections are configured.
the received packets in the proper order • UDP transmissions are much faster than
before delivering them to the receiving TCP, but applications must implement
process. their own error checking and recovery
• If one packet is missing or delayed, then procedures.
any packets which follow will have to wait
until the packet is delivered before they
can continue their journey.
73
Remote Procedure Calls (RPC)
• The general concept of RPC is to make procedure calls similarly
to calling on ordinary local procedures, except the procedure
being called lies on a remote machine.
• RPCs allows a client to invoke a procedure on a remote host as it
would invoke a procedure locally.
• A client has a request that the RPC translates and sends to the
server. This request may be a procedure or a function call to a
remote server. When the server receives the request, it sends
the required response back to the client.
74
• How does RPC work?
• The RPC system hides the details that allow communication to take place by
providing a stub on the client side.
• A separate stub exists for each separate remote procedure. When the client
invokes a remote procedure, the RPC system calls the appropriate stub,
passing it the parameters provided to the remote procedure.
• This stub locates the port on the server and marshals the parameters.
Parameter marshalling involves packaging the parameters into a form that
can be transmitted over a network.
• The stub then transmits a message to the server using message passing.
• A similar stub on the server side (also called RPC daemon) receives this
message and invokes the procedure on the server. If necessary, return values
are passed back to the client using the same technique.
75
Communication using RPC
76
• One potential difficulty is the formatting of data on local versus
remote systems. ( e.g. big-endian versus little-endian. ) The resolution
of this problem generally involves an agreed-upon intermediary
format, such as XDR (external data representation).
• On the client side, parameter marshalling involves converting the
machine-dependent data into XDR before they are sent to the server.
On the server side, the XDR data are unmarshalled and converted to
the machine-dependent representation for the server.
77
Pipes
• A pipe acts as a conduit allowing two processes to communicate.
• Pipes are one of the earliest and simplest channels of
communications between ( UNIX ) processes.
• There are four key considerations in implementing pipes:
• Unidirectional or Bidirectional communication?
• Is bidirectional communication half-duplex or full-duplex?
• Must a relationship such as parent-child exist between the processes?
• Can pipes communicate over a network, or only on the same machine?
• Two common types of pipes used on both UNIX and Windows
systems: ordinary pipes and named pipes.
78
• Ordinary pipes are uni-directional, with a reading end and a writing
end. ( If bidirectional communications are needed, then a second
pipe is required. )
• Ordinary pipes have a parent-child relationship between the
processes as the pipes can only be accessed by processes that
created or inherited them.
• Named pipes are more powerful than ordinary pipes and allow two
way communication. These pipes exist even after the processes using
them have terminated. They need to be explicitly deleted when not
required anymore.
79
THREADS
• A thread is a lightweight process.
• A thread is a path of execution within a process.
• A process can contain multiple threads.
• Threads provide a way to improve application performance through
parallelism. The idea of multithreading is to achieve parallelism by
dividing a process into multiple threads.
• A thread is a basic unit of CPU utilization.
• A traditional (or heavyweight) process has a single thread of control.
• If a process has multiple threads of control, it can perform more than
one task at a time.
81
• It comprises a thread ID, a program
counter, a register set, and a stack.
• It shares its code section, data
section, and other operating-system
resources, such as open files and
signals with other threads belonging
to the same process.
83
Types of threads
Threads are implemented in following two ways:
• User Level Threads − User managed threads.
• Kernel Level Threads − Operating System managed threads acting on
kernel, an operating system core.
84
User Level Threads
• The user-level threads are implemented by users and the kernel
is not aware of the existence of these threads.
• User-level threads are much faster than kernel level threads.
• There is no kernel involvement in synchronization for user-level
threads.
• If one user level thread performs blocking operation, then the
entire process gets blocked.
85
Kernel Level Threads
87
Many-to-one model
• The many-to-one model maps many user-
level threads to one kernel thread.
• Thread management is done by the thread
library in user space.
• The entire process will block if a thread makes
a blocking system call. Also, because only one
thread can access the kernel at a time,
multiple threads are unable to run in parallel
on multicore systems.
88
One-to-one model
• The one-to-one model maps each user thread
to a kernel thread.
• It provides more concurrency than the many-
to-one model by allowing another thread to
run when a thread makes a blocking system
call.
• It also allows multiple threads to run in parallel
on multiprocessors.
• The only drawback to this model is that
creating a user thread requires creating the
corresponding kernel thread.
89
Many-to-many model
• The many-to-many model multiplexes
many user-level threads to a smaller or
equal number of kernel threads.
• Developers can create as many user
threads as necessary, and the
corresponding kernel threads can run in
parallel on a multiprocessor.
• Also, when a thread performs a blocking
system call, the kernel can schedule
another thread for execution.
90