/* This program initializes semaphores on kiwi (and sister machines) */ /* First it gets 'em (they come set to 0 (block) "from the factory") */ /* and shows their values. Then, they're all set to 1. They get */ /* their values printed again. Then sleep for 5 seconds (you might */ /* want to suspend and do an ipcs to see the semaphore). Finally, */ /* semaphore set is removed from the system. */ /* YOU MUST BE SURE YOUR SEMAPHORES ARE REMOVED FROM THE SYSTEM AFTER*/ /* YOU USE THIS PROGRAM OR ANY OTHER THAT USES SEMAPHORES. TO DO SO */ /* FROM THE COMMAND LINE, ENTER ipcs TO GET THE SEMAPHORE SET ID, AND*/ /* THEN ISSUE ipcrm -s . Do ipcs to double check*/ #include #include #include #include #include #include #include /* #define NumSems 3 */ union semun { int val; struct semid_ds* buf; unsigned short *array; /* Array for GETALL, SETALL */ #if defined(_linux_) struct seminfo *__buf; /* Buffer for IPC_INFO */ #endif } arg; void PrintSems(semid,NumSems) int semid,NumSems; {int i; short *values; // Have to allocate space for array of short NOTE: man page says you don't. Values=(short *)malloc(sizeof(short)*NumSems); semctl(semid,NULL,GETALL,&values); // don't have to use union here? fprintf(stderr,"Semaphore Values "); for (i=0;i\n"); exit(1); } NumSems=atoi(argv[1]); arg.array=(ushort *)malloc(NumSems*sizeof(ushort)); key=getuid(); semid = semget(key, NumSems, 0600|IPC_CREAT); semctl(semid,0,IPC_STAT,&s); fprintf(stderr,"Semaphore id %d: ", semid); PrintSems(semid,NumSems); for (i=0;i