DAT2| SW6
Aalborg University| Computer Science Dpt.
Home| Course
Exercises (Lecture 10)
› Foreword › Exercise 1 › Exercise 2 › Exercise 3 › Exercise 4 › Exercise 5

Foreword

The following exercises needs for you to access a Unix system. A Linux operating system would be better (because I didn't try these exercises on Solaris) but probably not mandatory.

1. fork(), wait() and exit()

  1. Write a program which display its PID first, then fork and the child must display its PID and the PID of its parent.
  2. Execute the command ls -R ~ with the functions execv(), execl(), execlp(), execle(), execv() and execvp().
  3. Write a program that spawn 10 childs which are racing against each others. They are all looping 10.000 times and then terminating. The father display the pid of the winner and the order of termination.
  4. Write a program which spawn two childs which are, again, spawning two childs each until you reach a depth of 4. Each process display its PID and the PID of its parent.
  5. The same than previously but you can give the depth of the tree as an input to the program.
  6. What will do the following code:
    int main()
    {
      while(1)
        while(fork())
          malloc(1);
    
      return 0;
    }                                                                        
    
  7. What is the difference between exit() and return?

2. Rights and Restrictions Handling

  1. Write a program that display its UID (real and effective), and then replace his effective UID by it's real UID. Then after you displayed the "id" of this process, you must try to recover the original UID. (if you have root access on your machine, try to see what are the differences between running this program as a normal user and then as root)
  2. Write a program that check that the user which is using it is a certain number and then execute a command that only you can do. Set the "s" bit of this program. Is it secure ?
  3. Try to use the chroot() function (see the manpage) to force any user using a wrapper around /bin/sh to be locked in /tmp.

3. Scheduler Handling

Re-write the racing program from Exercise 1.3 and give to each child a different priority. Make statistics about the results depending on the different level of priority.

4. Concurrent Shell Programming

Try to analyze this code and see what would happen without the wait (for help look here):
#!/bin/bash

ROOT_UID=0   # Only users with $UID 0 have root privileges.
E_NOTROOT=65
E_NOPARAMS=66

if [ "$UID" -ne "$ROOT_UID" ]
then
  echo "Must be root to run this script."
  # "Run along kid, it's past your bedtime."
  exit $E_NOTROOT
fi

if [ -z "$1" ]
then
   echo "Usage: `basename $0` find-string"
   exit $E_NOPARAMS
fi

echo "Updating 'locate' database..."
echo "This may take a while."
updatedb /usr &     # Must be run as root.

wait # What would happen if we remove this wait ?
locate $1
exit 0

5. Writting a Daemon

A DAEMON (Disk And Execution Monitor) is a program which lives in the background and perform continuously a task usually for the system. This can be inetd (network), crond (job scheduler), nfsd (Network File System), apached (Web server), and so on (here, "d" stands for "daemon"). The goal of this question is to program a dummy daemon (dummyd -f <file> -t <delay> -h) which will display every <delay> seconds the content of the file <file> or display the help when the option -h is given in the command line.

Exercises:

  1. Read and understand the "Linux Daemon Writing HOWTO";
  2. Code the skeleton of the daemon with recognition of the different options;
  3. Make you dummyd daemon fully functional