Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
Advertisements

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 5 Threads os5.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Process Concept An operating system executes a variety of programs
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: Threads.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models(多线程模型)
Nadeem MajeedChoudhary.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe

One process writes to the pipe, the other reads from the pipe. Looks exactly the same as reading from/to a file. System call: int fd[2] ; pipe(&fd[0]) ; fd[0] now holds descriptor to read from pipe fd[1] now holds descriptor to write into pipe

Simple Example #include /* for Solaris */ #include char *message = "This is a message!!!" ; main() { char buf[1024] ; int fd[2]; pipe(fd); /*create pipe*/ if (fork() != 0) { /* I am the parent */ write(fd[1], message, strlen (message) + 1) ; } else { /*Child code */ read(fd[0], buf, 1024) ; printf("Got this from MaMa!!: %s\n", buf) ; }

Create a Pipeline Sometimes useful to connect a set of processes in a pipeline. Process c D Pipe Process A writes to pipe AB, Process B reads from AB and writes to BC Process C reads from BC and writes to CD …..

Inherited File Descriptors Process created with three files: stdin, stdout, and stderr (file descriptors 0, 1, 2). write(1, “EEEE”, 10) ; // goes to stdout (terminal) or printf read(0, buf, 4) ; //read from terminal

Redirecting stdin and stdout Can due on command line:./a.out > tmp // write output to tmp rather than terminal../a.out < foo // read from foo not terminal. When processing in a pipeline useful to redirect stdout of pipe n to stdin of pipe n+1. Key: When Unix allocates file descriptor, it always chooses the lowest available. If close stdin and open new file, e.g., close(0), fd = open(“foo”, ….) then fd = 0 and all subsequent reads from stdin will read instead from “foo”.

main() { int fd ; printf("this goes to stdout\n"); close(1); /* close stdout */ fd = open("foo",O_CREAT | O_WRONLY); /* fd = 1! */ printf("fd = %d\n", fd) ; //goes to file "foo" printf("Should not see this either!\n") ;//ditto }

Dup System Call int dupfd = dup(fd); Duplicates a file descriptor -- “aliased” Both point to same file, that being the argument to dup. Reads/writes from fd and dupfd going to the same file. Useful for situation such as: want to write some output to standard out, then to a file, then back to standard out -- all using printf. Redirecting I/O for pipeline.

pipeline (process1, process2) /* program names */ char *process1, *process2 ; { char buf[1024] ; int fd[2]; pipe(&fd[0]); /*create pipe*/ if (fork() != 0) { /* parent */ close(fd[0]); //don't need. close(STD_OUTPUT); dup(fd[1]); /* sets stdout to this pipe end */ close(fd[1]) ; //don’t need. Already set stdout to pipe. execl(process2,process2,0); }

else { /* child */ close(fd[1]); close(STD_INPUT); dup(fd[0]); /* replace stdin */ close(fd[0]); execl(process1,process1,0); }

fork() exec stdout stdin

Single and Multithreaded Processes

Benefits Responsiveness: Separate thread to handle user input. Web server spawns separate thread to handle incoming request. Resource Sharing: e.g., one code, data segment. Economy: Much cheaper to create and switch than processes. Utilization of MP Architectures: Assign each thread to a separate processor if available.

Lightweight process. Threads share all process resources. State: Thread ID, program counter, register set, and stack. User-level threads and kernel-level threads.

User Threads Thread management done by user-level threads library. Advantages: Very fast: Does not involve kernel in creation, scheduling, or switching. Disadvantages: When a thread blocks, whole process blocks.

Kernel Threads Supported by the Kernel. Kernel creates, schedules, and switches threads. Advantage: When one thread blocks, the whole process does not have to block. Thus can overlap I/O and computation. Disadvantage: Slower since kernel is involved.

Multithreading Models Many-to-One One-to-One Many-to-Many

Many-to-One Many user-level threads mapped to single kernel thread. Used on systems that do not support kernel threads.

Many-to-One Model

One-to-One Each user-level thread maps to kernel thread. Examples - Windows 95/98/NT/ OS/2

One-to-one Model

Many-to-Many Model 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

Many-to-Many Model

Threading Issues Semantics of fork() and exec() system calls. Thread cancellation.

Pthreads a POSIX standard (IEEE c) API for thread creation and synchronization. API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX operating systems.

Solaris 2 Threads

Solaris Process

Windows 2000 Threads Implements the one-to-one mapping. Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area