int main(int argc, char* argv[]) { init(); int action; do { printf("Choose an action to perform:\n"); printf("1 - Create a new productor.\n"); printf("2 - Create a new consumer.\n"); printf("3 - Display the system state.\n"); printf("4 - Quit\n"); scanf("%d", &action); switch(action) { case 1: createProductor(); break; case 2: createConsumer(); break; case 3: displaySystemState(); break; case 4: break; default: printf("Incorrect action!\n"); } printf("\n"); } while(action != 4); return 0; }
//----------------------------------------------------------------------------- void Consumer::init(CORBA::ORB_ptr orb) { //setup profiling stuff here handlerTimeoutMap_m = CDBProperties::getEventHandlerTimeoutMap(channelName_mp); profiler_mp = new Profiler(); // Must call resolveNamingService B-4 resolveNotifyChannel! // using activator's orb resolveNamingService(orb); // Create the NC if(!resolveInternalNotificationChannel()) ACS_SHORT_LOG((LM_ERROR,"NC '%s' couldn't be created nor resolved", channelName_mp)); //create consumer corba objects createConsumer(); if(notifyFactory_m == 0) resolveNotificationFactory(); if (orbHelper_mp != 0 ) callback_m->init(orbHelper_mp->getORB(), notifyFactory_m); else callback_m->init(orb, notifyFactory_m); }
main(){ system("./clear.sh"); srand(time(NULL)); int full,empty,mutex,shmid; /*********** creating semaphores and shared memory *********/ key_t keyFull = ftok(FULLKEYFILE,FULLKEYID); if((full = semget(keyFull,1,0666|IPC_CREAT)) < 0){ printf("\nCANT CREATE SEMAPHORE {full}\n"); exit(0); } key_t keyEmpty = ftok(EMPTYKEYFILE,EMPTYKEYID); if((empty = semget(keyEmpty,1,0666|IPC_CREAT)) < 0){ printf("\nCANT CREATE SEMAPHORE {empty}\n"); exit(0); } key_t keyMutex = ftok(MUTEXKEYFILE,MUTEXKEYID); if((mutex = semget(keyMutex,1,0666|IPC_CREAT)) < 0){ printf("\nCANT CREATE SEMAPHORE {mutex}\n"); exit(0); } key_t keyShared = ftok(SHAREDKEYFILE,SHAREDKEYID); if((shmid = shmget(keyShared,(N+2)*sizeof(int),0777|IPC_CREAT)) < 0){ printf("\nCANT ALLOCATE SHARED MEMORY\n"); exit(0); } /***********************************************************/ /*********** init shared mem and semaphore *****************/ int *a = (int *)shmat(shmid,0,0); a[IN] = 0; a[OUT] = 0; shmdt(a); if(semctl(full,0,SETVAL,(ushort)0) < 0){ printf("\nCANT INITIALIZE FULL\n"); exit(0); } if(semctl(empty,0,SETVAL,(ushort)N) < 0){ printf("\nCANT INITIALIZE FULL\n"); exit(0); } if(semctl(mutex,0,SETVAL,(ushort)1) < 0){ printf("\nCANT INITIALIZE FULL\n"); exit(0); } /************************************************************/ /****************** forking *********************************/ int prodpid[PRODCOUNT],conpid[CONCOUNT]; for(int i=0;i<PRODCOUNT;i++){ prodpid[i] = createProducer(i+1,shmid,full,empty,mutex); } for(int i=0;i<CONCOUNT;i++){ conpid[i] = createConsumer(i+1,shmid,full,empty,mutex); } int status; for(int i=0;i<PRODCOUNT;i++){ waitpid(prodpid[i],&status,0); } for(int i=0;i<CONCOUNT;i++){ waitpid(conpid[i],&status,0); } /************************************************************/ printf("\n"); system("./clear.sh"); return 0; }