Lab 3

POSIX Pthreads

Goals

  • Practice working with multi-threaded C programs. This lab will give you a good introduction on how to work with the Pthread library. You will be focusing on applying some of the most important concepts discussed in lecture. (Later labs will delve deeper into the topic.)
  • Practice working with C function pointers. The C language has a very interesting feature that allows programs to use functions as parameters to other functions. The concept and the syntax for function pointers can be a little intimidating, but this is something you will have to master in order to use 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

Copy the following C source code to your local working directory for this lab:

~cs315/Labs/Lab3/summation.c

This program requires two command line arguments to execute:

  1. numthreads – an integer value that specifies how many threads will be created, and
  2. increment – the number of integer values that each thread will work with.

The task performed by this code is simple: It creates the specified number of threads and assigns to each one of them an interval of integer numbers, say [a,b). Each thread computes a value s, which is the summation of the squares of the integers in its interval, that is:

a^2 + (a+1)^2 + (a+2)^2 + … + (b-2)^2 + (b-1)^2

Essentially, this program “parallelizes” the computation of the summation of the squares of integers in the interval [0, numthreads*increment) balancing the load equally among the number of threads specified. We say “parallelizes” in quotes because in a single-core machine, the threads would execute not in parallel, but rather concurrently: splitting the time of one single CPU. If this code runs on a multi-core machine the threads may really run in parallel (this depends on the set up of the library and the OS).

IMPORTANT: The code given to you in the file summation.c is likely to compile with warnings. It is your task to ensure to you eliminate these warnings in the files you will generate using this code as starting point!

Problem 1 [15 points]

In order to compile and link this program, you cannot simply do

$ gcc -std=gnu99 -Wall summation.c -o summation

  1. Try to compile with the command above to see what happens. Report the undefined symbols that the list of undefined references you see in a file called answers.txt.

It turns out that this program uses functions that are defined in libraries that are not being linked with your code by default. The two libraries in question, for this program, are the math library (which, among many other functions, defines pow(3)) and the pthread library (which defines all the data types and functions for POSIX threads).

Read the manual page of pow(3) and find any text that says “link with…” Pass the flag you find to gcc, when compiling the code. (By the way, this is a linker flag!) This links the math library with your program.

  1. In answers.txt, write the linker flag that you discovered you need to use.

In order to put this flag to use, you should simply append that little bit of text to the invocation of gcc above.

  1. Try it out and see what you get. If you see any undefined references, list them in answers.txt.

Now, append to the last invocation of gcc you used the flag -lpthread – the -l is telling gcc this is a linker flag, the pthread names the library to link. If all went as expected, you should now have an executable to run.

Create a rule in your Makefile to compile summation.c into an executable. This rule should be activated when one calls make without any command line arguments.

When you are done with this, you need to:

  • cd ~/csci315/Labs/Lab3
  • git add Makefile
  • git add answers.txt
  • git commit -m “Lab 3.1 completed”
  • git push

Problem 2 [20 points]

Run your summation executable few times, passing different values of number of threads and increment. Start with two threads and a somewhat large increment. Then keeping the increment value fixed, increase the number of threads to see how things might change.

Read the source summation.c carefully and do your best to understand how it works. Now, respond to the questions below in your answers.txt file, writing as concisely and clearly as possible.

  1. Explain the mechanism by which the main thread passes arguments to the other threads it creates.
  2. Lines 89, 90, and 91 make calls to function calloc(3). Explain what this function does and why it is preferable to malloc(3) in this type of circumstance. (Hint: Consider the data type that is being dynamically allocated.)
  3. Explain what is means for a thread to be in detached state (line 97 in source code). Discuss whether or not this program should create detached threads.
  4. Explain the mechanisms by which the main thread receives the results from each computation thread and by which it synchronizes with them.
  5. What would happen to the execution of the program if one of the child threads should decide to call exit(3) halfway through its computation? (Modify your source code to include this change and experiment with it. When you are done, leave this line commented out.)
  6. What would happen if one of the child threads should decide to call execl(3) to run “/bin/ls” in the executable’s current working directory? (Again, modify your source code to include this change and experiment with it. When you are done, leave this line commented out.)

When you are done with this, you need to:

  • cd ~/csci315/Labs/Lab3
  • git add answers.txt
  • git commit -m “Lab 3.2 completed”
  • git push

Problem 3 [15 points]

Copy you summation.c to a new file summation3.c, which will compute the time it takes to execute to completion. To accomplish this, you should instrument the program to call gettimeofday(2) right when starts and then again when it ends.

With a little bit of arithmetic, using the two struct timeval values returned, you can compute the time it took the program to run. Be careful to do the right arithmetic when you subtract two time values. Have this time printed to the standard output exactly before the program terminates.

Update your Makefile so it will compile summation3.c properly.

When you are done with this, you need to:

  • cd ~/csci315/Labs/Lab3
  • git add summation3.c
  • git add Makefile
  • git commit -m “Lab 3.3 completed”
  • git push

Problem 4 [20 points]

Copy your summation3.c to a new file summation4.c. In this new version, each thread will compute the time that it takes to execute individually and print the result to the standard output. Furthermore, have each thread print to the standard output the current time before it returns to the main thread. Note that you will have to use a combination of gettimeofday(2) and ctime(3) to produce a human readable date.

