Objective: You are going to create your first multithreaded program! We are going to play some good old- fashioned Ping Pong this time. This project may seem relatively simple, but it has some nuance to it that can definitely give you a hard time. The requirements are below!
Requirements:
You will create 2 functions, one called "ping" and one called pong.
You will utilize the following pthread functions:
pthread_create() - to create the new thread
pthread_join() - join to close the thread once it is completed
At the very end of your program you will print the total number of time ping() looped for. Hint: this should be done through pthread_join()
You must also answer this question - "If we remove pthread_join() from your program, what happens? Explain. you can do this in your readme.txt
Requirements for "ping":
ping will be your function that will be handled through the creation of thread.
You MUST pass an argument (in this case a pointer to an integer) through the creation of the thread to ping.
ping must accept a void pointer and return a void pointer. (if you don't know how to do this we will cover it in the up coming class for week 6! Also in future labs if needed)
ping will loop 13 times (0-12)
ping will sleep for 1 second each loop iteration (simply call sleep(1);)
once completed ping will return the total number of times it looped.
Requirements for "pong":
pong will be handled normally (do not create another thread to call this method).
In main() simply call pong();
pong should look something like this:
void pong() { int i = 0; while(i < 5) { sleep(2); printf("Pong\n"); i++; } }
pong should sleep for 2 seconds (as seen above) before printing, this is to help keep the threads somewhat synchronized.
pong should loop through 5 times (0-4 as seen above) simply printing (also as seen above).
HINT: You should be able to complete this project with just these 3 include statements. Don't forget to look back to the slide we discussed in class regarding pthreads!
SAMPLE EXECUTION: Your output should look like this (if it doesn't look like this on every execution that is ok, but must executions should look like below).
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.