Lecture 6 & 7
Lecture 6 & 7
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
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
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
• 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
.
User-Level Threads
• User threads: thread library at the user level
• Run-time system provides support for thread creation, scheduling
and management
User
Space
Kernel has
NO
Thread table knowledge of
Kernel Kernel threads!
Space
Process Table
.
Kernel-Level Threads
Supported directly by OS
Kernel performs thread creation, scheduling & management in kernel space
Process
Thread
User
Space
Kernel
Space
What is the relationship between user level and kernel level 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
• Examples
• IRIX
• HP-UX
• Tru64 UNIX
• Solaris 8 and earlier
*
Hybrid Implementation
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
Linux Threads
• Linux uses the term task (rather than process or thread) when
referring to a flow of control
37
Linux Threads
Next Lecture
CPU Scheduling