IPC Prof. Ikjun Yeom TA – Hoyoun

Slides:



Advertisements
Similar presentations
Pseudo Terminals Concept Application APIs. Overview A pseudo terminal (PTY) is a user level program that appears to be a terminal device to another program.
Advertisements

Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Processes and Pipes. 2 "He was below me. I saw his markings, manoeuvred myself behind him and shot him down. If I had known it was Saint-Exupery, I.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CSE 451 Section 4 Project 2 Design Considerations.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Today’s topic Inter-process communication with pipes.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Operating Systems Lecture 10. Agenda for Today Review of previous lecture Input, output, and error redirection in UNIX/Linux FIFOs in UNIX/Linux Use of.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Operating Systems Process Creation
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Pipes Pipes are an inter-process communication mechanism that allow two or more processes to send information to each other.
CS 105 “Tour of the Black Holes of Computing”
Interprocess Communication
4061 Session 13 (2/27). Today Pipes and FIFOs Today’s Objectives Understand the concept of IPC Understand the purpose of anonymous and named pipes Describe.
CSCI 330 UNIX and Network Programming
OS Labs 2/25/08 Frans Kaashoek MIT
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
Lecture 5 Systems Programming: Unix Processes: Orphans and Zombies
Week 3 Redirection, Pipes, and Background
Robust I/O package Chapter 11 practice problems
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
LINUX System : Lecture 8 Programming with Processes
Inter-Process Communication Pipes Moti Geva
Introduction to Operating Systems File I/O
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Programming Assignment 1
Fork and Exec Unix Model
Pipe.
File redirection ls > out
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
LINUX System Programming with Processes (additional)
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Programming Assignment # 2 – Supplementary Discussion
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Reference: Advanced Programming in the UNIX Environment, Third Edition, W. Richard Stevens and Stephen A. Rago, Addison-Wesley Professional Computing Series,
I/O and Process Management
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Processes Prof. Ikjun Yeom TA – Mugyo
CSCI 380: Operating Systems William Killian
dup, dup2 An existing file descriptor (filedes) is duplicated
FILE I/O File Descriptors I/O Efficiency File Sharing
CS 105 “Tour of the Black Holes of Computing”
Chapter 3: Processes CSS503 Systems Programming
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

IPC Prof. Ikjun Yeom TA – Hoyoun Lee(hy2930hy@naver.com) TA – Bedeuro Kim(secbdrkim@naver.com) Computer Network Laboratory Sungkyunkwan University http://comnet.skku.edu

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

Everything is a file Actually, “Everything is a file descriptor” Pros Can reuse tools, APIs on a wide range of resources Cons Not a fast or portable approach Communication using file interface?

Open Files in Kernel How the Unix kernel represents open files? 3-levels Descriptor table 1 table per process Pointer to entry in the “file table” File table Shared by all processes Current file position, mode, reference count, pointer to entry in the “v-node table” v-node table Information about file itself (size, permission, …)

[one table per process] [shared by all processes] Open Files in Kernel (2) How the Unix kernel represents open files? fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table [one table per process] Open file table [shared by all processes] v-node table File pos refcnt=1 ... stderr stdout stdin File access File size File type File A (terminal) File B (disk) Info in stat struct

Open Files in Kernel (3) Calling open() twice with the same filename Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] File A fd 0 File access fd 1 File pos File size fd 2 refcnt=1 File type fd 3 fd 4 ... ... File B File pos refcnt=1 ...

Open Files in Kernel (4) Calling fork() Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] Parent’s table File A fd 0 File access fd 1 File pos File size fd 2 refcnt=2 File type fd 3 fd 4 ... ... Child’s table File B File access fd 0 File size File pos fd 1 File type fd 2 refcnt=2 fd 3 ... ... fd 4

