Sistemas Operativos 3MIEEC

Problem Set #3: Processes and their API in Linux/Unix


Questions

  1. About processes in Unix/Linux.
    1. What is a process?
    2. How many processes are executed when you give the command man pstree?
    3. Using the pstree command find out which processes are executing on your behalf. Hint: read pstree's man page.
    4. Why do processes execute on behalf of a user?
  2. About the API for creating/synchronizing Unix/Linux processes
    1. What is the fork system call for? How do you distinguish between the parent and the child process?
    2. What is the execve system call for?
    3. How many different values can a process receive via the wait()/waitpid() system call when one of its child terminates?

Problems

  1. Write a program that prints its arguments on the standard output as well as its environment variables.

    FYI:

  2. Write a program that:

    1. Writes its environment variables and their values in a file, whose name is its first argument.
    2. Creates a child process, which writes its environment variables and their files to another file, whose name is a command line argument of both the parent and the child processes.
      Compare the files created by both processes. What is the relationship between the environment variables of a process and of its children?

  3. Write a program that creates a child and a grand-child. Each process shall write its PID and that of its parent, as well as the termination code of its child, if it has any. The child and the grand-child shall terminate with code 2 and 3, respectively.

    Hint: Develop this program gradually. E.g.:

    1. Write a version in which the only process prints its PID and that of its parent.
    2. Write a new version in which the first process creates a child process, prints the PID of its child and terminates. The child process prints only its PID and that of its parent.
    3. Write a new version in which: (i) the parent waits for the termination of its child and prints its exit code; (ii) The child process terminates with exit code of 2.

  4. Write a program that creates a process that compiles a C program whose name is passed on its command line. IMP.: Your program should execute gcc (/usr/bin/gcc), by invoking the execve() system call with appropriate arguments. The parent should wait for the termination of its child.

  5. Write a new version of the previous program, such that rather than creating a child process to execute the compiler, your program executes directly gcc.