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); }"> 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); }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Shell (Addendum)

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 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)

3 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); }

4 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”

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

6 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

7 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); }

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

9 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)

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

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

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

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

14 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?

15 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

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

17 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

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

19 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

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

21 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

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

23 Example r Let us now put it together

24 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);

25 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); }


Download ppt "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."

Similar presentations


Ads by Google