void main() { unsigned int i; int lock,cond; Write("\nTest Create lock\n", 18,1 ); lock = CreateLock("lock",4); AcquireLock(lock); cond=CreateCondition("condition",9); SignalCondition(cond,lock); ReleaseLock(lock); Exit(0); }
int main() { int cv1=0; int l1=CreateLock(); Write("\n Running cvtest5.c\n",22,1); Write("\nTESTING: CVTEST5.C TRIES TO ACCESS THE CV NOT CREATED BY IT",60,1); Exec("../test/cvtest1",15); Yield(); Yield(); Yield(); WaitCondition(cv1,l1); SignalCondition(cv1,l1); BroadcastCondition(cv1,l1); Exit(0); }
void consumer(void *arg) { char *name = arg; while ( true ) { EnterMonitor(mon); while ( !key ) WaitCondition(cond); printf("%s: key %c\n", name, key); key = 0; SignalCondition(cond); LeaveMonitor(mon); } }
void main() { Print("\nTest2 is for waiting and signaling condition variable.\n", sizeof("\nTest2 is for waiting and signaling condition variable.\n")/sizeof(char), 0, 0); l1 = CreateLock("l1", 2); c1 = CreateCondition("c1", 2); m1 = CreateMV("m1", 2, 3); AcquireLock(l1); Print("client2 of Test2 has acquired lock1.\n", sizeof("client2 of Test2 has acquired lock1.\n")/sizeof(char), 0, 0); SetMV(m1, 1, 10); SignalCondition(c1, l1); Print("client2 of Test2 signals client1.\n", sizeof("client2 of Test2 signals client1.\n")/sizeof(char), 0, 0); ReleaseLock(l1); Print("client2 of Test2 has released lock1.\n", sizeof("client2 of Test2 has released lock1.\n")/sizeof(char), 0, 0); Exit(0); }
int main(void) { mon = CreateMonitor("dos"); cond = CreateCondition("key", mon); Ready(CreateTask(consumer, 2000, "consumer", "consumer", DEFAULT_PRIO)); while ( true ) { EnterMonitor(mon); while ( key ) WaitCondition(cond); key = getch(); if ( key == 'S' || key == 's' ) return 0; SignalCondition(cond); LeaveMonitor(mon); } }
void Cashier() { int holdingES,i; int _totalAmount, _cashWaitingCount, _patientType, _pid, _xrayStatus, _xrayImages, _shotStatus; while (1) { /*----------------------------------------------------- // Cashier wait for Nurse bringing patient to his desk //-----------------------------------------------------*/ AcquireLock(cashWaitingLock); _cashWaitingCount = GetMV(cashWaitingCount, 0); if (_cashWaitingCount > 0) { SetMV(cashWaitingCount, 0,_cashWaitingCount-1); SignalCondition(cashWaitingCV, cashWaitingLock); AcquireLock(cashLock); ReleaseLock(cashWaitingLock); WaitCondition(cashCV, cashLock); /*------------------------------------------ // Cashier gets the ES and calculate the fee //------------------------------------------*/ holdingES = GetMV(cashExamSheet, 0); _patientType = GetMV(esStack, holdingES*18+8); _pid = GetMV(esStack, holdingES*18+0); if(_patientType == 0) { Print("Cashier receives the examination sheet from Adult Patient [%d].\n", sizeof("Cashier receives the examination sheet from Adult Patient [%d].\n")/sizeof(char), 1, _pid); } else { Print("Cashier receives the examination sheet from Child Patient [%d] from Parent [%d].\n", sizeof("Cashier receives the examination sheet from Child Patient [%d] from Parent [%d].\n")/sizeof(char), 2, _pid*256+_pid); } _totalAmount = 20; _xrayStatus = GetMV(esStack, holdingES*18+11); _xrayImages = GetMV(esStack, holdingES*18+12); if (_xrayStatus == 4) { for (i=0; i<_xrayImages; i++) { _totalAmount += 10; } _totalAmount += 20; } _shotStatus = GetMV(esStack, holdingES*18+14); if (_shotStatus == 3) { _totalAmount += 20; } SetMV(esStack, holdingES*18+17, _totalAmount); if(_patientType == 0) { Print("Cashier reads the examination sheet of Adult Patient [%d] and asks him to pay $ [%d].\n", sizeof("Cashier reads the examination sheet of Adult Patient [%d] and asks him to pay $ [%d].\n")/sizeof(char), 2, _pid*256+_totalAmount); } else { Print("Cashier reads the examination sheet of Child Patient [%d] and asks Parent [%d] to pay $ [%d].\n", sizeof("Cashier reads the examination sheet of Child Patient [%d] and asks Parent [%d] to pay $ [%d].\n")/sizeof(char), 3, _pid*65536+_pid*256+_totalAmount); } SignalCondition(cashCV, cashLock); WaitCondition(cashCV, cashLock); if(_patientType == 0) { Print("Cashier accepts $[%d] from Adult Patient [%d].\n", sizeof("Cashier accepts $[%d] from Adult Patient [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); Print("Cashier gives a receipt of $[%d] to Adult Patient [%d].\n", sizeof("Cashier gives a receipt of $[%d] to Adult Patient [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); } else { Print("Cashier accepts $[%d] from Parent [%d].\n", sizeof("Cashier accepts $[%d] from Parent [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); Print("Cashier gives a receipt of $[%d] to Parent [%d].\n", sizeof("Cashier gives a receipt of $[%d] to Parent [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); } SignalCondition(cashCV, cashLock); WaitCondition(cashCV, cashLock); ReleaseLock(cashLock); } else { ReleaseLock(cashWaitingLock); } Yield(); } }