0% found this document useful (0 votes)
5 views

Lecture 6 & 7

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 6 & 7

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

CSE 2024

Operating System
Lecture 6 & 7
Communication & Threads
Saurabh Mishra, PhD
Assistant Professor
Dept of CSE
Content
• Sockets
• RPC
• RPC Issues
• Threads
IPC using sockets
• What if wanted to communicate between processes that have
no common ancestor?
• Ans: sockets
• IPC for processes that are not necessarily on the same host.
• Sockets use names to refer to one another.
• Means of network IO.
Sockets
• A socket is defined as an endpoint for communication
• A pair of processes communicating over a network employ a pair of
sockets-one each process
• Concatenation of IP address and port – a number included at start of
message packet to differentiate network services on a host
• The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
• Communication consists between a pair of sockets
• Socket is associated with a protocol.
• IPC is transmitting a message between a socket in one process to a
socket in another process.
Socket Communication
Sockets
• All ports below 1024 are well known, used for standard services
• E.g. FTP listens to port no 21, HTTPs listens to port 80, Telent to 23
• Special IP address 127.0.0.1 to refer to system on which process is
running
• Messages sent to particular IP and port# can be received by the
process whose socket is associated with that IP and port#.
• Processes cannot share ports with other processes within the
computer. Can receive messages on different ports.
Sockets
• For mode details on sockets, you can refer to:
https://jan.newmarch.name/ClientServer/socket/socket.html
Remote Procedure Calls

• Remote Procedure Call (RPC) is a protocol that one program can use to
request a service from a program located in another computer on a
network without having to understand the network's details.
• It is similar in many respects to the IPC mechanism.
• However, because we are dealing with an environment in which the processes are
executing on separate systems, we must use a message-based communication
scheme to provide remote service.
• In contrast to the IPC facility, the messages exchanged in RPC communication are
well structured and are thus no longer just packets of data.
• Each message is addressed to an RPC daemon listening to a port on the remote
system, and each contains an identifier of the function to execute and the
parameters to pass to that function
• The function is then executed as requested and the output is sent back to the
requester in a separate message
Remote Procedure Calls

• Remote procedure call (RPC) abstracts procedure(without having to


understand network protocols) calls between processes on
networked systems
• Again uses ports for service differentiation
• Stubs – client-side proxy for the actual procedure on the server
• The client-side stub locates the server and marshalls the parameters
• The server-side stub receives this message, unpacks the marshalled
parameters, and performs the procedure on the server
• On Windows, stub code compile from specification written in
Microsoft Interface Definition Language (MIDL)
How RPC Works?
• The semantics of RPCs allow a client to invoke a procedure on a remote
host as it would invoke a procedure locally
• The RPC system hides the details that allow communication to take place by
providing a stub on the client side.
• Typically, 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 receives this message and invokes the
procedure on the server.
• If necessary, return values are passed back to the client using the same
technique.
Execution of RPC
RPC Issues
Issues How they resolved
• Differences in data representation on the • systems define a machine-independent
client and server machines. RPC representation of data. One such
representation is known as external data
• Eg. Representation of 32-bit integers: representation (XDR).
• On the client side, parameter
• Some systems (known as big-endian) use marshalling involves converting the
the high memory address to store the machine dependent data into XDR before
most significant byte, while other they are sent to the server.
systems (known as little-endian) store • On the server side, the XDR data are
the least significant byte at the high unmarshalled and converted to the
memory address. machine- dependent representation for
the server
RPC Issues
Issues How they resolved
• With standard procedure calls, some form of • 1) The binding information may be
binding takes place during link, load, or execution predetermined, in the form of fixed port
time so that a procedure call's name is replaced addresses. At compile time, an RPC call has a
by the memory address of the procedure call. The fixed port number associated with it. Once a
RPC scheme requires a similar binding of the program is compiled, the server cannot change
client and the server port, but how does a client the port number of the requested service
know the port numbers on the server? Neither
system has full information about the other • 2) Binding can be done dynamically by a
because they do not share memory. rendezvous mechanism. Typically, an operating
system provides a rendezvous (also called a
matchmaker) daemon on a fixed RPC port. A
client then sends a message containing the name
of the RPC to the rendezvous daemon requesting
the port address of the RPC it needs to execute.
The port number is returned, and the RPC calls
can be sent to that port until the process
terminates (or the server crashes).
Remote Procedure Calls (Cont.)

