Consider the following program: #include <pthread.h> #include <stdio.h> #include
ID: 3846991 • Letter: C
Question
Consider the following program:
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#define MAX_RESOURCES 100
int available_resources = MAX_RESOURCES;
int decrease_count(int count) {
if (available_resources < count) {
return -1;
}
else {
available_resources -= count;
return 0;
}
}
int increase_count(int count) {
available_resources += count;
return 0;
}
void *runner(void *param); /* the thread */
int main(int argc, char *argv[]) {
pthread_t tid1, tid2, tid3, tid4; /* the thread identifiers*/
pthread_attr_t attr; /* set of attributes for the thread */
/* get the default attributes */
pthread_attr_init(&attr);
/* create the threads */
pthread_create(&tid1,&attr,runner,NULL);
pthread_create(&tid2,&attr,runner,NULL);
pthread_create(&tid3,&attr,runner,NULL);
pthread_create(&tid4,&attr,runner,NULL);
/* now wait for the thread to exit */
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
printf("available_resources: %d ", available_resources);
}
/**
* The thread will begin control in this function
*/
void *runner(void *param) {
int i;
for (i=1; i<= 1000000; i++) {
if (decrease_count(3) == 0) {
increase_count(3);
}
}
pthread_exit(0);
}
Study the code and notice that logically after running the program, the available_resources variable should have the initial value of 100.
Translate and run the program several times. You will notice that the result that prints the value of the available_resources variable can be changed. This is considered a misconduct and is a consequence of the lack of some synchronization method.
Create two new versions of the program by solving the problem by using two different techniques:
1. Using binary semaphores.
2. Using mutex locks.
Run the new version several times to check if the problem is resolved.
Explanation / Answer
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#define MAX_RESOURCES 100
int available_resources = MAX_RESOURCES;
int decrease_count(int count) {
if (available_resources < count) {
return -1;
}
else {
available_resources -= count;
return 0;
}
}
int increase_count(int count) {
available_resources += count;
return 0;
}
void *runner(void *param); /* the thread */
int main(int argc, char *argv[]) {
pthread_t tid1, tid2, tid3, tid4; /* the thread identifiers*/
pthread_attr_t attr; /* set of attributes for the thread */
/* get the default attributes */
pthread_attr_init(&attr);
/* create the threads */
pthread_create(&tid1,&attr,runner,NULL);
pthread_create(&tid2,&attr,runner,NULL);
pthread_create(&tid3,&attr,runner,NULL);
pthread_create(&tid4,&attr,runner,NULL);
/* now wait for the thread to exit */
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
printf("available_resources: %d ", available_resources);
}
/**
* The thread will begin control in this function
*/
void *runner(void *param) {
int i;
for (i=1; i<= 1000000; i++) {
if (decrease_count(3) == 0) {
increase_count(3);
}
}
pthread_exit(0);
}