Homework: processes and threads

This lecture covers scheduling policies of threads and processes. Recall that a process is an abstraction which includes a program counter, registers, and an address space which includes the code, static data, stack and the heap. Threads are abstractions similar to processes except that multiple threads can share the same address space. Each thread has it's own program counter, registers and stack. Threads can share code, static data, and heap. Threads take significantly less space than processes because they don't require separate address spaces.

Hand-In Procedure

You are to turn in this homework during lecture. Please write up your answers to the exercises below and hand them in to a staff member at the beginning of lecture. Mention your CSE login ID at the top of your homework submission.

Assignment Part 1 (Processes)

On a linux machine, type the following command
$ cat | tee output.file

cat is a UNIX utility that prints the contents of STDIN to STDOUT. tee is a UNIX utility that prints the contents of STDIN to both STDOUT and to the file named by its argument (output.file). After you type this command, you can type in some characters followed by the newline character.

Turn in: While this command is running, examine the processes created:

  1. Use pstree to see the process hierarchy. Tell us what you find about the process hierarchy.
  2. Use 'ps x' to identify the process IDs of the processes created by cat and tee commands. Linux provides a proc pseudo-filesystem which can be used to examine the state of a process using filesystem namespace.

Assignment Part 2 (Threads)

Threads can be implemented completely at the user level. i.e., we do not require privileged operations to implement a thread abstraction and schedule different threads. In other words, a process can provide multiple threads by implementing a scheduler. Let's see how this can be done.

To implement threads, the process needs to provide the abstraction of multiple control-flow (program counter), multiple register sets and multiple stacks. This can be done if after every periodic time interval, one thread can be interrupted and saved and another thread can be loaded. Saving a thread involves saving it's program counter, registers and stack pointer. Similarly, loading a thread involves loading the new thread's program counter, registers and stack pointer. Neither the save operation, nor the load operation requires any privileged operation -- we are just loading and saving registers.

So the only remaining issue is how to periodically interrupt a running thread from within a process. For an OS, this interruption is done by the hardware timer device. A process can do this using the SIGALRM signal.

Such threads implemented inside a process are called user-level threads. The OS cannot distinguish between multiple user-level threads and it can only see one process that is running which includes the thread scheduler and the different threads.

Turn in:
Read the manpage of the signal, alarm, and setitimer. Understand how SIGALRM can help in implementing user-level threads. Briefly describe how you will do this (2-3 sentences and some pseudo-code).

Assignment Part 3 (Scheduling)

Carefully read Pintos Threads Assignment completely. Install pintos and run it using both bochs and qemu. Tools page has detailed instruction on setting up and running pintos. Look through pintos source code and design a solution. Ask your doubts and questions during the lecture.

Setup a subversion repository on CSC servers. Follow the instructions on the tools page. Ensure that nobody else can access the files in your directory by setting the file and directory permissions (using chmod). See the CSC subversion page for details on how to do this. Maintaining the security of your project files is your responsibility. If your project files are visible to others (through any backdoor), we will penalize you.

Logon to palasi.cse.iitd.ernet.in and type "submit-pintos base". You will be prompted for the URL of your repository. The script will tell you if your repository is correctly formed. If the script does not succeed ("Submission NOT made."), fix the reported errors and submit again. Your repository is expected to be identical to the pintos source code provided by us here

NOTE: 'submit-pintos' is now functional.

Turn in:
After you have submitted using the submit-pintos script, write the URL of your repository in your written submission.