Return to Operationg Systems home
Suppose to have in substitution of the function test-and-set and of the function swap, the following atomic function:
int atomicIncrement (int *var) { int tmp = *var; *var = tmp + 1; return (tmp); }
Use this function to write the functions init, lock, and unlock to insert in the reservation code and in the release code of a critical section.
Suggestion:
Use two global variables ticketNumber and turnNumber.
The first indicates the reservation order to enter the critical section, and the second manages the turn for accessing the critical section.
Manage these variables through the functions init, lock, and unlock.
Given the following precedence graph:
A <--------
/ | \ ^
/ | \ |
B C D |
| / \ | |
| / \ | |
| E F | |
| \ / | |
| \ / | |
| G H |
\ | / |
\ | / |
I ---------
in which each vertex corresponds to an independent task (a process or a thread).
Implement it with the following ways, and using the minimum number of semaphores.
Each task corresponds to a process.
Each process is (re-)created and destroyed at each iteration of the principal cycle.
Semaphores are implemented by means of pipes.
Each task corresponds to a thread.
Each thread is (re-)created and destroyed at each iteration of the principal cycle.
Semaphores are implemented by means of the POSIX library, and types/functions sem_*.
Each task corresponds to a thread.
Threads are all cyclic and they are created and destroyed only one time in the principal cycle.
Semaphores are implemented by means of the POSIX library, and types/functions sem_*.