CS 3733 Operating Systems, Fall 2008 Assignment 5


Due Friday, November 21

This assignment is a continuation of Assignment 4. In this assignment will will eliminate the use of pipes for communication by using threads. We will still need to create child processes to exec the C compiler, but the synchronization will be done differently. Put each part of this assignment in a new directory. Keep everything from Assignment 4 in a separate place.

Forking and Threads

What happens when a process having mutliple threads executes fork? For POSIX threads this is specified. The new process has a single thread which starts executing after the call to fork. Therefore, the child can call exec as in Assignment 4 and this does not affect the other threads of the original process. Any of the threads can wait for the child process. If you want to make sure that the right thread waits for the child, use waitpid with the process ID of the child as its parameter.

Part 1
Write a new method, threadcompile, to replace childcompile in makelib. It will have one void pointer parameter and return a void pointer. The parameter will be interpreted as a filename. The function will return a NULL pointer. It will get the path of the compile from a #define in makelib. The actual compiling should be done with a call to compile.

The makelib.c file will have a static global integer variable, count, which will count the number of completed compiles. The threadcompile function will run as a thread and increment the count variable if the compile was successful.

The makelib.c file will have methods called clearcount to clear the count to 0 and getcount to return the value of the count.

The main program, makethread, will take command line parameters like makechild from Assignment 4 and create threads to run threadcompile. It will then join these threads and create an executable if all compiles were successful. The main program will report the results as before.

Use synchronization as appropriate.
Answer the questions:

  1. Where was synchronization needed and how did you implement it?
  2. If you call makechildparallel from Assignment 4 with three command line parameters, how many different processes call compile. (If you cannot figure this out, have compile print a message giving the PID of the calling process.
  3. Answer the above question for makethread.
  4. Did you have to modify compile for this part? If so, in what way and why?

Part 2
In a new directory, make a new function, threadcompilereturn which is like threadcompile, but instead of incrementing count it returns a pointer to integer 0 on success and to 1 on failure. Write a new main function, makethreadreturn, that uses this. The main function keeps track of the count using the return value of each thread.

Use synchronization as appropriate.
Answer the question: Where was synchronization needed and how did you implement it?

Extra Credit:
There are two ways to obtain extra credit on this assignment. The extra credit can help you if you did not do well on a previous assignment or on the first midterm exam. Each of the extra credit parts is independent. You can do any one, or both of them. For each of the extra credit parts that you do, you must write an explanation of how you implemented and tested your program. Describe in words the algorithm that you used in the implementation.

Part 3: Optional, For Extra Credit
You can do this part if you did not get credit for Part 5 of assignment 4. Modify compile so that it only performs the compile if necessary. The compile is necessary if the object file does not exist, or it exists, but it is older than the source file. For some additional extra credit you can create the executable only when necessary. Explain what when necessary means in this case.

Part 4: Optional, For Extra Credit
Modify the program so that the main program reports success or failure of each compile as they occur. In this case you cannot use join directly, since join cannot choose the order in which threads are joined. You must figure out a mechanism for implementing this.

Explain how your solution works. Can it tell which compile has finish when it reports success or failure?

Note
In this assignment and in Assignment 4, the individual compiles are done concurrently and can generate output which can intermingle with each other and with the main program. Do not worry about this. This program is handled in Part 7 of Assignment 4.

Handing in your assignment
Test your programs as in Assignment 4. Use this cover sheet.