Lecture 17. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture17/ $ cp r /home/hwang/cs375/lecture17/*.

Similar documents
Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

IPC and Unix Special Files

CS 351 Sample Final Exam, Spring 2015

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

프로세스간통신 (Interprocess communication) i 숙명여대창병모

CSE 410: Systems Programming

Interprocess Communication E. Im

File Descriptors and Piping

Processes COMPSCI 386

Process Management! Goals of this Lecture!

Process Creation in UNIX

Pointers about pointers. Announcements. Pointer type. Example

Topics. Unix Pipes (CSE 422S) In A Nutshell. Ken Wong Washington University. Pipe (Unnamed FIFO) in the Shell.

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*.

Process Management 1

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

Process Management! Goals of this Lecture!

St. MARTIN S ENGINEERING COLLEGE Dhulapally,Secunderabad DEPARTMENT OF INFORMATION TECHNOLOGY Academic year

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Lecture 20. Log into Linux. Copy directory /home/hwang/cs375/lecture20 Project 5 due today. Project 6 posted, due Tuesday, April 8. Questions?

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

CSCI 4061: Pipes and FIFOs

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Programmation Systèmes Cours 4 IPC: FIFO

Outline. Relationship between file descriptors and open files

Programmation Système Cours 4 IPC: FIFO

Files and Directories

Machine Problem 3: UNIX System Programming. 100 points + 10 bonus points Due date: To Be Announced

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

UNIT I Linux Utilities

(MCQZ-CS604 Operating Systems)

COP 4604 UNIX System Programming IPC. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Mid Term from Feb-2005 to Nov 2012 CS604- Operating System

Design Overview of the FreeBSD Kernel CIS 657

Lecture 23: System-Level I/O

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

read(2) There can be several cases where read returns less than the number of bytes requested:

Outline. CSCI 4730/6730 Systems Programming Refresher. What is a Pipe? Example: Shell Pipes. Programming with Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I

Creating a Shell or Command Interperter Program CSCI411 Lab

UNIX Kernel. UNIX History

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

The Shell, System Calls, Processes, and Basic Inter-Process Communication

CS240: Programming in C

Basic OS Progamming Abstrac2ons

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

CS 550 Operating Systems Spring Inter Process Communication

NETWORK AND SYSTEM PROGRAMMING

Workshop on Inter Process Communication Solutions

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

ADVANCED OPERATING SYSTEMS

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

Inter-process Communication. Using Pipes

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

CSI Module 2: Processes

Basic OS Progamming Abstrac7ons

CS 3733 Operating Systems

Inter-Process Communication

628 Lecture Notes Week 4

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

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

CS 5523: Operating Systems

Machine Problem 3: UNIX System Programming. 100 points (Basic level only) Due date: TBA

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

W4118: OS Overview. Junfeng Yang

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Operating Systemss and Multicore Programming (1DT089)

Department of Computer Science and Technology, UTU 2014

CS2028 -UNIX INTERNALS

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

UNIX input and output

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

File I/O and File Systems

CL020 - Advanced Linux and UNIX Programming

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Lecture 2: February 6

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ECE 650 Systems Programming & Engineering. Spring 2018

Transcription:

Lecture 17 Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture17/ $ cp r /home/hwang/cs375/lecture17/*. Both subdirectories have makefiles that will make all the programs. The "unnamed" subdirectory has programs using pipe( ), while the "named" subdirectory has programs using FIFOs (named pipes). Reminder: Project 5 due on Thursday. Questions? Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 1

Outline UNIX IPC Methods An Introduction to (unnamed) Pipes IPC via Pipes Named Pipes (FIFOs) Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 2

UNIX IPC UNIX allows several methods of interprocess communication (IPC). Here are ten: shared file pointers: One process positions a file pointer (using lseek( )); a second process reads the pointer position (also lseek( )). The position is the (integer) data to be communicated. (This is rarely used. The method is restricted to related processes. Synchronization also is required.) signals: Signals can be used for notifications, but cannot be used to transfer data. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 3

UNIX IPC process tracing: This is commonly used by debuggers, but is too complicated for normal IPC. files: Passing data via a file is the most common form of IPC, but difficult to use with concurrent processes. Files may get huge when processes run for long periods. pipes (unnamed): They solve the synchronization problem that exists with files. Processes must be related to use pipes. Reads and writes may not be atomic on all systems (so they cannot be used with multiple readers or writers); pipes also are slow. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 4

UNIX IPC FIFOs (named pipes): They solve all pipe problems except that they may still be too slow. semaphores: They are used for process synchronization or exclusive access to resources. message queues: Allow small message packets to be transferred synchronously between processes. shared memory: The fastest method of IPC. Semaphores or messages may be used for synchronization. sockets: Used for network communication between processes on different (or the same) hosts. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 5

Introduction to Pipes We will discuss IPC via unnamed pipes first. You are familiar with pipes as a shell facility: $ who sort pr l 24 Here three processes are connected via two pipes. The standard output of the process on the left of the is connected to the standard input of the process on the right. Pipes use the standard UNIX read/write calls for passing data between processes. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 6

The popen( ) Function The popen( ) function is built on top of the lower level pipe( ) routine. It receives a command string and an open mode, and returns a stdio FILE * stream (so stdio routines must be used): FILE *rf = popen("ls l", "r"); while(fgets(buf,sizeof(buf),rf)!=null) { /* process lines */ } Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 7

