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.

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

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.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
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.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Linux (II) Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習.
CSE 451 Section 4 Project 2 Design Considerations.
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.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Today’s topic Inter-process communication with pipes.
Some Example C Programs. These programs show how to use the exec function.
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.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
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.
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.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Pipe-Related System Calls COS 431 University of Maine.
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
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.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
CSCI 330 UNIX and Network Programming
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
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.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
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.
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.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Week 3 Redirection, Pipes, and Background
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Precept 14 : Ish dup() & Signal Handling
CSC 382: Computer Security
Processes in Unix, Linux, and Windows
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)
Processes in Unix, Linux, and Windows
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Andy Wang Operating Systems COP 4610 / CGS 5765
Programming Assignment # 2 – Supplementary Discussion
IPC Prof. Ikjun Yeom TA – Hoyoun
dup, dup2 An existing file descriptor (filedes) is duplicated
Processes in Unix, Linux, and Windows
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

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 sort r By default a command like ps requires that its output goes to standard output i.e., the terminal r The sort command requires that a file be provided as a command line argument (standard input)

First Attempt pid = fork(); if (pid<0) { perror("Problem forking"); exit(1); } else if (pid>0) { /* parent process */ execlp("ps","ps","-le", NULL); perror("exec problem"); exit(1); } else { /* child process */ } execlp("sort","sort",NULL); perror("exec problem"); exit(1); } return(0); }

Example r Why doesn’t this work? m The output of the ps -le goes to the terminal r We want it to be the input to the sort r The diagram on the next page shows the status of the file descriptor tables after the fork m No changes are made in the code related to “First attempt”

Fork and Files Parent File Descriptor table stdin stdout stderr System file table Terminal info stdin stdout stderr Child File Descriptor table Terminal info

Example r First let us m Create shared memory that is to be used by the parent and child processes r This is done using the pipe function r The pipe function is executed before the fork function r The results of ps –le should be put into the shared memory to be used by child for sort r See next slide for code r The slide after code slide depicts the file descriptor table and System File table

Example int main(int argc, char **argv) { int fds[2]; pid_t pid; /* attempt to create a pipe */ if (pipe(fds)<0) { perror("Fatal Error"); exit(1); }

Example Parent File Desc. table fds[0] fds[1] stdin stdout stderr System file table Terminal info Shared mem. info: read Shared mem. Info: write

Example r Each entry in the system file table has information about the “file” which could be the terminal, disk file or pipe (shared memory)

Example System file table Terminal info Shared mem. Info: read Shared mem. Info: write Shared Memory

Example r Let us now add the code for the fork r See next slide for the code

Example /* create another process */ pid = fork(); if (pid<0) { perror("Problem forking"); exit(1); } …………….. What is the status of the file descriptor table

Example Parent File Desc. table fds[0] fds[1] stdin stdout stderr System file table fds[0] fds[1] stdin stdout stderr Child File Desc. table Terminal info Shared mem. Info: read Shared mem. Info: write

Example r We want the output of the ps –le to be put into the shared memory r The sort command should read from the shared memory r How do we get there?

Example r Let us start with the parent r We should close the read end of the pipe since the parent will only write to shared memory. This is done using the following command: close(fds[0]); r What does the parent file descriptor table look like? See next slide m For now we will ignore the child process

Example Parent File Desc. table fds[0]=NULL fds[1] stdin stdout stderr System file table fds[0] fds[1] stdin stdout stderr Child File Desc. table Terminal info Shared mem. info: read Shared mem. info: write

Example r Now we want what would normally go to the standard output to go to the shared memory r This is done with the following code: if ( dup2(fds[1],STDOUT_FILENO)<0) { perror("can't dup"); exit(1); } r The new parent file descriptor table is on the next page

Example Parent File Desc. table fds[0]=NULL fds[1] stdin stdout stderr System file table fds[0] fds[1] stdin stdout stderr Child File Desc. table Terminal info Shared mem. info: read Shared mem. info: write

Example r Now let us look at the child r We should close the write end of the pipe since the child will only read from shared memory. This is done using the following command: close(fds[1]); r What does the child file descriptor table look like? See next slide

Example Parent File Desc. table fds[0]=NULL fds[1] stdin stdout stderr System file table fds[0] fds[1]=NULL stdin stdout stderr Child File Desc. table Terminal info Shared mem. info: read Shared mem. info: write

Example r Now want to set it up so that the child reads from the shared memory r This is done with the following code: if ( dup2(fds[0],STDIN_FILENO)<0) { perror("can't dup"); exit(1); } r The new child file descriptor is on the next page

Example Parent File Desc. table fds[0]=NULL fds[1] stdin stdout stderr System file table fds[0] fds[1]=NULL stdin stdout stderr Child File Desc. table Terminal info Shared mem. info: read Shared mem. info: write

Example r Let us now put it together

Example /* create another process */ pid = fork(); if (pid<0) { perror("Problem forking"); exit(1); } else if (pid>0) { /* parent process */ close(fds[0]); /* close stdout, reconnect to the writing end of the pipe */ if ( dup2(fds[1],STDOUT_FILENO)<0) { perror("can't dup"); exit(1); } execlp("ps","ps","-le", NULL); perror("exec problem"); exit(1);

Example } else { /* child process */ close(fds[1]); if (dup2(fds[0],STDIN_FILENO) < 0) { perror("can't dup"); exit(1); } execlp("sort","sort",NULL); perror("exec problem"); exit(1); } return(0); }