Most Unix systems include a utility program called tee that copies standard inpu
ID: 3678425 • Letter: M
Question
Most Unix systems include a utility program called tee that copies standard input to standard output and a file descriptor passed on the command line. For instance, $ cat -n example.txt | tee example~.txt would display example.txt to standard output with line numbers and example~txt would be a copy of example.txt with line numbers. Use unnamed pipes to create your own version of tee called my tee, equivalent to the above cat command At the command line you will type the following to be equivalent to the above cat command: $ my tee example.txt example~.txt More details: Check that you have 3 command line arguments. Argv{1} will be the file used with cat-n. argv[2] will be the file you write to create a pipe fork the process child will: Call dup2 so that any writes to standard output will go instead to the write end of the pipe. Then, execute the cat-n command using excel Parent will: open argv[2] for writing read from the pipe until there are no characters left. Write to std out write to the file descriptor associated with argv[2] Note: do not use exec for the parent don't forget to handle the error situation of a forkExplanation / Answer
Below I have written a sample code for your reference ,
This program segments are to demonstrate the child parent relationship of the processes on the pipeline. The
processes on the pipiline are brothers. This is different from System V. In System V, the left process is the child
of the right process.
$ cat mytee.c
int main(int argc, char *argv[])
{ int i;
char argbuf[4096] = "tee ";
printf("Ppid: %i, mytee ID: %i ", getppid(), getpid());
for (i = 1; i < argc ; i++)
{ strcat(argbuf, argv[i]);
strcat(argbuf, " "); }
strcat(argbuf, ";"); system(argbuf);
}
# Invoke gcc compiler to produce executables.
$ gcc -o myecho myecho.c
$ gcc -o mytee mytee.c
$ myecho a b c d | mytee
Ppid: 938, mytee ID: 1592
Ppid: 938, myecho ID: 1591
abcd
# The parent of mytee and myecho is the current bash shell
# with process ID 938.
$ echo $$
938
The output of cat is the input of wc. cat writes its output to stdout. Without file argument, wc (word count) reads its input from stdin.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.