Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

LINUX System Process (additional slides)
4.1 Operating Systems Lecture 11 UNIX Pipes Read Handout "An Introduction to Concurrency..."
UC Santa Barbara Project 1 Discussion Bryce Boe 2011/04/12.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Introduction to Linux (II) Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習.
Advanced Programming in the UNIX Environment Hop Lee.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Operating systems Lab 04 System Call, Nested Fork(),Exec(),Pipe()
Some Example C Programs. These programs show how to use the exec function.
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.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
Slide 1 Interprocess communication. Slide 2 1. Pipes  A form of interprocess communication Between processes that have a common ancestor  Typical use:
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
OPERATING SYSTEMS 12 - FILES PIETER HARTEL 1. Files  Properties  Long term existence of data  Sharable between processes  Access control  Operations.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
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.
Creating and Executing Processes
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.
Chapter 6 UNIX Special Files Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
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.
Pipe-Related System Calls COS 431 University of Maine.
Operating Systems Process Creation
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
1 제 12 장 프로세스 사이의 통신. 2 Objectives describe how pipes are used for IPC define the two kinds of pipes use the system calls used with pipes network programming.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
資訊系統原理作業二補充說明 fork() – create a child process –#include –pid_t fork(void) Returns: –0 in child –process ID of child (>0) in parent –-1 on error.
Pipes Pipes are an inter-process communication mechanism that allow two or more processes to send information to each other.
Interprocess Communication
Process Management Azzam Mourad COEN 346.
CSCI 330 UNIX and Network Programming
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
OPERATING SYSTEMS 3 - PROCESSES PIETER HARTEL 1. Principle of concurrency - giving a process the illusion that it owns the whole machine  A process has:
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Week 3 Redirection, Pipes, and Background
LINUX System Process in Detail
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Programming Assignment 1
Fork and Exec Unix Model
Pipe.
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
LINUX System Programming with Processes (additional)
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Programming Assignment # 2 – Supplementary Discussion
IPC Prof. Ikjun Yeom TA – Hoyoun
System Calls: pipes CSRU3130 Ellen Zhang.
dup, dup2 An existing file descriptor (filedes) is duplicated
Section 2 Processes January 27rd, 2017 Taught by Joshua Don.
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available file descriptor With dup2, we specify the value of the new descriptor with the filedes2 argument If filedes2 is already open, it is first closed. Ex) dup2(fd,STDOUT_FILENO); 1 #include int dup(int filedes); int dup2(int filedes, int filedes2); Both return : new file descriptor if OK, -1 on error

example dup(1); … fd 0 fd 1 fd 2 fd 3 fd 4 File descriptor tableFile table entry

3 #include // redirection.c #include int main(int argc, char* argv[]) { char *path = "/bin/ls"; char *arg0 = "ls"; pid_t pid; int fd; int status; if (argc!=2) { printf("wrong command. ex) a.out filename\n"); exit(0); } pid = fork(); if (pid == 0) { fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU); dup2(fd, STDOUT_FILENO); close(fd); if (execl(path, arg0, NULL) == -1) perror("execl"); } else { close(fd); wait(&status); }

Pipe $ who | sort

pipe Two file descriptors are returned through the fd argument fd[0]: can be used to read from the pipe, and fd[1]: can be used to write to the pipe Anything that is written on fd[1] may be read by fd[0]. This is of no use in a single process. However, between processes, it gives a method of communication The pipe() system call gives parent-child processes a way to communicate with each other. 5 #include int pipe(int filedes[2]); return : 0 if OK, -1 on error

pipe filedes[0] is open for reading and filedes[1] is open for writing. Output of filedes[1] is the input for filedes[0] 6

pipe after fork user process fd[0]fd[1] pipe kernel child process fd[0]fd[1] pipe kernel parent process fd[0]fd[1]

Page 8 Pipes parent  child: parent closes fd[0] child closes fd[1] parent  child: parent closes fd[1] child closes fd[0] pipe kernel fd[1] parent fd[0] child pipe kernel fd[0] parent fd[1] child

Pipe example 9 #include // pipe2.c #define READ 0 #define WRITE 1 char* phrase = "Stuff this in your pipe and smoke it"; main( ) { int fd[2], bytesRead; char message[100]; pipe(fd); if (fork() == 0) { // child close(fd[READ]); write(fd[WRITE], phrase, strlen(phrase)+1); fprintf(stdout, "[%d, child] write completed.\n", getpid()); close(fd[WRITE]); } else { // parent close(fd[WRITE]); bytesRead = read(fd[READ], message, 100); fprintf(stdout, "[%d, parent] read completed.\n", getpid()); printf("Read %d bytes: %s\n", bytesRead,message); close(fd[READ]); }

10 #include // pipe.c #include int main(int argc, char *argv[]) { char *path = "/bin/ls"; char *arg0 = "ls"; pid_t pid; int pipefd[2]; int status; pipe(pipefd); pid = fork(); if (pid == 0) { dup2(pipefd[1], STDOUT_FILENO); close(pipefd[0]); close(pipefd[1]); if (execl(path, arg0, NULL) == -1) perror("execl"); } else { if (fork() == 0) { dup2(pipefd[0], STDIN_FILENO); close(pipefd[0]); close(pipefd[1]); if (execl("/bin/cat", "cat", NULL) == -1) perror("execl cat"); } else { close(pipefd[0]); close(pipefd[1]); wait(&status); } Another Example