Execution Strategy for Your Shell

Execution Strategy for Your Shell - // Last simple command...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Execution Strategy for Your Shell Parent process does all the piping and redirection before forking the processes. The children will inherit the redirection. The parent needs to save input/output and restore it at the end. Stderr is the same for all processes a | b | c | d > outfile < infile
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Execution Strategy for Your Shell execute(){ //save in/out int tmpin=dup(0); int tmpout=dup(1); //set the initial input int fdin; if (infile) { fdin = open(infile,……); } else { // Use default input fdin=dup(tmpin); int ret; int fdout; for(i=0;i<numsimplecommands; i++) { //redirect input dup2(fdin, 0); close(fdin); //setup output if (i == numsimplecommands-1){
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // Last simple command if(outfile){ fdout=open(outfile,); } Execution Strategy for Your Shell else { // Not last //simple command //create pipe int fdpipe[2]; pipe(fdpipe); fdout=fdpipe[0]; fdin=fdpipe[1]; }// if/else // Redirect output dup2(fdout,1); // Create child process ret=fork(); if(ret==0) { execvp(scmd[i].args[0], scmd[i].args); perror(execvp); _exit(1); } } // for Execution Strategy for Your Shell //restore in/out defaults dup2(tmpin,0); dup2(tmpout,1); close(tmpin); close(tmpout); if (!background) { // Wait for last command waitpid(ret, NULL); }...
View Full Document

This note was uploaded on 01/30/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Fall '11 term at Purdue University-West Lafayette.

Page1 / 4

Execution Strategy for Your Shell - // Last simple command...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online