void gender3(){ int val1 = semvalue(1); int val2 = semvalue(2); int up, down; if ((val1 == 1001)||semvalue(1)==1001){ down = semdown(3); }else { up = semup(1); up = semup(2); up = semup(4); repTIME = rand()%200000+2000; usleep(repTIME); int time = rand()%40000+1000; usleep(time); } }
/* what does this program do? First we make a semaphore check what its value is do semdown on it start sleep 10 seconds -> at this point, another program would try to semdown the semaphore we created end sleep now do a semup, program B should be able to proceed. -> program C should now free the semaphore, and try a semvalue on it. */ int main() { int result; int e; message m; e = errno; printf("+-----------------+\n"); printf("| Program A Start |\n"); printf("+-----------------+\n"); result = seminit(2000, 1); printf("<1> Create semaphore named 2000, with value 1: result=%d & e=%d\n", result, errno); result = semvalue(2000); printf("<2> Look at value of semaphore[index for indentifier(2000)]: result=%d & e=%d\n", result, errno); result = semdown(2000); printf("<3> Do a sem down: result=%d & e=%d\n", result, errno); printf("<4> I am to do nothing for 10 seconds.\n"); int wait_unit; for (wait_unit = 1; wait_unit < 11; wait_unit++) { sleep(1); printf(" Waiting %d\n", wait_unit); } printf("<4> I done doing nothing.\n"); result = semup(2000); printf("<5> Sem Up: result=%d & e=%d\n", result, errno); printf("<6> Program A has no more to do.\n", result, errno); return 0; }
void gender2(){ int val1 = semvalue(1); int val3 = semvalue(3); int down, up; if ((val1 == 1001)&&(val3 == 1001)){ down = semdown(2); }else { int nursVAL = semvalue(4); while (nursVAL == 1001); up = semup(2); up = semup(3); while (repTIME == 0); usleep(repTIME); int time = rand()%4000000+1000; usleep(time); } }
int main() { int semid; int semval; seminit(&semid, 0); semval = semvalue(semid); printf("semval: %d\n", semval); semv(semid); semval = semvalue(semid); printf("semval: %d\n", semval); semp(semid); #if 0 #endif semdistr(semid); return 0; }
/* what does this program do? First we make a semaphore check what its value is do semdown on it start sleep 10 seconds -> at this point, another program would try to semdown the semaphore we created end sleep now do a semup, program B should be able to proceed. -> program C should now free the semaphore, and try a semvalue on it. */ int main() { int result; int e; message m; e = errno; printf("+-----------------+\n"); printf("| Program D Start |\n"); printf("+-----------------+\n"); result = semfree(2000); printf("<1> Free semaphore (return 1 if successful): result=%d & e=%d\n", result, errno); result = semvalue(2000); printf("<2> Can we still see value? this should fail: result=%d & e=%d\n", result, errno); return 0; }