The pipe( ) Routine popen( ) invokes a shell to execute the command and is expensive in terms of resources. pipe( ) does not require that a shell be started and also gives us more control over the reading and writing of data. pipe( ) returns two file descriptors in an array. We read from one and write to the other. See pipe_xmpl1.cpp pdf[1] pdf[0] Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 8

The pipe( ) Routine int pfd[2]; if(pipe(pfd) == 1) error(strerror(errno)); // read from pfd[0]/write to pfd[1] string msg = "Hello world!"; // we can write a short message to // the pipe and not worry about the // write blocking and process deadlock write(pfd[1], msg.c_str(), msg.length()); count = read(pfd[0], buffer, sizeof(buffer)); buffer[count] = '\0'; cout << buffer << endl; Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 9

Pipe Characteristics Several of the system routines act slightly differently when operating on a file descriptor associated with a pipe instead of a file descriptor associated with a file. write( ) will normally block on a full pipe. Pipes are at least 4096 bytes on all UNIX systems. (On Linux >2.6.11 the capacity is 65536 bytes.) There are no partial writes. To get an end-offile on a read, you must close the write end of the pipe. (pipe_xmpl2.cpp demonstrates a blocked write.) Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 10

Pipe Characteristics read( ) will block until at least one byte is present unless the O_NDELAY flag is set (see fcntl( )) or the writing descriptor is closed. The byte count argument to read( ) will not necessarily be satisfied. The lseek( ) routine can not be used with pipes. You have to read the pipe to access the data and once data is read there is no way to put it back. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 11

IPC Via a Pipe pipe( ) is used for IPC between related processes. (It is not possible for an unrelated process to get access to a system file descriptor owned by another process, but children inherit all file descriptors from their parents.) See pipe_xmpl3.cpp Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 12

IPC Via a Pipe A process normally uses only one end of the pipe. Always close the end you are not using! Pipes are unidirectional; for bidirectional process communication you need two pipes. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 13

Pipes to exec( )'d Programs We rely on child processes inheriting the pipe file descriptors to allow IPC via pipes. pipe(pfd); // both open to both processes if (fork() == 0) { // child close(pfd[0]); // close read end write(pfd[1], msg, bufsize); exit(0); } // parent close(pfd[1]); // close write end n = read(pfd[0],msg, bufsize); Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 14

Pipes to exec( )'d Programs Normally we exec a program in the child. File descriptors stay open across an exec (unless fcntl( ) is used to set the close-on-exec flag), but the exec'd process will not have access to the array containing the file descriptor numbers. One method used to get around this problem is to pass the descriptors as arguments to the program. (See wtr_xmpl1.cpp and rdr_xmpl1.cpp) Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 15

dup( ) and dup2( ) Routines Most programs are designed to read from standard input and write to standard output. How do communicate with such a program via a pipe? We use dup( ) or dup2( ). The dup(oldfd) routine duplicates an existing file descriptor. The new file descriptor returned by dup( ) is guaranteed to be the lowest numbered available. dup2(oldfd, newfd) duplicates oldfd over newfd, closing newfd first, if necessary. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 16

