1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
void oxyAtom(int o);
int main(int argc, char** argv) {
int oxygen = 0, hydrogen = 0,
numOxy, numHydro,
oxyIndex, hydroIndex,
mutex, oxyQueue, total,
hydroBonded, hydroQueue;
numOxy = stoi(argv[1]);
numHydro = stoi(argv[2]);
total = numOxy + numHydro;
oxyIndex = numOxy;
hydroIndex = numHydro;
mutex = semget(SEMKEY_A, 1, 0700|IPC_CREAT);
semctl(mutex, 0, SETVAL, 1); //mutex = Semaphore(1)
oxyQueue = semget(SEMKEY_B, 1, 0700|IPC_CREAT);
semctl(oxyQueue, 0, SETVAL, 0); //oxyQueue = Semaphore(0)
hydroBonded = semget(SEMKEY_C, 2, 0700|IPC_CREAT);
semctl(hydroBonded, 0, SETVAL, 0); //hydroBonded = Semaphore(0)
semctl(hydroBonded, 1, SETVAL, 0);
hydroQueue = semget(SEMKEY_D, 2, 0700|IPC_CREAT);
semctl(hydroQueue, 0, SETVAL, 0); //hydroQueue = Semaphore(0)
semctl(hydroQueue, 1, SETVAL, 0);
//pthread_t t[total];
thread t[total];
//CREATING THREADS
while(oxyIndex-1 >= 0 || hydroIndex-1 >= 0) {
if(oxyIndex-1 >= 0) {
t[oxyIndex-1] = thread(oxyAtom, oxyIndex);
oxyIndex--;
}
if(hydroIndex-1 >= 0) {
t[total-1] = thread(hydroAtom, hydroIndex);
hydroIndex--;
}
}
void oxyAtom(int o) {
...
}
}
|