Open Files in Kernel (5) What will be the result? temp.txt int main(void) { char c; int fd = open(“temp.txt", O_RDONLY); if (fork() == 0) { assert(read(fd, &c, 1) >= 0); exit(0); } else { wait(NULL); printf(“c=%c\n”,c); } return 0; #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <sys/wait.h> #include <assert.h> #include <stdio.h> temp.txt swexperiment

I/O Redirection Q: How does a shell implement I/O redirection? $ ls > foo.txt A: By calling the dup2(oldfd, newfd) function. Copies (per-process) descriptor table entry oldfd to entry newfd Descriptor table before dup2(4,1) Descriptor table after dup2(4,1) a b fd 0 fd 1 fd 2 fd 3 fd 4 b fd 0 fd 1 fd 2 fd 3 fd 4

I/O Redirection Example (1) Before calling dup2(4,1), stdout (descriptor 1) points to a terminal and descriptor 4 points to an open disk file. Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] File A fd 0 File access fd 1 File pos File size fd 2 refcnt=1 File type fd 3 fd 4 ... ... File B File access File size File pos File type refcnt=1 ... ...

I/O Redirection Example (2) After calling dup2(4,1), stdout is not redirected to the disk file pointed at by descriptor 4. Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] File A fd 0 File access fd 1 File pos File size fd 2 refcnt=0 File type fd 3 fd 4 ... ... File B File access File size File pos File type refcnt=2 ... ...

Pipes Pipes Limitations The oldest form of UNIX IPC (Inter-process Communication) and provide by all Unix systems. IPC using ‘file interface’ Limitations Half-duplex: data flows only in one direction. Data only can be read once. Two pipes Anonymous pipe No name Named pipe We can see it with a file-system

Anonymous Pipe (1) int pipe (int fd[2]); Two file descriptors are returned through the fd argument fd[0]: open for reading fd[1]: open for writing The output of fd[1] is the input for fd[0].

Anonymous Pipe (2) parent => child: parent <= child: parent closes fd[0]; child closes fd[1]; parent <= child: parent closes fd[1]; child closes fd[0];

Anonymous Pipe (3) parent => child: parent <= child: parent closes fd[0]; child closes fd[1]; parent <= child: parent closes fd[1]; child closes fd[0];

Reading/Writing Pipe When one end of a pipe is closed, reading from a pipe returns an end of file. writing to a pipe causes SIGPIPE is generated and the write returns an error (EPIPE). fstat function returns a file type of FIFO for the pipe file descriptors (can be tested by S_ISFIFO macro) You should close unused file descriptors!

Using Anonymous Pipe #include <unistd.h> #define MAXLINE 80 int main(void) { int n, fd[2]; pid_t pid; char line[MAXLINE]; if(pipe(fd) < 0) exit(1); if((pid = fork()) < 0) exit(2); if (pid > 0) { /* parent */ close(fd[0]); write(fd[1], "hello world\n", 12); } else { /* child */ close(fd[1]); n = read(fd[0], line, MAXLINE); write(1, line, n); } exit(0);

Named Pipe (FIFO) int mknod (const char *path, mode_t mode, dev_t dev) mknod (“path”, S_IFIFO, 0); /usr/bin/mkfifo program can also be used to make FIFOs on the command line.

Using FIFOs Opening a FIFO Reading/Writing a FIFO An open for read(write)-only blocks until some other process opens the FIFO for writing(reading). Reading/Writing a FIFO Writing to a FIFO that no process has open for reading causes SIGPIPE to generate. When the last writer for a FIFO closes the FIFO, an end of file is generated for the reader of the FIFO. PIPE_BUF: the maximum amount of data that can be written atomically to a FIFO (without being interleaved among multiple writers).

Use of FIFOs (1) Duplicating a Stream Shell commands to pass data from one shell pipeline to another without creating intermediate temporary files $ mkfifo fifo1 $ cat < fifo1 > output.txt & $ cat < input.txt >fifo1 Input.txt Output.txt cat cat fifo1

Use of FIFOs (1) Duplicating a Stream Shell commands to pass data from one shell pipeline to another without creating intermediate temporary files $ mkfifo fifo1 $ prog3 < fifo1 & $ prog1 < infile | tee fifo1 | prog2 infile prog3 fifo1 prog1 tee prog2

Use of FIFOs (2) Client-server Communication A client-server application to pass data between the client and server on the same machine. Clients write to a “well-known” FIFO to send a request to the server.

Summary IPC (Inter-Process Communication) Signal Pipe Named pipe (FIFO) Shared memory Semaphore Sockets …

Exercise Make C programs run the following tasks: $ echo "124 * (42 + 3) % 17" | bc - main -> pipe -> fork dup2 -> exec family  echo dup2 -> exec family  bc $ which echo $ which bc