Warning: include(remarks.php): failed to open stream: No such file or directory in /mnt/tanenbaum/sd3/thesards/masson/W3/v2/Teachings/PR-IT-4205/tp2/content.php on line 3
Warning: include(): Failed opening 'remarks.php' for inclusion (include_path='.:/usr/share/php5:/usr/share/php') in /mnt/tanenbaum/sd3/thesards/masson/W3/v2/Teachings/PR-IT-4205/tp2/content.php on line 3
Exercise 1 - Attempted hijacking, with bare handsWrite a program that redirects stdout to a file, then performs an "ls" to this file. We must therefore open the file with open(), then use dup2() so that subsequent accesses to the standard output cause accesses to the file. Then you can make a execlp() to start ls -l.
Exercise 2 - Attempted hijacking with a fork
- Write a program that runs the program specified on the command line, and according to its exit, displays "OK" or "ERROR". Reminders: to retrieve the return code, use waitpid(), and OK is a return code of zero. Example: Program [ "foo" = "bar" ] should display ERROR. Please consult the manpage of the command [, it is instructive (man bash).
- Modify the previous program so that it does not display anything other than OK or ERROR, that is to say that the program serves as a "condition" should not display anything. To prevent him from displaying, it is proposed to redirect its output to the file /dev/null. You should not make a simple exec but a fork + exec. You should also redirect the standard output only in the son, so the father can still display OK or ERROR at the end of the program.
Exercise 3 - He went through here, he go through this againUsing fork(), create two processes communicating through a pipe (itself created with the system call pipe()). The son will read from standard input and write the characters set into uppercase with toupper() in the pipe. The father read from the pipe and write to standard output.
Exercise 4 - The rationalist son commits suicide, the father dies of griefRepeat the previous exercise, but this time the father writes (byte by byte, for example) and the son reads. The son will simply repeat the standard output what he reads, but stopping after 10 characters (like the command "head -c10"). Then the son exits. We also hope that the father ended when the son ends. Several solutions are possible:
- Use SIGCHLD to inform the father of the son termination.
- Carefully close unused file descriptors (ie, after fork(), each process closes half of the pipe it does not use). The next write to the pipe in the parent will causes a SIGPIPE.