Question: 1. Making water. This problem is simulating the process of hooking up hydrogen atoms with oxygen atoms to form water molecules. Each atom is represented
1. Making water. This problem is simulating the process of hooking up hydrogen atoms with oxygen atoms to form water molecules. Each atom is represented by a separate thread. We will need to associate two hydrogen threads with one oxygen thread to make a water molecule, then all three threads exit together. We will use two general semaphores, one to count the number of hydrogen threads and one for the number of oxygen threads. Please use Pthread for multi-threading and Pthread semaphores for synchronization. (3 pts)
sem_t oxygen, hydrogen; // two Pthread semaphores int main(int argc, char* argv[]) {
//fill in your code to initialize two semaphores using sem_init()
int num_of_water = atoi(argv[1]); //# of water molecules pthread_t tidox[num_of_water], tidhydro[2*num_of_water]; //create hydrogen atom threads for (int k = 0; k < num_of_water; k++)
pthread_create(&tidox[k], NULL, Oxygen, NULL);
//create hydrogen atom threads for (int k = 0; k < 2*num_of_water; k++)
pthread_create(&tidhydro[k], NULL, Hydrogen, NULL);
for (int k = 0; k < 2*num_of_water; k++) pthread_join(tidhydro[k], NULL);
for (int k = 0; k < num_of_water; k++) pthread_join(tidox[k], NULL);
//fill in your code to destroy semaphores using sem_destroy()
return 0; }
void* Oxygen(void* arg) {
//fill in your synchronization code
}
void* Hydrogen(void* arg) {
//fill in your synchronization code
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
