SOFT3410 Tutorial 6 Synchronisation 1
Question 1: Shared Memory and Locking
Below is an example of two threads accessing shared data. Compile this program and observe the final value.
struct thread_data { int value;
};
void* work1(void* arg) {
struct thread_data* data = (struct thread_data*) arg; for(int i = 0; i < 10000; i++) {
data->data += 1; return NULL;
}
int main() {
}
}
struct thread_data data = { 0 };
pthread_t threads[2];
pthread_create(threads, NULL, work, &data); pthread_create(threads+1, NULL, work, &data);
for(int i = 0; i < 2; i++) { pthread_join(threads[i], NULL);
}
return 0;
• When running this program multiple times, is the final result consistent?
• If you have observed the final value does not meet the expected result, discuss why this could be happening
• How could ensure that the final result is what we expect? What modifications do we need to make?
1
}
}
*(tdata->account) += amount;
sleep(1);
}
return NULL;
int main() {
int data = 100;
pthread_t threads[THREAD_COUNT];
struct thread_data tdata[THREAD_COUNT] = { { .account = &data, .amount = 10 }, { .account = &data, .amount = -10 }
};
for(int i = 0; i < THREAD_COUNT; i++) { pthread_create(threads+i, NULL,
operation_on_money, tdata+i);
}
for(int i = 0; i < THREAD_COUNT; i++) {
pthread_join(threads[i], NULL); return 0;
}
What can you observe with this program? What is the expected value of account? Does it meet the expected the value or does something occur?
SOFT3410 Synchronisation 1
Question 2: A bank with single customer
As a simple starter question for the semester, you are tasked with writing a simple Hello World program. You should utilise the printf or puts function for this task.
#include
#include
#define THREAD_COUNT (2)
struct thread_data { int* account;
int amount;
};
void* operation_on_money(void* data) { struct thread_data* tdata = data; int amount = tdata->amount; for(int i = 0; i < 60; i++) {
Concurrency Page 2 of 3
Question 3: Locking the bank
Using what you have learned from the previous questions, use mutexes to prevent the above race con- dition from occurring on the accounts. Given the following function signature deposit(struct account* from, struct account* to, int amount),considerhowyoucouldencounter a deadlock. Elabroate and explain why you would encounter the deadlock and how you could prevent
it from occurring.
Question 4: Homework - Coarse Grained Locking
Use your linked list that you have constructed previously or you could construct a dynamic array for this task if you had not completed the linked list. The following functions must be thread safe.
void linkedlist_map_put(struct linkedlist_map* map, void* key void* value);
void* linkedlist_map_get(struct linkedlist_map* map, void* key);
void* linkedlist_map_remove(struct linkedlist_map* map, void* key);
Any methods that will retrieve, insert or change the data in your linked list or dynamic array must be threadsafe.
• Construct different test cases with different number of elements
• Using the test cases, measure and record the execution of the data structure
• Record these results and use gnuplot to graph the data
• Using techniques from the previous lecture and tutorial, apply them, benchmark, graph and compare a with baseline and other combinations of techniques
• Write a conclusion and explain your observations
• In your conclusion, create a proposal to improve the execution time of your thread safe linked list
For your submission, your report must be submitted to canvas via TurnitIn. You must submit your report to canvas portal by 11:59pm, 18th October, 2020.
SOFT3410 Synchronisation 1
Concurrency Page 3 of 3