In: Computer Science
Generating a sequence using Pthreads
Having multiple threads call a function, like will have a non-deterministic execution. Write a program with 3 threads that call a function called do_work. Each thread will be responsible for generating a number and appending it to a buffer. Thread 1 generates number 1, thread 2 generates number 2, and thread 3 generates number 3. These numbers assigned to the threads are passed in as arguments. Each thread will store its value in a shared buffer of integers having a size of 3 elements called “buffer”. When the third element is added to the buffer by either thread 1, 2 or 3, then it checks to see if the sequence is “123”. If not, it clears the buffer and the threads try to generate the sequence again. Once the total number of sequences of “123” reach 10, the threads print out the total number of tries it took to print “123”. For example, keep track of the total number of other sequences generated (includi it should print out it’s corresponding number. Below is example output at the end of the program’s execution. Ensure that your program produces the exact same output formatting; you will see why in a moment. ...
My id: 2
My id: 1
Total ses geneted: 38
Number of cect sequences: 10
Testing:
You will test that your program appears to work correctly. I provide you with a python script that tests the output of your program: A3sequence_test.py. After you believe your pr document it as well, with screenshots and a text description. For instance “my program never stops running and it should stop because the counter value is 10 and therefore...”.
Please find the following program to check the buffer content updated by 3 different threads.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
//buffer to hold 1,2 and 3
char buffer[3]={0,0,0};
//index to fill the buffer with 1,2, and 3
int gindex=0;
//thread function
void *do_work(void *intger)
{
char *num=(char *)intger;
//update the buffer
buffer[gindex]=*num;
//reset the buffer index
if(gindex++ == 3)
gindex=0;
return NULL;
}
int main()
{
pthread_t thread_id[3];
char a[]={'1','2','3'};
int i=0;
int totalCount = 0;
int totalThreadSpawnCount =0;
while(totalCount <= 10)
{
//spawn 3 different threads with argument passed here
for(i=0; i<=2; i++)
pthread_create(&thread_id[i], NULL, do_work, (void
*)&a[i]);
//wait for three threads to join the main program
pthread_join(thread_id[0], NULL);
pthread_join(thread_id[1], NULL);
pthread_join(thread_id[2], NULL);
//check whether the buffer content is "123" and take the
count
if(!strncmp(buffer, "123", 3))
{
totalCount++;
}
//increment the total try count
totalThreadSpawnCount++;
//exit the program once 10 iterations is completed.
if(totalCount==10)
{
break;
}
memset(buffer, '\0', sizeof(buffer));
}
printf("Total sequences generated: %d.\n",
totalThreadSpawnCount);
printf("Number of correct sequences: %d.\n", totalCount);
exit(0);
}
Output:
USER>./a.out
Total sequences generated: 49.
Number of correct sequences: 10.
USER>
USER>./a.out
Total sequences generated: 47.
Number of correct sequences: 10.
USER>./a.out
Total sequences generated: 35.
Number of correct sequences: 10.
USER>
Screen Shot: