COSC 3346, Operating Systems

Programming with pThreads

Date Assigned: Monday, June 19, 2017
Due Date: Wednesday, June 28, 2017 by 11:59 p.m.


The purpose of this assignment is to become familiar with the pThreads API and write simple programs using pThreads.  The first program is already done for you.  You will have to modify it to obtain timing results and explore the use of data parallelism techniques for computing PI.  The second part of this programming assignment uses threads in a more functional parallelism paradigm for generating and displaying the Fibonacci sequence.  The third part of this programming project you will explore the use of thread synchronization without using semaphores.   For this programming project, you must use the departmental server


PART A: Computing PI With Threads

The first part of this assignment is to explore the use of threads in an application that computes PI.  Download the compute_pi_montecarlo.c program to your Project2 subdirectory.  Create an appropriate Makefile to support the creation of the PI application (you can do this by modifying the Project1 Makefile).  Compile and run this program a few times to understand the basic operation and to verify that it does approximate PI.  Next, time your program and/or add appropriate timing system calls to your code.  Run the application varying the number of threads and number of sample points.  You may get a core dump when you exceed the number of threads permitted (note that value).  Record the time(s) it takes your program to execute when changing the number of threads and number of sample points.

Create a plot of time vs. number of threads (or possibly time vs. log of the number of threads) when the number of sample points is fixed.  Create a plot of time vs. number of sample points (or possibly time vs. log of the number of sample points) when the number of threads is fixed.  Use an Excel like application to create your plot.

The name of this program must be osproj2a.c or osproj2a.cpp.


PART B: Computing Fibonacci Numbers With Threads

This programming project is based on problem 4.26 on page 197 in our textbook (OSC).  The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,....  Formally, it can be expressed as:

fib0 = 0
fib1 = 1
fibn = fibn-1 + fibn-2

Write a multithreaded program that generates the Fibonacci series using the pThreads library.  This program should work as follows:  The user will enter on the command line then number of Fibonacci numbers that the program will generate.  The program will then create a separate thread that will generate the Fibonacci numbers placing the sequence in data that is shared by the threads (an array or vector is probably the most convenient data structure).  When the thread finishes execution, the parent thread will output the sequence generated by the child thread.  Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finishes, this will require having the parent thread wait for the child thread to finish, using the techniques described in Section 4.4 of our textbook.

The name of this program must be osproj2b.c or osproj2b.cpp


PART C: Printing Words With sched_yield()

Write a program that accepts a phrase of unspecified length on the command line. For example:
prompt% vowcon Operating Systems Class at TAMUCC
The main() in this program has to create two threads running functions (vow and cons). The threads should take turns printing the respective words of the phrase supplied on the command line. The vow thread should print all the words that start with a vowel and the cons thread should print all words starting with a consonant. Note that the main thread (running main()) should not print anything itself, the output should be supplied by the threads that it creates. The order of words in the phrase should not be changed in the printout. Your program should work for any phrase of any reasonable length, not just the one given in the example. The output of your program should look similar to the following
$ ./osproj2c Operating Systems Class at TAMUCC
vow: Operating
cons: Systems
cons: Class
vow: at
cons: TAMUCC
In this part you are *not allowed* to use synchronization primitives such as mutexes for thread coordination. You can use sched_yield() to relinquish control of the CPU. You will have to investigate some of the other pthread functions available to you for this project. Look for examples online.

The name of this program must be osproj2c.c or osproj2c.cpp.



There are a few systems calls to get and record timing values in user programs available in C/C++.  The 'time' system command can also be used as a system call.  Other timing operations such as ctime(), gettimeofdday(), ftime(), and time() are system functions and can only be used in a program.  CAREFULLY read the man pages for these commands and system calls.



This is an individual assignment.  Write a well structured, well documented, computer program using C/C++ and the supporting files provided to you.  Your project must follow the documentation standards defined here.  Use procedures and functions.  You may use the STL in your solution.  This project is worth 100 points.



Please see the project submission guidelines for details on how your project should be formatted for submission.  Include an electronic version of  your report in your project subdirectory.