Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Similar documents
Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

ICS Principles of Operating Systems

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Lecture 2 Process Management

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 4: Processes. Process Concept

Chapter 4: Processes

Diagram of Process State Process Control Block (PCB)

Chapter 3: Processes. Operating System Concepts 8th Edition

Process Concept. Chapter 4: Processes. Diagram of Process State. Process State. Process Control Block (PCB) Process Control Block (PCB)

Chapter 4: Processes

The Big Picture So Far. Chapter 4: Processes

Chapter 4: Processes. Process Concept

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

The Big Picture So Far. Chapter 4: Processes

Chapter 4: Multi-Threaded Programming

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

Chapter 3: Processes. Operating System Concepts 8 th Edition,

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

CHAPTER 3 - PROCESS CONCEPT

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 3: Processes

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

Chapter 3: Processes. Operating System Concepts 8th Edition, modified by Stewart Weiss

Part Two - Process Management. Chapter 3: Processes

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

Chapter 3: Processes

Process Concept Process Scheduling Operations On Process Inter-Process Communication Communication in Client-Server Systems

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Chapter 3: Process Concept

Chapter 3: Process Concept

CS 450 Operating System Week 4 Lecture Notes

Chapter 3: Processes. Operating System Concepts 9 th Edition

Process. Program Vs. process. During execution, the process may be in one of the following states

OPERATING SYSTEM. Chapter 4: Threads

Chapter 3 Process Description and Control

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Course: Operating Systems Instructor: M Umair. M Umair

Chapter 4: Processes. Process Concept. Process State

Processes. Process Concept

Processes and Threads

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

CS420: Operating Systems. Interprocess Communication

Definition Multithreading Models Threading Issues Pthreads (Unix)

Chapter 4: Threads. Chapter 4: Threads

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

Chapter 3: Process Concept

Chapter 4: Threads. Operating System Concepts 9 th Edition

! The Process Control Block (PCB) " is included in the context,

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Chapter 4: Processes

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Chapter 4: Threads. Operating System Concepts 9 th Edition

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CHAPTER 2: PROCESS MANAGEMENT

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

Chapter 5: Threads. Outline

Chapter 3: Processes. Operating System Concepts 8th Edition,

Process Description and Control

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Threads Chapter 5 1 Chapter 5

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

CSCE 313: Intro to Computer Systems

Operating Systems. Figure: Process States. 1 P a g e

Chapter 3: Processes. Operating System Concepts 9 th Edition

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

Transcription:

Os-slide#1 /*Sequential Producer & Consumer*/ int i=0; repeat forever Gather material for item i; Produce item i; Use item i; Discard item i; I=I+1; end repeat Analogy: Manufacturing and distribution Print shops Bank transaction Airline reservation Compiler Assembler Problems: A simple process always running in sequence can be very inefficient. All situations can not be modeled as a sequential process Os-slide#2 Page 1

Producer produces information Consumer consumes information out in Os-slide#3 in out in out in out Var buffer array[0..n-1] in=0; out=0; /*producer*/ repeat forever.. Produce item nextp;.. While( n+1 mode n == out) do nothing buffer[in]=nextp; in = in+1 mod n end repeat /*consumer*/ repeat forever While( in == out) do nothing nextc=buffer[out]; out = out+1 mod n.. Consume item nextp;.. end repeat Os-slide#4 Page 2

Processes can run independently, But... In many situations it is advantageous if processes can work together: Information sharing» many user may want to access same info at the same time» multiple write problem. computational speedup» A single job can be divided into concurrent tasks and each task can run in parallel while communicating occasionally.» producer-consumer problem (uncompress-print, compilerassembler cases). Modularity Os-slide#5! This is a facility that OS provides so that co-operating processes can communicate with each other more easily. Goal: Save processes from buffer management, synchronization. Blocking Send: send(destination_process, message) Sends a message to another process then blocks until message is received. Blocking Receive: receive(source_process, message) Blocks until the message is received. Os-slide#6 Page 3

Send() OS Recv() /*producer*/ repeat forever Produce item nextp; Send(consumer, nextp) end repeat /*consumer*/ repeat forever receive(producer, nextc) consume item nextp; end repeat Os-slide#7 "#$ How links are established? Can a link be associated with more than 2 processes? How many links can there be between each pair of processes? What is the capacity of a link? Any buffer? If so, how much? Can the message size vary? Is the link unidirectional or bidirectional? What to do if messages are lost? What to do if either sender or receiver dies? Os-slide#8 Page 4

'(%$ % Direct Communication: explicitly name the other process one link between two process can be bidirectional Indirect Communication: use mailbox owned by receiver many can be send to one. Receiver may change house & Zero Capacity no message wait sender or receiver one must wait Bounded Capacity sender or receiver one must wait, if buffer is full Unbounded Capacity sender can always continue Os-slide#9 % 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. Os-slide#10 Page 5

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. Os-slide#11 Operations create a new mailbox send and receive messages through mailbox destroy a mailbox Primitives are defined as: send(a, message) send a message to mailbox A receive(a, message) receive a message from mailbox A Os-slide#12 Page 6

Mailbox sharing P1, P2, and P3 share mailbox A. P1, sends; P2 and P3 receive. Who gets the message? Solutions Allow a link to be associated with at most two processes. Allow only one process at a time to execute a receive operation. Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. Os-slide#13 $) Message passing may be either blocking or non-blocking. Blocking is considered synchronous Non-blocking is considered asynchronous send and receive primitives may be either blocking or non-blocking. Os-slide#14 Page 7

