Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.

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

© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
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 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.
Project 1. Process ID (pid) Synopsis #include pid_t getpid(void) – returns the pid of the currently running process. pid_t getppid(void) – returns the.
Introduction to Linux (II) Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習.
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.
Today’s topic Inter-process communication with pipes.
Operating systems Lab 04 System Call, Nested Fork(),Exec(),Pipe()
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.
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.
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.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
File Handling Spring 2013Programming and Data Structure1.
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.
Minishell InKwan Yu Topics Unix System calls waitpid() pipe() dup2() C function calls strtok() strcmp() Minishell Software Enginnering.
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.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
File IO and command line input CSE 2451 Rong Shi.
Chapter 71 Deadlock Detection revisited. Chapter 72 Message Passing (see Section 4.5 in Processes Chapter)  A general method used for interprocess communication.
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.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Operating Systems Process Creation
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
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
CSCI 330 UNIX and Network Programming
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Week 3 Redirection, Pipes, and Background
Robust I/O package Chapter 11 practice problems
Precept 14 : Ish dup() & Signal Handling
Command Line Arguments
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Fork and Exec Unix Model
Pipe.
File redirection ls > out
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Andy Wang Operating Systems COP 4610 / CGS 5765
Programming Assignment # 2 – Supplementary Discussion
Command Line Parameters
IPC Prof. Ikjun Yeom TA – Hoyoun
dup, dup2 An existing file descriptor (filedes) is duplicated
Intro to the Shell with Fork, Exec, Wait
Lecture 5 review At the beginning of a program, what is the value of fd after ‘fd = open(…);’ is executed? What is file no 0? What is file no 1? What is.
System Programming: Process Management
Presentation transcript:

Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.

Forming a Ring of One Process /* Example 4.1 */ #include <unistd.h> int fd[2]; pipe(fd); dup2(fd[0], STDIN_FILENO); dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]);

One Process – Results of pipe [1] [0] file descriptor table [0] standard input [1] standard output [2] standard error [3] pipe a read [4] pipe a write [2] A [4] [3] pipe a

One Process – Results after dup2s file descriptor table [0] pipe a read [1] pipe a write [2] standard error [3] [4] [2] [0] A [1] [4] [3] pipe a

One Process – Results after closes file descriptor table [0] pipe a read [1] pipe a write [2] standard error [2] [0] A [1] pipe a

Forming a Ring of Two Processes /* Example 4.2 */ #include <unistd.h> int fd[2]; pid_t haschild; pipe(fd); dup2(fd[0], STDIN_FILENO); dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); pipe(fd); if (haschild = fork()) dup2(fd[1], STDOUT_FILENO); /* parent redirects std output */ else dup2(fd[0], STDIN_FILENO); /* child redirects std input */ close(fd[0]); close(fd[1]); ***Start with a ring of one process.***

Two Processes – After Second Pipe [2] Process A file descriptor table [0] pipe a read [1] pipe a write [2] standard error [3] pipe b read [4] pipe b write A [0] [4] [3] [1] pipe a pipe b

Two Processes – After fork Process A file descriptor table [0] pipe a read [1] pipe a write [2] standard error [3] pipe b read [4] pipe b write [2] A [0] [4] [3] [1] Process B file descriptor table [0] pipe a read [1] pipe a write [2] standard error [3] pipe b read [4] pipe write pipe a pipe b [3] [0] [1] [4] B [2]

Two Processes – After Second dup2s Process A file descriptor table [0] pipe a read [1] pipe b write [2] standard error [3] pipe b read [4] [2] A [1] [0] [4] [3] Process B file descriptor table [0] pipe b read [1] pipe a write [2] standard error [3] [4] pipe write pipe a pipe b [0] [1] [3] [4] B [2]

Two Processes – After Second Closes Process A file descriptor table [0] pipe a read [1] pipe b write [2] standard error [2] A [1] [0] Process B file descriptor table [0] pipe b read [1] pipe a write [2] standard error pipe a pipe b [0] [1] B [2]

Forming a Ring of N Processes Start with a ring of N-1 processes Have the last process in the N-1 ring perform a pipe Have the last process in the N-1 ring perform a fork Have the parent perform a dup2 of fd[1] with standard output Have the child perform a dup2 of fd[0] with standard input Close fd[0] and fd[1] for both processes

Creating a Ring of N Processes – Part 1 /* Program 4.1 */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> /* Sample C program for generating a unidirectional ring of processes.Invoke this program with a command-line arg ument indicating the number of processes on the ring. Communication is done via pipes that connect the standard output of a process to the standard input of its successor on the ring. After the ring is created, each process identifies itself with its process ID and the process ID of its parent. Each process then exits. */ void main(int argc, char *argv[ ]) { int i; /* number of this process (starting with 1) */ int childpid; /* indicates process should spawn another */ int nprocs; /* total number of processes in ring */ int fd[2]; /* file descriptors returned by pipe */ int error; /* return value from dup2 call */ /* check command line for a valid number of processes to generate */ if ( (argc != 2) || ((nprocs = atoi (argv[1])) <= 0) ) { fprintf (stderr, "Usage: %s nprocs\n", argv[0]); exit(1); } /* connect std input to std output via a pipe */ if (pipe (fd) == -1) { perror("Could not create pipe"); exit(1); } if ((dup2(fd[0], STDIN_FILENO) == -1) || (dup2(fd[1], STDOUT_FILENO) == -1)) { perror("Could not dup pipes"); exit(1); } if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) { perror("Could not close extra descriptors"); exit(1); }

Creating a Ring of N Processes – Part 2 /* create the remaining processes with their connecting pipes */ for (i = 1; i < nprocs; i++) { if (pipe (fd) == -1) { fprintf(stderr,"Could not create pipe %d: %s\n", i, strerror(errno)); exit(1); } if ((childpid = fork()) == -1) { fprintf(stderr, "Could not create child %d: %s\n", i, strerror(errno)); exit(1); } if (childpid > 0) /* for parent process, reassign stdout */ error = dup2(fd[1], STDOUT_FILENO); else error = dup2(fd[0], STDIN_FILENO); if (error == -1) { fprintf(stderr, "Could not dup pipes for iteration %d: %s\n", i, strerror(errno)); exit(1); } if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) { fprintf(stderr, "Could not close extra descriptors %d: %s\n", i, strerror(errno)); exit(1); } if (childpid) break; } /* say hello to the world */ fprintf(stderr,"This is process %d with ID %d and parent id %d\n", i, (int)getpid(), (int)getppid()); exit (0); } /* end of main program here */