Programming Assignment # 2 – Supplementary Discussion

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

4.1 Operating Systems Lecture 11 UNIX Pipes Read Handout "An Introduction to Concurrency..."
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
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.
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.
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.
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.
CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT I/O Redirection.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
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.
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.
Operating Systems Process Creation
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
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
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Process Management Azzam Mourad COEN 346.
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.
Shell Execution Basic: fork, child execs, parent waits code of program in box –RC == return value from fork() Call fork RC=0 Call exec Subsequent instructions.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Lecture 5 Systems Programming: Unix Processes: Orphans and Zombies
Week 3 Redirection, Pipes, and Background
Process API COMP 755.
Operating Systems Moti Geva
Robust I/O package Chapter 11 practice problems
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Precept 14 : Ish dup() & Signal Handling
LINUX System : Lecture 8 Programming with Processes
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Programming Assignment 1
Processes in Unix, Linux, and Windows
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
Processes in Unix, Linux, and Windows
Inter-Process Communication
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Tutorial 3 Tutorial 3.
Andy Wang Operating Systems COP 4610 / CGS 5765
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Process Programming Interface
I/O and Process Management
IPC Prof. Ikjun Yeom TA – Hoyoun
CSCI 380: Operating Systems William Killian
dup, dup2 An existing file descriptor (filedes) is duplicated
Processes in Unix, Linux, and Windows
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

Programming Assignment # 2 – Supplementary Discussion Tanzir Ahmed CSCE 313 Fall 2018

Programming Assignment # 2 Main Challenges (Still after fork + exec) I/O Redirection Piping To solve these challenges, we need the topics: File I/O basics Inter Process Communication using pipes

File I/O – File Descriptors int main () { int fd; // file descriptor char* buf [] = “file content”; fd = open (“foobar.txt”, O_CRAEATE|O_WRONLY); write (fd, buf, strlen (buf)); close (fd); fd = open(“foobar.txt”, O_RDONLY, 0); read(fd, &c, 1); printf(“c=%c\n”, c); close (fd); return 0; } Descriptor table [one table per process] Every process has a file descriptor table, where there are 3 default entries to begin with: Standard input, output, and error stdin fd 0 stdout fd 1 stderr fd 2 fd 3 fd

I/O Redirection fd 0 fd 1 fd 2 fd 3 fd 4 fd 0 fd 1 fd 2 fd 3 fd 4 Question: How does a shell implement I/O redirection? unix> ls > foo.txt Answer: By calling the dup2(source, destination) function Copies (per-process) descriptor table entry source to entry destination 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

Implementing “ls –la>foo.txt” #include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> int main () { int fd = open ("foo.txt", O_CREAT|O_WRONLY, S_IRUSR | S_IWUSR); dup2 (fd, 1); // overwriting stdout with the new file execlp ("ls", "ls", "-l", "-a", NULL); // now execute return 0; }

IPC Pipe BEFORE pipe AFTER pipe 0 1 2 3 4 0 1 2 0 1 2 0 1 2 3 4 Process has some usual files open Kernel creates a pipe and sets file descriptors

IPC Pipe - Method Connects the two fds as pipe Is this of any use at all ??? Connects the two fds as pipe

Pipe Between Two Processes int main () { int fds [2]; pipe (fds); // connect the pipe if (!fork()){ // on the child side char * msg = "a test message"; printf ("CHILD: Sending %s\n", msg); write (fds [1], msg, strlen(msg)+1); }else{ char buf [100]; read (fds [0], buf, 100); printf ("PARENT: Received %s\n", buf); } return 0; child parent

Shell Piping Example: “ls –l | grep soda” Meaning of the command: Find all files that has the string “soda” in the filename and show detailed properties of those files How many processes do we have to run (in addition to our shell process)? Process # 1: To run “ls –l” Process # 2: To run “grep soda” What else do we need so that the process #1 sends its output to process #2 Idea: If we can connect stdout of p1 to stdin of p2, we are done!! Step 1: Redirect stdout of p1 to a file descriptor fd1 Step 2: Redirect stdin of p2 to a another file descriptor fd2 Step 3: Now, pipe fd1 and fd2 together so that fd1 is the “write side” and fd2 is the “read side”

Shell Piping: “ls –l | grep soda” void main () { int fds [2]; pipe (fds); // connect the pipe if (!fork()){ // on the child side dup2 (fds[1], 1); // redirect stdout to pipeout execlp ("ls", "ls", "-l", NULL); }else{ dup2 (fds[0], 0); // redirect stdin to pipe in execlp (“grep", “soda", "-l", NULL); } Step 3 Step 1 Step 2

Difficulty with the Previous You have to hard code the stages 2 pipes (3 pipe separated portions) will change the code completely: cannot generalize this code Will require us to have multiple pairs of file descriptors Can we think of a more general way of doing the same? Child process (redirects stdout) ls –l|grep this|grep that|… Parent process (redirects stdin)

A General Pipe Portion int fd [2]; pipe (fd); if (fork() == 0){ dup2 (fd[1], 1); // overwriting stdout to the pipe’s WRITE end close (fd[0]); // close unused pipe end execute (portion[i]); }else{ wait (0); close (fd[1]); // close unused pipe end dup2 (fd [0],0); // overwriting stdin to pipe’s READ end for the later portions } How are the stack allocated fd[2] being visible in the later portions? Shouldn’t the #of fd’s be 2*(#of portions) Also, should the last portion do the same thing (i.e., redir its stdout)? You also need to close the unused pipe ends Otherwise, your program will deadlock

Over All – Repeat the Following Read the line full of command Split the line into portions by “|” For each portion except the last: Pipe() Fork() a child Redirect the child’s STDOUT to Write End of pipe Execute the current portion under this child Under the parent, just redirect the STDIN to the Read End of the pipe