Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I/O redirection in a C shell. How do implement I/O redirection in a fork-exec()

ID: 3607400 • Letter: I

Question

I/O redirection in a C shell.

How do implement I/O redirection in a fork-exec() type system call?(any info on I/O redirection helps)

}//end if

here is more info on what im doing if this helps

The line is typed in the command line

programname argl arg2 outputfile will execute the program programname with arguments argl and arg2 , the stdin FILE stream replaced by inputfile and the stdout FILE stream replaced by outputfile . For more cases on how redirection I/O redirection should be handled, please visit the textbook website: http://www.tldp.org/LDP/intro- linux/html/sect_05_01 html With output redirection, if the redirection character is > then the outputfile is created if it does not exist and truncated if it does. If the redirection token is >> then outputfile is created if it does not exist and appended to if it does. Note: you can assume that the redirection symbols, & >> will be delimited from other command line arguments by white space - one or more spaces and/or tabs. This condition and the meanings for the redirection symbols outlined above and in the project differ from that for the standard shell IVO redirection is accomplished in the child process immediately after the fork and before the exec command. At this point, the child has inherited all the filehandles of its parent and still has access to a copy of the parent memory. Thus, it will know if redirection is to be performed, and, if it does change the stdin and/or stdout file streams, this will only effect the child not the parent. You can use open to create file descriptors for inputfile and/or outputfile and then use dup or dup2 to replace either the stdin descriptor ( STDIN_FILENO from unistd.h ) or the stdout descriptor STDOUT_FILENO from unistd.h freopen . This function is one of the three functions you can However, the easiest way to do this is to use use to open a standard IVO stream.

Explanation / Answer

In case 0:
child process has all the information of the command and hence command information will be present here. dup command can be used here for re-direction.

dup() system call is the right one to use here as it is used for input/output redirection or piping the output on unix shell.
Example:
Suppose, in your case you want to redirect program to the output file, redirect the output of prog command to a file, we use the following command in the shell

./program > outputfile

File descriptor 1 is bound to the standard output stream. The program output command is supposed to output the data on this output stream i.e. 1. But, using '>' operator we are able to redirect this output to file 'outputfile'.

What happens when the process that is executing the shell here is that it parses the command and when it finds '>' operator, it will first find the file descriptor of the right hand side operand - 'outputfile' OR create the new fd if file doesnt exist already. Once, it finds this fd, it will close the stdout file descriptor and call a dup() on the given fd for this 'outputfile'.
Thats it, from this step onwards, the output will be redirected to the file 'outputfile'. it is good to close the file descriptor to preserve the number of descriptors.

/*I/O redirection*/
{
fd = open ("output file", O_RDWR|O_CREAT); //output file path here. If not created will create it
close(stdout); //we are closing the stdout. Since we are doing this under child, it will not impact parent
dup(fd);
close(fd);
/* stdout is now redirected to the outputfile */
}
execl as used gives you options to add arguments to your executable

The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument. WUNTRACED option is used to return if a child has stopped.