Download presentation
Presentation is loading. Please wait.
1
Threads and Data Sharing
NETW 3005 Threads and Data Sharing
2
Last Lecture Hierarchical structure in Operating Systems
System calls and interrupts Representing processes in Operating Systems Overview of process scheduling NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
3
This lecture Cooperating processes.
Communication between parent and child processes. Shared memory and pipes. Threads. Inter-process communication. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
4
Interprocess Communication
Processes within a system may be independent or cooperating Cooperating process can affect or be affected by other processes, including sharing data Reasons for cooperating processes: Information sharing Computation speedup Modularity Convenience Cooperating processes need interprocess communication (IPC) Two models of IPC Shared memory Message passing
5
Shared memory: a simple kind of data sharing
The map memory family of system calls allow two processes to share some region of main memory. This is achieved by overriding the operating system’s normal constraints on memory access for processes. Operating System P2 P1 NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
6
IPC by shared memory ; Producer and Consumer Problem
A producer process writes data to a buffer of fixed size. A consumer process reads data from the buffer. The two processes are being scheduled independently by the CPU. Producer and Consumer Problem, explained in detail in Lecture 5 NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
7
IPC : Pipes (in UNIX) Pipes provide a simple method for sharing data.
Example : grep ‘party’ events.txt | lpr NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
8
What’s happening here? We’re typing the command to the shell.
When you hit ‘return’, the shell process normally forks a child process, which is told to execute the specified program. When you link two programs with a pipe, the shell process first sets up a buffer in memory, then forks two child processes, which write to / read from this buffer. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
9
Threads (1) Operating systems frequently support a special kind of process called a thread to allow more complex data-sharing. A standard process has a data section, code section, and a full PCB. A thread just has a program counter, a register set and a stack space. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
10
Threads (2) You can have several threads within a single process, using the same code section, data section, memory and I/O resources. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
11
Threads (3) S PC R S PC R S PC R code segment data segment
OS management information NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
12
Terminology The code segment, data segment, and O/S housekeeping information of a process is collectively known as a task. A task with just one thread is called a heavyweight process. A thread is also known as a lightweight process. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
13
Operations on threads Threads and processes can do a similar range of things. A thread can wait while an I/O process completes (called blocking). A thread can be in different states – ready, blocked, running, or terminated. A thread can create child threads. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
14
Advantages of threads (1)
Responsiveness. Multithreading an interactive application may allow a program to continue running, even if part of it is waiting. Switching speed. Switching between threads is faster than between (heavyweight) processes, because there’s less to change. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
15
Advantages of threads (2)
Communication between processes. We don’t need special setups for shared memory when implementing communicating processes as threads in the same task. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
16
Problems with Threads Can suffer from Race Condition
Difficult to predict the behavior of threads Difficult to diagnose and test NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
17
User-level threads Implemented in user-level libraries, without the need for system calls. Fastest kind of thread to switch between. But, the kernel doesn’t know about individual threads within a process. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
18
Kernel-level threads Implemented via system calls.
In this case, the kernel knows about individual threads. But switching between threads is slower. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
19
Multi-threading models
Many systems support both user-level and kernel-level threads. We then need some way to map user-level threads to kernel-level threads. many to one one to one many to Many NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
20
Many to one :Multi-threading model
In a many-to-one model, many user threads are mapped onto a single kernel thread. This suffers from the problem of blocking: the kernel doesn’t know about it when one thread blocks. Examples: Solaris Green Threads GNU Portable Threads NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
21
One to one :Multi-threading models
In a one-to-one model, each user thread is mapped onto a single kernel thread. This doesn’t suffer from the above prob-lem, but there’s an overhead in creating the corresponding kernel threads. Most systems only support some maximum number of kernel threads. Examples Windows Linux Solaris 9 and later NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
22
M:M -Multi-threading model
In a many-to-many model, a set of user threads is multiplexed onto a (smaller or equal) set of kernel threads. You need to have an extra mechanism that allocates each user thread to a kernel thread. A many-to-many scheme avoids many of the disadvantages of both above schemes. Examples : Solaris prior to version 9 Windows with Thread Fiber package NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
23
Java threads As well as any threads defined in a Java program, there are a number of extra threads running on behalf of the JVM. For instance: a thread to listen and react to the mouse/keyboard; a thread to do garbage collection; a thread to handle timer events (e.g. the sleep() method). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
24
Interprocess Communication – Message Passing
Mechanism for processes to communicate and to synchronize their actions Message system – processes communicate with each other without resorting to shared variables IPC facility provides two operations: send(message) receive(message) The message size is either fixed or variable To pass messages between processes, a communication link( Hardware Bus , Network , shared memory) must be established.
25
Direct Communication Processes must name each other explicitly:
send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q Properties of communication link Links are established automatically A link is associated with exactly one pair of communicating processes Between each pair there exists exactly one link The link may be unidirectional, but is usually bi-directional
26
Indirect Communication
Messages are directed and received from mailboxes (also referred to as ports) Each mailbox has a unique id Processes can communicate only if they share a mailbox Properties of communication link Link established only if processes share a common mailbox A link may be associated with many processes Each pair of processes may share several communication links Link may be unidirectional or bi-directional
27
Indirect Communication
send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
28
Lecture 03 - Threads and data-sharing
NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
29
Summary: ways of sharing data
Creating a child process Shared memory Pipes Threads Message passing system NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing
30
Next Lecture Scheduling
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.