*$ A simple process always running in sequence can be very inefficient. There is a need of dividing some jobs into multiple cooperating processes. Two many processes running concurrently, result in too much context switching. Solution: Thread Os-slide#15 +$,+-"($ Bank Program Producer Program Consumer Program 0 2560 3000 4200 OS Job 1 Job 2 Job 3 Address space 3000 1200 Program text Global data Queue 8800 10240 Job 4 unused Stack Enter queue dispatch CPU Exit Os-slide#16 Page 8

&( A process is: A unit of resource ownership» A process has an address space» A process has open files» A process may hold IO devices It is also: A unit of scheduling» A process is the item of concurrent execution in the OS» A CPU scheduler (dispather) assigns one process at a time onto a CPU. These two functions are usually linked together, but they don t have to. In modern OS: Process=unit of resource ownership Thread=unit of scheduling Os-slide#17 *$ Process = unit of resource ownership A process has:» an address space» program code» OS resources (files, IO devices) Thread=unit of scheduling A thread is a single sequential execution stream within a process (also called lightweight process) A thread has:» program counter» stack pointer (SP) Process B» registers A thread shares with other threads in the process group» an address space» program code, global variables» OS resources (files, IO devices) Shared data Shared data Process A Os-slide#18 Page 9

Os-slide#19 *$! A thread is bound to a particular process A process may contain multiple threads of control inside. All of the threads in a process: can execute concurrently share a common address space (and thus data other than private stack). Threads can block, create children, etc. Os-slide#20 Page 10

&*$ MS DOS, MAC OS Traditional UNIX VMS, Mach, Chorus, OS/2, Windows NT, Solaris, IRIX Embedded Systems, Nachos Os-slide#21.$*$/ A process with multiple thread makes great server (printer server, file server, database server): One server process, many worker threads. If one thread blocks ( such as read request0, others can still continue executing. All threads can share common data, no need for complicated inter process communication also saves memory! But.. No protection between threads since all threads in a process share common address space they can interfare with one another. Generally all threads belongs to a single process so protection is not a big problem. Os-slide#22 Page 11

+0 *$ User-level threads: a library of functions (to create, fork, switch, etc.) which user processes can call to create and manage their own threads. Positive Points: Does not require OS modification Simple representation- a PC, registers, stack and a small control block, all stored in the processes address space. Fast- generally just a function call. No kernel intervention is needed. Negative Points: OS has no knowledge of the threads so may get unfair attention Requires non-blocking system calls (otherwise entire process blocks if a single thread blocks). If one thread causes page-fault the entire process blocks. Example: POSIX Pthread, Mach C-threads, Solaris 2 UI-threads Os-slide#23 1 0 *$ Kernel-level threads: kernel provides the system calls to create and manage threads. Positive Points: kernel has full knowledge of all the threads. Scheduler can allocate more time to processes with more threads. Good for applications that frequently blocks. Negative Points: Slow: each system call is about 100 times slower. Significant overhead and kernel complexity requires a full TCB (thread control block) for all threads. Examples: Windows NT, Windows 2000, Solaris 2 BeOS, Tru64 Unix Os-slide#24 Page 12

2 Many system supports both user and kernel level thread. The mapping can be varied. Many-to-One One-to-One Many-to-Many Os-slide#25 2" Many user-level threads mapped to single kernel thread. Used on systems that do not support kernel threads. Os-slide#26 Page 13

"" Each user-level thread maps to kernel thread. Examples - Windows 95/98/NT/2000 - OS/2 Os-slide#27 222 Allows many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Solaris 2 Windows NT/2000 with the ThreadFiber package Os-slide#28 Page 14

'3 *$ Fork If one thread calls fork() does the new process duplicate all threads or the new process is single threaded? Some Unix systems have both versions. Execv generally works in the same way. It will overwrite the entire process including all threads. Os-slide#29 *$ How to handle cancellation? Case A: Multiple thread searching one DB. Then if one thread completes all can be terminated. Case B: multiple threads in an WebBrowser. One can be cancelled while others are running. Os-slide#30 Page 15

*$ Asynchrornous Cancellation One thread immediately terminates the other. Deferred cancellation The target thread periodically checks if it should be terminated. It can orderly terminate. Cancellation point Os-slide#31 +0 *$ User API users create user level threads by PThread or UI-thread API for thread creation and management. LWP It has an intermediate level of thread called LWP light weight process. Each process contains atleast one LWP The thread library mutiplexes user level threads on a pool of LWP. The user level thread who are currently on an LWP executes. Rests are blocked. CPU Os-slide#32 Page 16

1 *$ Standard Kernel Threads Executes all operations within kernel Each LWP has a kernel thread. Some kernel threads have no associated LWP and only does kernel job. Kernel level threads are only objects recognized by the scheduler. CPU Os-slide#33 *$4 User level threads can be bound to a LWP. If bound only that thread will run on that LWP. On request a LWP can be dedicated to one CPU. Unbound threads of one application use a common pool of LWPs. A group of threads in solaris (only) can all bind to one LWP. CPU Os-slide#34 Page 17

&2- Light weight process Task 1 User level thread Kernel thread CPU User level threads can be created easily. Only when guaranteed concurrency with respect to a kernel event (such as an file I/O) will be needed one separate LWP will be needed. Os-slide#35 $% User level thread It has a thread ID, register set (PC, Stack Pointer), stack, priority ( used by thread library). It is created by library call and Implemented in user space. Very fast. LWP One register set for the user level thread now running. Memory and accounting information. A kernel data structure resides in kernel space. Kernel level thread A small data structure and a stack. The data structure includes a copy of kernel registers, a pointer to LWP to which it is attached, and priority and scheduling information. Process has everything described in PCB+ a pointer to a list of its threads. Os-slide#36 Page 18