void my_lock(int fd) { int oflag, i; union semun arg; struct semid_ds seminfo; if (initflag == 0) { oflag = IPC_CREAT | IPC_EXCL | SVSEM_MODE; if ((semid = semget(Ftok(LOCK_PATH, 0), 1, oflag)) >= 0) { arg.val = 1; Semctl(semid, 0, SETVAL, arg); } else if (errno == EEXIST) { semid = Semget(Ftok(LOCK_PATH, 0), 1, SVSEM_MODE); arg.buf = &seminfo; for (i = 0; i < MAX_TRIES; i++) { Semctl(semid, 0, IPC_STAT, arg); if (arg.buf->sem_otime != 0) goto init; sleep(1); } err_quit("semget OK, but semaphore not initalized"); } else err_sys("semget error"); init: initflag = 1; postop.sem_num = 0; postop.sem_op = 1; postop.sem_flg = SEM_UNDO; waitop.semnum = 0; waitop.semop = -1; waitop.semflg = SEM_UNDO; } Semop(semid, &waitop, 1); }
int SMP_create(size_t size) { int semflg = 0600; DDI_Comm *comm = (DDI_Comm *) Comm_find(DDI_COMM_WORLD); /* hardcoded atm */ SMP_Data *new_data = (SMP_Data *) Malloc(sizeof(SMP_Data)); SMP_Data *end_data = (SMP_Data *) SMP_find_end(); STD_DEBUG((stdout,"%s: Entered DDI_SMP_Create.\n",DDI_Id())) if(end_data) end_data->next = (void *) new_data; else gv(smp_base_data) = (SMP_Data *) new_data; # if defined USE_SYSV Comm_sync_smp(comm); if(comm->me_local == 0) { new_data->handle = gv(smp_data_id)++; new_data->shmid = gv(shmid) = Shmget(IPC_PRIVATE,size,SHM_R|SHM_W); new_data->semid = Semget(IPC_PRIVATE,1,semflg); new_data->size = size; new_data->next = NULL; } Comm_bcast_smp(new_data,sizeof(SMP_Data),0,comm); new_data->addr = Shmat(new_data->shmid,0,0); MAX_DEBUG((stdout,"%s: SMP memory [%i] shmid=%i, semid=%i, addr=%x.\n", DDI_Id(),new_data->handle,new_data->shmid,new_data->semid,new_data->addr)) Comm_sync_smp(comm); if(comm->me_local == 0) { Shmctl(new_data->shmid,IPC_RMID,NULL); gv(shmid) = 0; } Comm_sync_smp(comm); # else new_data->handle = gv(smp_data_id)++; new_data->size = size; new_data->next = NULL; new_data->addr = Malloc(size); /* MWS: May 2010 It appears above that systems without SysV memory are expected to allocate Process-replicated memory instead of Node-replicated, and get on with it. If these are duplicated, at full size, as it appears, that's likely devastating for the system total memory usage. The parallel CCSD(T) on IBM Blue Gene/P got into a deadlock, but other systems with sockets or MPI seem to work if allowed to proceed. At this time, we kill off just the BG here... */ # ifdef IBMBG fprintf(stdout,"DDI compiled w/o SysV operating system support.\n"); fprintf(stdout,"IBM/BG parallel CCSD(T) cannot run w/o SysV.\n"); Fatal_error(911); # endif # endif return new_data->handle; }
int main(int argc, char **argv) { int i, nthreads; pthread_t tid[MAXNTHREADS]; union semun arg; if (argc != 3) err_quit("usage: incr_svsem2 <#loops> <#threads>"); nloop = atoi(argv[1]); nthreads = min(atoi(argv[2]), MAXNTHREADS); /* 4create semaphore and initialize to 0 */ shared.semid = Semget(IPC_PRIVATE, 1, IPC_CREAT | SVSEM_MODE); arg.val = 0; Semctl(shared.semid, 0, SETVAL, arg); postop.sem_num = 0; /* and init the two semop() structures */ postop.sem_op = 1; postop.sem_flg = SEM_UNDO; waitop.sem_num = 0; waitop.sem_op = -1; waitop.sem_flg = SEM_UNDO; /* 4create all the threads */ Set_concurrency(nthreads); for (i = 0; i < nthreads; i++) { Pthread_create(&tid[i], NULL, incr, NULL); } /* 4start the timer and release the semaphore */ Start_time(); Semop(shared.semid, &postop, 1); /* up by 1 */ /* 4wait for all the threads */ for (i = 0; i < nthreads; i++) { Pthread_join(tid[i], NULL); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared.counter != nloop * nthreads) printf("error: counter = %ld\n", shared.counter); Semctl(shared.semid, 0, IPC_RMID); exit(0); }
void my_lock(int fd) { int oflag, i; union semun arg; struct semid_ds seminfo; if (initflag == 0) { oflag = IPC_CREAT | IPC_EXCL | SVSEM_MODE; if ( (semid = semget(Ftok(LOCK_PATH, 0), 1, oflag)) >= 0) { /* 4success, we're the first so initialize */ arg.val = 1; Semctl(semid, 0, SETVAL, arg); } else if (errno == EEXIST) { /* 4someone else has created; make sure it's initialized */ semid = Semget(Ftok(LOCK_PATH, 0), 1, SVSEM_MODE); arg.buf = &seminfo; for (i = 0; i < MAX_TRIES; i++) { Semctl(semid, 0, IPC_STAT, arg); if (arg.buf->sem_otime != 0) goto init; Write(pipefd[1], "", 1); /* tell parent */ sleep(1); } err_quit("semget OK, but semaphore not initialized"); } else err_sys("semget error"); init: initflag = 1; postop.sem_num = 0; /* and init the two semop() structures */ postop.sem_op = 1; postop.sem_flg = SEM_UNDO; waitop.sem_num = 0; waitop.sem_op = -1; waitop.sem_flg = SEM_UNDO; } Semop(semid, &waitop, 1); /* down by 1 */ }
int main(int argc, char **argv) { int semid, nsems, i; struct semid_ds seminfo; unsigned short *ptr; union semun arg; if (argc < 2) err_quit("Usage: setvalue <pathname> [ value ...]"); //first get the number of semaphore in the set semid = Semget(Ftok(argv[1], 0), 0, 0); arg.buf = &seminfo; Semctl(semid, 0, IPC_STAT, arg); nsems = arg.buf->sem_nsems; //now get the values from the command line ptr = Calloc(nsems, sizeof(unsigned short)); arg.array = ptr; for (i = 0; i < nsems; i++) ptr[i] = atoi(argv[i + 2]); Semctl(semid, 0, SETALL, arg); exit(0); }