• Data representation handled via External Data Representation


(XDL) format to account for different architectures
• Big-endian and little-endian
• Remote communication has more failure scenarios than local
• Messages can be delivered exactly once rather than at most once
• OS typically provides a rendezvous (or matchmaker) service to
connect client and server
Threads
• To introduce the notion of a thread
◼a fundamental unit of CPU utilization that forms the basis of multithreaded
computer systems

• To examine issues related to multithreaded programming

Process
Thread 1
Thread 2
Thread 3
Thread vs. Process
• Processes
• Independent execution units use their own
states, address spaces, and interact with each
other via IPC
• Traditional Processes have single flow of
control (thread)

• Thread
• Flow of control (activity) within a process
• A single process on a modern OS may have
multiple threads
• All threads share the code, data, and files
while having some separated resources
• Threads directly interact with each other using
shared resources

Both are methods for Concurrency and Parallelism


.

Example: A Multi-Activity Text Editor


• Process approach on data When in the Course of
human events, it becomes
We hold these truths to be
self-evident, that all men are
destructive of these ends, it
is the Right of the People to

• P1: read from keyboard


necessary for one people to created equal, that they are alter or to abolish it, and to
dissolve the political bands endowed by their Creator institute new Government,
which have connected them with certain unalienable laying its foundation on such
with another, and to assume Rights, that among these are principles and organizing its
among the powers of the Life, Liberty and the pursuit powers in such form, as to

• P2: format document


earth, the separate and of Happiness.--That to them shall seem most likely
equal station to which the secure these rights, to effect their Safety and
Laws of Nature and of Governments are instituted Happiness. Prudence,
Nature's God entitle them, a among Men, deriving their indeed, will dictate that
decent respect to the just powers from the consent Governments long
opinions of mankind requires of the governed, --That established should not be

• P3: write to disk


that they should declare the whenever any Form of changed for light and
causes which impel them to Government becomes transient causes; and
the separation. accordingly all

The processes will actively access


the same set of data.

How do the processes


exchange data?
Kernel
Context Switch for Processes- costly
.

Ideal Solution for the Text Editor : Threads


• Three activities within one When in the Course of
human events, it becomes
necessary for one people to
We hold these truths to be
self-evident, that all men are
created equal, that they are
destructive of these ends, it
is the Right of the People to
alter or to abolish it, and to

process
dissolve the political bands endowed by their Creator institute new Government,
which have connected them with certain unalienable laying its foundation on such
with another, and to assume Rights, that among these are principles and organizing its
among the powers of the Life, Liberty and the pursuit powers in such form, as to
earth, the separate and of Happiness.--That to them shall seem most likely
equal station to which the secure these rights, to effect their Safety and

• Single address space


Laws of Nature and of Governments are instituted Happiness. Prudence,
Nature's God entitle them, a among Men, deriving their indeed, will dictate that
decent respect to the just powers from the consent Governments long
opinions of mankind requires of the governed, --That established should not be
that they should declare the whenever any Form of changed for light and

• Same execution environment


causes which impel them to Government becomes transient causes; and
the separation. accordingly all

• Data shared easily

• Switch between activities


• Only running context
• No change in address space Kernel
Another Example: Web servers
Thread Benefits
• Responsiveness Concurrent
Execution on a
• Interactive application Single-core
System