dup( ) and dup2( ) Routines See wtr_xmpl2.cpp and rdr_xmpl2.cpp pipe(pfd); if(fork() == 0) { close(0); // so 0 is available dup(pfd[0]); // will dup over 0 close(pfd[0]); close(pfd[1]); execl("rdstdin", "rdstdin", (char *)0); cerr << "exec failure" << endl; exit(0); } close(pfd[0]); // close read end of pipe n = write(pfd[1], msg, bufsize); close(pfd[1]); Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 17

dup( ) and dup2( ) Routines See wtr_xmpl3.cpp for a dup2( ) example. pipe(pfd); if(fork() == 0) { dup2(pfd[0], 0); // will dup over 0 close(pfd[0]); close(pfd[1]); execl("rdstdin", "rdstdin", (char *)0); cerr << "exec failure" << endl; exit(0); } close(pfd[0]); // close read end of pipe n = write(pfd[1], msg, bufsize); close(pfd[1]); Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 18

Named Pipes Named pipes (FIFOs) are special types of files that can be used for communication between unrelated processes. (They are not real files and take up no disk space.) A FIFO combines features of files and pipes. It has a name and may be opened for reading or writing by any process with appropriate permissions. Once opened, a FIFO acts more like a pipe. Data, once read, cannot be read again, nor does lseek( ) work. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 19

Named Pipes FIFOs can be created from the command line using the mkfifo command (or mknod): $ mkfifo myfifo $ ls l myfifo prw rw r 1 hwang hwang 0 Oct 25 16:40 myfifo The FIFO can be used to pass data between programs: $ cat /etc/passwd > myfifo & $ cat myfifo $ gnuplot persist < myfifo & $ echo "set nokey; plot sin(x)" > myfifo Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 20

The mkfifo( ) Routine From within a program, a FIFO can be created using mkfifo( ) (or mknod( )): mkfifo("/tmp/myfifo", 0777); The second argument is the desired FIFO permissions. mkfifo( ) returns -1 on error. The FIFO will exist until deleted (with rm at the command line or by calling the unlink( ) routine from within a program). Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 21

FIFO Characteristics An open( ) for read-only on a FIFO will block until another process opens the FIFO for writing. An open( ) for write-only will block until a reader opens the FIFO. This allows synchronization before data transmission. (The open( ) routine will not block if the O_NONBLOCK flag is used in the open( ).) read( ) will block if no data is present. write( ) will block on a full FIFO. (But not if O_NONBLOCK is used.) Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 22

FIFO Characteristics Writes of less than PIPE_BUF (65536 on Linux >2.6.11) are guaranteed to be atomic, i.e. the write can not be interrupted. We can have multiple writers to a FIFO if the messages are smaller in size than PIPE_BUF, and the messages will not be intermixed. FIFOs are often used for client-server communication between multiple clients and a single server. See fifo_wtr.cpp, fifo_rdr.cpp, server.cpp, and client.cpp Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 23

Example Writer Code // Error checking omitted for brevity const char *FIFO_NAME = "/tmp/db_server"; // Check for existence if(access(fifo_name, F_OK) == 1) { // does not exist, create it mkfifo(fifo_name, 0777); } // block on open until reader opens too wfd = open(fifo_name, O_WRONLY); count = write(wfd, buffer, length); close(wfd); unlink(fifo_name); Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 24

Example Reader Code // Error checking omitted for brevity // We wait on server to create fifo while(true) { rfd = open(fifo_name, O_RDONLY); if(rfd == 1) { if(errno == ENOENT) { // does not exist, wait then try again sleep(1); continue; } else { exit(1); } } } // end while count = read(rfd, buffer, length); Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 25

In-class Exercise Copy pipe_xmpl3.cpp. Comment out the line in the child that closes the write end of the pipe. Compile and run the program. Why doesn't the child exit? (Be sure to kill the child manually using the kill command.) Copy and modify pipe_xmpl3.cpp so that the parent reads /etc/passwd and passes all the data to the child via the pipe. The child should read from the pipe and write the data to standard output. Tuesday, October 26 CS 375 UNIX System Programming - Lecture 17 26