NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Ch4. Processes Process Concept (1) new waiting terminate readyrunning admitted interrupt exit scheduler dispatch I/O or event completion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
3.5 Interprocess Communication
Threads CSCI 444/544 Operating Systems Fall 2008.
Process Concept n An operating system executes a variety of programs: –Batch system – jobs –Time-shared systems – user programs or tasks n Textbook uses.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
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, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
CE Operating Systems Lecture 5 Processes. Overview of lecture In this lecture we will be looking at What is a process? Structure of a process Process.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
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, Feb 7, 2006 Process Concept Process – a program.
Operating Systems Part III: Process Management (Process States and Transitions)
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
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.
Threads G.Anuradha (Reference : William Stallings)
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
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.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department.
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.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Process by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Processes and threads.
Chapter 3: Process Concept
Topic 3 (Textbook - Chapter 3) Processes
Operating System Concepts
Operating Systems (CS 340 D)
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 3: Process Concept
Operating Systems (CS 340 D)
Applied Operating System Concepts
Operating Systems Processes and Threads.
Lecture 2: Processes Part 1
Threads and Data Sharing
Process & its States Lecture 5.
Operating System Concepts
Presentation transcript:

NETW 3005 Threads and Data Sharing

Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing2

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-sharing3

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-sharing4

Independent and cooperating processes Independent processes. The execution of one process cannot affect the execution of another. Cooperating processes. The execution of one process can affect the execution of another. Naturally, any processes which share data are cooperating processes. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing5

Advantages of process cooperation Information sharing. Two processes might want to access the same data. Computation speedup. To introduce some parallelism into program execution. Modularity. Having program functions performed by their own processes. Convenience. A user might want to print a file at the same time as editing it. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing6

Communication between processes NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing7 root child daemoninit user1user2user3 system procapplication child A process can create a child process to do a particular task. But to do so, it has to be able to communicate with its child.

Creating child process in UNIX (1) In UNIX, a child process is created with a system call called fork. fork creates a new process, consisting of a copy of the address space of the parent process. The child process then typically executes an execlp command, which loads a new program into its memory space (erasing the copy of the parent). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing8

Creating child process in UNIX (2) System calls are functions, and functions can return values. fork is no exception. Both parent and child processes continue after the fork function call—but a different value is returned in the two cases: –the value for the child process is 0. –the value for the parent process is the PID of the child. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing9

Some questions (1) Why is the parent’s address space copied to the child process if execlp is just going to wipe it all out? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing10

Some questions (1) Why is the parent’s address space copied to the child process if execlp is just going to wipe it all out? Because you want to be able to pass arbitrary values as the parameters of execlp. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing11

Some questions (2) Why is the parent process given the PID of the child process? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing12

Some questions (2) Why is the parent process given the PID of the child process? So it knows which process is going to deliver the results of the computation. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing13

What next? As we have seen, the child process will go off and do its own thing, possibly executing a different program. The parent process often executes a wait system call, which moves it off the ready queue until the child process has terminated. When the child process has terminated, it may return data to the parent. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing14

Process termination When a process has executed its last statement, it executes an exit system call. At this point: –it may return data to its parent process (the one waiting for it to terminate); –all its resources (main memory, open files etc.) are de-allocated by the operating system. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing15

Process termination Processes can also die more violently – they can be terminated by other processes (e.g. in UNIX with kill). There is an important constraint on which processes you can kill — you can only kill your children. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing16 Zombie process: a process whose parent has terminated.

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. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing17 Operating System P2 P1

The Producer-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. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing18

Obviously … The producer process must wait if the buffer gets full; The consumer process must wait if the buffer is empty. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing19 This is a taster for the issue of process synchronisation.

Pipes (in UNIX) Pipes provide a simple method for sharing data. grep ‘party’ events.txt | lpr NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing20

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-sharing21

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-sharing22

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-sharing23

Threads (3) NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing24 SPCR S R S R code segment data segment OS management information

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-sharing25

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-sharing26

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-sharing27

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. Redundancy avoidance. If you need several versions of one program reading the same data at the same time, it’s inefficient to have each version implemented as a heavyweight process. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing28

Problems with threads It’s a bit perilous running threads, because there are no OS-enforced constraints on how threads can interact. However, if the code has been designed by a single person, there’s no reason why it can’t be written correctly. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing29

Servers as threads (1) A web server is a process that provides data over the web to requesting clients. (Possibly a large number of them.) Imagine a queue of client requests arriving in an input buffer. If we implemented the web server as a single process, what would the problem be? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing30

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing31 The file server would be inequitable. The clients at the end of the queue wouldn’t get ANY service until the ones on the front had been fully serviced.

Servers as threads (2) It’s more efficient if the web server process forks a child process to deal with each separate request that arrives. Why? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing32

Servers as threads (2) It’s more efficient if the web server process forks a child process to deal with each separate request that arrives. Why? Because this way each client gets some access to the CPU, and gets some data delivered. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing33

Servers as threads (3) One particular thread is a daemon – it does nothing but listen for new requests. Each time a new request is detected, a new thread is created to service this request. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing34

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-sharing35

Why should that matter? I.Unfairness. One process might have 100 threads, another process just one. The kernel gives them equal time in the CPU. II.System calls. If a thread makes a system call, then the whole (heavy- weight) process is suspended until it’s completed; that includes all the other threads. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing36

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-sharing37

Multi-threading models (1) Many systems support both user-level and kernel-level threads. We then need some way to map user- level threads to kernel-level threads. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing38

Multi-threading models (2) 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. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing39

Multi-threading models (3) 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. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing40

Multi-threading models (4) 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. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing41

Java threads (1) 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-sharing42

Java threads (2) The JVM will implement threads in different ways in different versions, and in different operating systems. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing43

Message-passing systems (1) One final, general, way of communicating between processes is via a message- passing system. To pass messages between processes, a communication link must be established. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing44

Message-passing systems (2) This can be implemented in various ways, but it must specify certain logical characteristics: –Can a link be associated with more than two processes? –What is the capacity of the link? –Is the link unidirectional or bidirectional? The main instructions: send, receive. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing45

Direct and indirect communication Direct communication is achieved by naming the process that a message is sent to / received from, e.g. send(P001, message). BUT, process IDs are problematic – a process’ ID might be different next time. Solution: send to and receive from mail- boxes, e.g. send(mbox1, message). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing46

Summary: ways of sharing data Creating a child process Shared memory Pipes Threads Message passing system NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing47

Reading For this lecture, you should (have) read Chapter 4 (Sections 1–4) of Silberschatz et al. For the tutorial, you MUST read 4.1 and 4.2. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing48

Next Lecture Scheduling Chapter 5 (Sections 1-3, 7)