6CS005 Week 3 Workshop
6CS005 Week 3 Workshop
Student Number:
Name:
You may need to refer to the Week 3 lecture sides in order to complete these tasks.
1. The following program demonstrates 3 thread sending string messages to each other, using a global
array. The messages are sent meant to be sent in the following order:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
/* Receiving a message */
printf("Thread %ld received the message '%s'\n",tid, messages[tid]);
free(messages[tid]);
messages[tid] = NULL;
}
return NULL;
}
void main()
{
pthread_t thrID1, thrID2, thrID3;
2. Using the technique of “busy-waiting” to correct the program, and establishing the correct order of
messages.
3. Use pthread “mutex” to correct the program in (1). You will need multiple mutexes.