Topic: Threads Submission Time. By the end of Lab. 10% Penalty if you submit by
ID: 3915105 • Letter: T
Question
Topic: Threads
Submission Time. By the end of Lab. 10% Penalty if you submit by 11.59 p.m. No Submission after that.
Note: To run the thread code you would have to include libraries. It would be good to include all of the following libraries.
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
Finally to execute thread code in gcc you have to use -lpthread option. For example if your c file is thread.c then you can run it like this
gcc -o thread thread.c -lpthread
./thread
Question:
You must write a C program which will create two threads in your main function inside a while loop. So, your main function would be something likefollowing:
// global variable
pthread_t tid[2];
int main()
{
int i =0;
While (i<2)
{
/create thread here. Example given below ( not 100% correct ) //pthread_create(tid[i],NULL,&doSomething,(Fill last argument yourself)); // have to pass i in the last argument, but how ? google is your friend.
//increment i
} sleep(5); // waiting for the threads to finish.
return 0;
}
You should also declare your threads which you are passing in the thread creation method as global variables (as done above). So, you can access them in any function.
When you create the threads. You must call a function doSomething using that thread. You have to give the name of the function inside the thread create function call.
Also, you have to pass your counter i to the function of thread (last argument of pthread_create). Inside the function you have to do following things.
1. Check if the current thread is thread 1 or thread 2. Can do that by getting the id of current thread by calling appropriate function (google the function to get the id of a thread). Compare the returned id with the id of thread 1 and thread 2 in an if statement (We studied a function to compare id’s of two threads). (Remember you declared your threads as global variable so you can access them in this function)
2. If it’s the thread 1 then you will print a message saying (I am thread one and the value of i is …….. (have to actually show the value here)) you will access the value of i by the parameter received in the function. But the parameter of the function is void pointer. So, you would have to type cast it to integer pointer and then get the actual value by dereferencing the pointer.
3. If it’s thread 2 then you will print a message saying (I am thread two and the value of i is : (get the value here) )
4. Ideally, for thread one the value of i should be 0 and for thread two the value of i should be 1. Because in your thread create function call you pass i=0 for thread 1 and i=1 for thread 2. But if you run your code a couple of times. You would see this is not the case.
5. Why do you think this is happening? Give reasons.
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void *doSomething(void *arg)
{
pthread_t thread_id;
thread_id=pthread_self();
if(thread_id == tid[0])
{
printf("I am thread1 and value of i is : %d ",(int*)arg);
}
if(thread_id == tid[1])
{
printf("I am thread2 and value of i is : %d ",(int*)arg);
}
}
int main()
{
int i =0;
while (i<2)
{
//create thread here. Example given below ( not 100% correct )
//pthread_create(tid[i],NULL,&doSomething,(Fill last argument yourself)); // have to pass i in the last argument, but how ? google is your friend.
pthread_create(&tid[i],NULL,&doSomething,(void*)i);
++i;
} sleep(5); // waiting for the threads to finish.
i = 0;
while(i < 2)
{
pthread_join(tid[i],NULL);
++i;
}
return 0;
}
------------------------------------------------------
//output
I am thread2 and value of i is : 1
I am thread1 and value of i is : 0
-----------------------------------------------
5. Answer to Question
Threads run in any order .They depends on the thead scheduling of the operatin system. Programers do not have control of the order in which threads run .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.