Pre-lab 3

POSIX Pthreads

Credits

The material developed for this lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this “credits” note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e- mail to: perrone[at]bucknell[dot]edu

Set Up

Create a directory for Lab 3 under your csci315/ directory. You will will write a few simple programs for this pre-lab, all of which should be committed to your git repository. Remember, that you should be committing files to your local repo incrementally, that is, as you develop each program. This will help you recover files you might accidentally corrupt. Students have run into situations in which small modifications made to code that “nearly” works end up introducing some hard to track down error. Think of committing intermediate work that you feel brings you one step closer to the final solution. And, it goes without saying, that you must always commit and push the final solution, as well.

Read sections 1 through 5 in the POSIX Threads Programming tutorial, by Blaise Barney, from Lawrence Livermore National Laboratory. Read the source code examples you find and make a strong effort to understand how they work. Pay particular attention to the topics of thread creation and thread termination, how one can pass parameters to a thread, and how one can synchronize the termination of threads (join).

Pre-Lab Problem 1 [20 points]

Write a program called char-threads.c, which creates three threads that never terminate. The general shape of all three threads is similar, what is particular to each one is the character they print. Here is the general structure:

while (1) {

  • Print one character; the character to print depends on which thread it is:
    • Thread 1 prints a digit followed by a newline character. In each iteration of the loop, the thread should print a digit in [0:9], starting with 0, then 1, 2, 3,  …, and 9, repeating the sequence all over again.
    • Thread 2 prints a letters followed by a newline character. The thread should cycle through the lowercase letters of the alphabet a to z, and cycle back to ‘a‘, repeating the sequence all over again.
    • Thread 3 prints always the same character ‘#‘ followed by a newline character.
  • After printing the one character, the thread computes the sum of all integers from 1 to 1,000,000. (You might need to adjust this number to make the results visible.) Do nothing with the result; we just want to the thread to spend some time doing CPU-bound work.

}

In your pre-lab.txt file jot down your observations regarding the following:

  1. Do the threads order of execution match the order in which they were created?
  2. Looking at the characters that are printed to the standard output, do the threads always follow the same order of execution? That is, we want to know whether if you always see thread 1, then thread 2, then thread 3, etc., repeating in a predictable order.

When you are done with this, you need to:

  • cd ~/csci315/Labs/Lab3
  • git add pre-lab.txt
  • git add char-threads.c
  • git commit -m “Pre-lab 3.1 completed”
  • git push

Pre-Lab Problem 2 [10 points]

Write a program called mytime.c, which calls gettimeofday(2), passes the results to ctime(3), and then prints to the screen the resulting string. Whether you have used these functions before or not, take the time to read their man pages to understand what they do and how to use them.

When you are done with this, you need to:

  • cd ~/csci315/Labs/Lab3
  • git add mytime.c
  • git commit -m “Pre-lab 3.2 completed”
  • git push

Note: In order to earn full credit in this pre-lab assignment, you must have pushed your solutions to your remote git repo before the start of you actual Lab 3 session.

Rubric

  • Problem 1: [20 points] the correct implementation of the char-threads.c program; [10 points] pre-lab.txt with text describing your observations for (1) and (2) 
  • Problem 2: [10 points] the correct implementation of the mytime.c program.
Print Friendly