• Resource sharing
• Address space and other
resources
• Economy: less overhead
• Solaris: process creation 30X Parallel
overhead than thread; Execution on
a Multicore
• Context switching threads within a System
process 5X faster
• Scalability
• Better utilization of
multiprocessor/multicore systems
.

Thread Implementations: Issues


• Process usually starts with a single thread and creates others
• Thread management operations (similar to process management)
• Creation: procedure/method for the new thread to run
• Scheduling: runtime properties/attributes
• Destruction: release resources
• Thread Synchronization
• join, wait, etc.
• Who manages threads and where
• User space: managed by applications using some libraries
• Kernel space: managed by OS
• all modern OSes have kernel level support (more efficient)
.

User-Level Threads
• User threads: thread library at the user level
• Run-time system provides support for thread creation, scheduling
and management

Run-time system Thread


Process

User
Space
Kernel has
NO
Thread table knowledge of
Kernel Kernel threads!
Space

Process Table
.

User-Level Threads (cont.)


• Each process needs its own private ◼ Advantages
thread table to keep track of the ⚫ Fast thread switching: no kernel activity
threads in that process. involved
⚫ Customized/flexible thread scheduling
• The thread-table keeps track of the algorithm
per-thread items ⚫ Application portability: different machines
• (program counter, stack pointer, register, state..) with library
• When a thread does something that ◼ Problems/disadvantages:
may cause it to become blocked ⚫ Kernel only knows process
locally (e.g. wait for another thread), it ⚫ Blocking system calls: kernel blocks
calls a run-time system procedure. process, so one thread blocks all
• If the thread must be put into blocked activities (many-to-one mapping)
state, the procedure performs thread ⚫ All threads share one CPU, so cannot
switching use multi-proc./core
.

Kernel-Level Threads
Supported directly by OS
Kernel performs thread creation, scheduling & management in kernel space

Process
Thread

User
Space

Kernel
Space

Thread table Process Table


.

Kernel-Level Threads (cont.)


• Advantages
• User activity/thread with blocking I/O does NOT block other
activities/threads from the same user
• When a thread blocks, the kernel may choose another thread from the
same or different process
• Multi-activities in applications can use multi-proc/cores
• Problems/disadvantages
• Thread management could be relatively costly:
• all methods that might block a thread are implemented as system calls
• Non-flexible scheduling policy
• Non-portability: application can only run on same type of machine

What is the relationship between user level and kernel level threads?

How to map user level threads to kernel level threads?


Mapping: User → Kernel Threads
• Many-to-one
• Many user threads → one kernel thread (-/+
are same as in user-level threads)
Examples: Solaris Green Threads, GNU Portable Threads

• One-to-One
• One user thread → one kernel thread;
• + more concurrency
• - limited number of kernel threads
Examples: Windows NT/XP/2000, Linux, Solaris 9 and later

• Many-to-Many
• Many user threads → many kernel threads
• + no limit on the number of user threads
• - not true concurrency because kernel has
limited number of threads
Examples: Solaris prior to version 9, Windows NT/2000 with the ThreadFiber package
Two-level Model

• Similar to M:M, except that it allows a user thread to be bound to


kernel thread

• Examples
• IRIX
• HP-UX
• Tru64 UNIX
• Solaris 8 and earlier
*

Hybrid Implementation

• Use kernel-level threads, and then multiplex user-level threads onto


some or all of the kernel threads.

combine the best of both approaches


.

Light-Weight Process (LWP)


