• Open a new file and include the pthread.h file. Create two new function prototypes called thread1.
• Open a new file and include the pthread.h file. Create two new function prototypes called thread1 and thread2 (i.e “void* thread1();”). These will be the methods that our pthreads will run. Look at the example code in Section 4, or in t1.c, and t2.c. • Code the main function. Make two instances of the data type pthread_t called “i1” and “i2”. • Create the threads using the pthread_create function. Create both threads, passing similar argu- ments as shown in the example in the help section. The last argument may be passed a value of NULL, since we don’t pass any arguments to our thread1 and thread2 functions. 1 • After the main function write the thread1 and thread2 functions. Just have each function print a message to the screen identifying itself (i.e. printf(“Hello,I am thread 1n”);). Add a print statement in the end of the main function (i.e. printf(“Hello,I am main processn”);). • Compile the program using the -lpthread option. $ gcc -o ex1 -lpthread ex1.c This option compiles the code linked with the pthread library. Run the program and observe the output. What happens? Well, we created the threads, but we forgot to use the pthread_join function to allow them to finish before main terminates. 1. To make sure the main terminates before the threads finish, add a sleep(5) statement in the beginning of the thread functions. Can you see the threads’ output? Why? 2. Add the two pthread_join statements just before the printf statement in main. Pass a value of NULL for the second argument. Recompile and rerun the program. What is the output? Why? 3. (2 pts ) Include your commented code with your submission. Label it “pthread example”. 3.2 Thread synchronization In this experiment you will learn how to synchronize threads that share a common data source. Mutexes and conditional variables are used as synchronization mechanisms for pthreads. 3.2.1 Mutex We will first look at code examples that illustrate how threads use mutexes to exclusively access critical area. Examine the contents of t1.c. 1. Compile and run t1.c, what is the output value of v? 2. Delete the pthread_mutex_lock and pthread_mutex_unlock statement in both increment and decrement threads. Recompile and rerun t1.c, what is the output value of v? Explain why the output is the same, or different. 3.2.2 Conditional Variable Next examine t2.c. This is a “Hello World” program using threads. The program uses threads to print ”hello world”. The thread that prints “world” waits for the other thread to finish printing “hello”. This is achieved using condition variables. • Modify the program by adding another thread (and routine) called “again” Use a second conditional variable to synchronize the three threads so that they print out the statement “Hello World Again!” • To implement this correctly, you must understand why the “done” flag is necessary. Think about the case where the hello function runs first and sends the signal before world is waiting (you can use a sleep statement to force this case). Note that a signal is not received unless someone is waiting for it first. Could the world thread sleep forever? When you make your changes, take this problem into account. 1.include your modified code with your lab submission and comment on what you added or changed. Label this “t2.c”. 2 3.3 Modified Producer Consumer Problem Next, you will work on a producer-consumer type problem that is different from what we discussed in class. So, please read the description carefully. Examine t3.c. The goal of this program is to run a group of consumers and a single producer in synchronization. The program will start one producer thread, which runs the function “producer”, and many consumer threads, each of which runs the function “consumer”. The producer should produce items only when the number of items in the supply has reached zero. Until this happens, the producer waits. The producer produces 10 items each time. When there are no more consumer threads remaining to consume items, the producer must exit. Each consumer thread waits until there is at least one item of supply remaining to consume. It then consumes one item of supply, and then exits. Your task is to fill in the code for the producer. The code for the consumer has already been filled in. 1.Include your modified code with your lab submission and comment on what you added or changed. Label this “t3.c May 13 2022 07:27 PM
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.Read more
Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.Read more
Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.Read more
Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.Read more
By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.Read more