Exercise 1 - Attempted hijacking, with bare hands

Write 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 again

Using 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 grief

Repeat 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.