• Lightweight process (LWP): intermediate data structure
• For user-level threads, LWP is a Virtual processor
• Each LWP attaches to a kernel thread
• Multiple user-level threads → a single LWP
• Normally from the same process
• A process may be assigned multiple LWPs
• Typically, an LWP for each blocking system call
• OS schedules kernel threads (hence, LWPs) on the CPU
LWP: Advantages and Disadvantages
• + User level threads are easy to • Occasionally, we still need to
create/destroy/sync create/destroy LWP (as expensive as
• + A blocking call will not suspend kernel threads)
the process if we have enough LWP • Makes up calls (scheduler
• + Application does not need to know activation)
about LWP • + simplifies LWP management
• +LWP can be executed on different • - Violates the layered structure
CPUs, hiding multiprocessing
Thread Pool
• Recall web server example,
• We created a thread for every request
• This is better than creating a process, but still time consuming
• No limit is put on the number of threads
• Pool of threads
• Create some number of treads at the startup
• These threads will wait to work and put back into pool
• Advantages:
• Usually slightly faster to service a request with an existing thread than create a new
thread
• Allows the number of threads in the application(s) to be bound to the size of the pool
• Adjust thread number in pool
• According to usage pattern and system load
.

Thread Pool Example: Web server


Dispatcher while(TRUE) {
thread getNextRequest(&buf);
handoffWork(&buf);
}

Worker
thread

while(TRUE) {
waitForWork(&buf);
lookForPageInCache(&buf,&page);
if(pageNotInCache(&page)) {
Kernel readPageFromDisk(&buf,&page);
Web page }
cache returnPage(&page);
Network
}
connection
Sr.No Characteristi Multi-programming Multitaskng Multi-threading Multiprocessing
cs
1 Definition: Multiprogramming Multitasking is the Each thread is a small, Multiprocessing
refers to the practice of practice of working on independently executing describes a system with
running many many things at once. process inside a larger more than one
applications in parallel process. Multithreading is the processor and can thus
inside the same physical name for this approach. run many sets of
memory instructions
simultaneously.
Description In computer Multithreading is a form Multiprogramming was Multithreading is
programming, of multitasking involving developed to allow similar to multitasking
multiprogramming is a more than one thread simultaneous execution of but allows multiple
technique for of execution in a programs on early computers threads within a single
maximising processor program, each of which with small amounts of program instead of
usage by allowing shares some common memory. Multitasking between various
several processes to be process space and expanded on this early idea, programs.
run simultaneously. As a variables with other allowing multiple programs Multiprocessing allows
result of multitasking, an threads. When two or to execute simultaneously more than one CPU
operating system can more processors are with complete memory (central processing
run several programs used in a computer protection, allowing unused unit) to work together
concurrently by sharing system, this is called memory space to be used by as if they were one
the computer’s multiprocessing. another program when machine
resources. needed.
Sr.No Characteristics Multi-programming Multitaskng Multi-threading Multiprocessing
2 Number of CPU: One CPU is used. One CPU is used. Use one or more More than one CPU
than one CPU. is used.
3 Job processing The tasks are taking Time commitment is The time required Job processing times
time: longer to process. not very much. to do a work is have been reduced.
very standard.
4 Number of One process is One by one, the job Many More than one
processes being executed at a time. is executed. independent process is executed
executed: processes are
occurring
simultaneously
5 Economical: Yes, it is economical, Yes, it is economical, Yes, it is No, it is not that
economical, much economical
6 Throughput: There is a decrease in Moderate Average Maximum
throughput. throughput. throughput. throughput has been
reached.
7 Efficiency: Least Moderate Moderate Maximum
Windows XP Threads
• Implements the one-to-one mapping, kernel-level
• Each thread contains
• A thread id
• Register set
• Separate user and kernel stacks
• Private data storage area

• The register set, stacks, and private storage area are


known as the context of the threads
• The primary data structures of a thread include:
• ETHREAD (executive thread block)
• KTHREAD (kernel thread block)
• TEB (thread environment block)
Windows XP Threads
*

Linux Threads
• Linux uses the term task (rather than process or thread) when
referring to a flow of control

• Linux provides clone() system call to create threads


• A set of flags, passed as arguments to the clone() system call determine how
much sharing is involved (e.g. open files, memory space, etc.)

• Linux: 1-to-1 thread mapping


• NPTL (Native POSIX Thread Library)

37
Linux Threads
Next Lecture
CPU Scheduling

You might also like