Threads and Data Sharing

Slides:



Advertisements
Similar presentations
Operating Systems Lecture 7.
Advertisements

©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Threads CSCI 444/544 Operating Systems Fall 2008.
1/30/2004CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Process Concept n An operating system executes a variety of programs: –Batch system – jobs –Time-shared systems – user programs or tasks n Textbook uses.
Operating System Concepts
Process Concept An operating system executes a variety of programs
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Silberschatz and Galvin  Operating System Concepts Module 4: Processes Process Concept Process Scheduling Operation on Processes Cooperating.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Silberschatz, Galvin and Gagne  Applied Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operation on Processes.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems.
Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server.
Chapter 3 Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Jan 19, 2005 Chapter 3: Processes Process Concept.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 4 Process Slide 1 Chapter 4 Process.
1 11/1/2015 Chapter 4: Processes l Process Concept l Process Scheduling l Operations on Processes l Cooperating Processes l Interprocess Communication.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 10 Processes II Read.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 3 Operating Systems.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
Lecture 4: Processes & Threads. Lecture 4 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents The concept of Process Process states and life-cycle.
 Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication in Client-Server.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
Operating System Concepts
XE33OSA Chapter 3: Processes. 3.2XE33OSASilberschatz, Galvin and Gagne ©2005 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes.
Introduction to threads
Chapter 3: Processes.
OPERATING SYSTEM CONCEPT AND PRACTISE
Processes and threads.
Principles of Operating Systems
Chapter 3: Process Concept
Operating System Concepts
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 4: Processes Process Concept Process Scheduling
Processes Overview: Process Concept Process Scheduling
Chapter 3: Process Concept
Threads & multithreading
Chapter 3: Processes.
Applied Operating System Concepts
Chapter 3: Processes.
Operating Systems Processes and Threads.
Chapter 4: Threads.
Chapter 4: Processes Process Concept Process Scheduling
Lecture 2: Processes Part 1
Chapter 3: Processes.
Recap OS manages and arbitrates resources
Chapter 3: Processes.
Inter Process Communication (IPC)
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 3: Processes.
Chapter 3: Processes.
Operating System Concepts
Chapter 3: Processes.
Chapter 3: Processes.
Chapter 3: Processes.
Chapter 3: Processes.
Processes August 10, 2019 OS:Processes.
Presentation transcript:

Threads and Data Sharing NETW 3005 Threads and Data Sharing

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

Lecture 03 - Threads and data-sharing NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing

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

Next Lecture Scheduling