Update your Makefile so it will compile summation4.c properly.

Additionally, write in answers.txt the responses to the following questions.

  1. Can you guarantee that the date printed by each thread is always the termination time of that very thread?
  2. If there is any problem, explain what causes it and how you can avoid it. Also, remember to modify your source code and submit a version that sure prints out the termination time for each thread.
When you are done with this, you need to:
  • cd ~/csci315/Labs/Lab3
  • git add summation4.c
  • git add Makefile
  • git add answers.txt
  • git commit -m “Lab 3.4 completed”
  • git push

 

Hand In

Before turning in your work for grading, create a text file in your Lab 3 directory called submission.txt. In this file, provide a list to indicate to the grader, problem by problem, if you completed the problem and whether it works to specification. Wrap everything up by turning in this file:

  • git add ~/csci315/Labs/Lab3/submission.txt
  • git commit -m “Lab 3 completed”
  • git push

Rubric

  1. [up to -10 points] An incorrect or incomplete Makefile to build all programs in lab and pre-lab.
  2. [5 points] Problem 1.1 – In answers.txt: undefined symbols in summation.c
  3. [5 points] Problem 1.2 – In answers.txt: linker flag to use the math library
  4. [5 points] Problem 1.3 – In answers.txt: any remaining undefined symbols?
  5. [3 points: Problem 2.1 – In answers.txt: passing arguments to threads
  6. [3 points]: Problem 2.2 – In answers.txt: how calloc may be better than malloc
  7. [3 points]: Problem 2.3 – In answers.txt: thread detached state
  8. [3 points]: Problem 2.4 – In answers.txt: thread synchronization and returning results
  9. [4 points]: Problem 2.5 – In answers.txt: what happens when one thread calls exit
  10. [4 points]: Problem 2.6 – In answers.txt: what happens when one thread calls execl
  11. [10 points]: Problem 3.1 – Returns time for execution of summation (gettimeofday)
  12. [5 points]: Problem 3.2 – Correct subtraction of time values
  13. [10 points]: Problem 4.1 – Each thread computes its run time and prints end time with ctime
  14. [5 points]: Problem 4.2 – In answers.txt: is the end time printed reliable?
  15. [5 points]: Problem 4.3 – In answers.txt: what causes problems with ctime and how to avoid it

1 thought on “Lab 3

  1. Userbola Agent Sportsbook Trusted
    The football gambling game is very popular in Indonesia.

    Besides having the ability to play sports betting, you can also play On line casino, and slots.

    Userbola as an official and trustworthy Indonesian gambling website and bookmaker provides fast and free list services.

    We all provide the best service so that you can be able to play gambling easily without having to
    download and install apps, for sbobet the golf ball can be accessed via sbobet mobile or wap, while sbobet casino is equipped with HTML5 features.

    And that we also provide customer support services, if you have trouble logging in or don’t have the latest
    sbobet link or alternative, you can e mail us via live chat, WhatsApp, We Chat, and Collection.

    Game Userbola Online Gambling
    The userbola site also offers ibcbet / maxbet ball gambling games, joker slots gaming gambling,
    live casino, s128 and sv338 cockfighting, fielding balls (tangkasnet, tangkas88, tangkas1).

    For those of you who choose various online gambling games, the userbola site is he best option for you for you Indonesian online gambling
    players. The maxbet program can play ball betting, live casino allbet, keno,
    and number games. Also there are slots and seafood capturing games that are very famous from joker
    gambling. If you love bets fighting, you can gamble and watch football through
    the SV128 or SV338 providers.

    Userbola Internet gambling Site

    Userbola is an online betting site that has an official permit from PAGCOR, so
    you shouldn’t hesitate to produce a finance
    transaction and play on our site, and your data will be safe.

    Userbola as a reliable betting site, we pay your earn no matter how fast you
    play gambling in only 3 minutes for the process of withdrawing cash, while for the process
    of depositing money for approximately one minute.
    Userbola provides purchase services from 9 banks namely Bank
    BCA, BNI, BRI, Mandiri, CIMB Niaga, Danamon, Panin, OCBC NISP.
    There are also alternative transaction services such as OVO, Dana.

    Register now and get the satisfaction and
    convenience of playing gambling on the Userbola site.

    THE WAY TO DEPOSIT ONLINE BETS
    You must first register to become a member of a football gambling agent.
    You then will be given a customer ID to log in to enter
    the online betting game. Make a deposit of funds immediately to the bank
    account amount of the Trusted Football Agent if you need to
    play immediately. The steps to make a deposit are as comes after:

    Submit Deposit Form
    Request via our SMS CENTRE at + 62-821-6182-5555
    Request via Whatsapp + 62-821-6182-5555
    Request via Live Chat Userbola immediately
    Please use the steps above to avoid irresponsible people
    on behalf of Userbola. End up being careful to avoid things we don’t want collectively.
    Congratulations on joining the Indonesian Trusted Football Wagering Agent community.
    Userbola is for those of you who are serious about actively playing with us.
    Have a good bet.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.