void startAccount(int clientSocket, char *commandArg, int *in_session) { if(*in_session >= 0){ write(clientSocket, "Cannot create account, you're in a session.", 43); return; } sem_t *bankLock = sem_open(BANKLOCK, 0); sem_wait(bankLock); // find specified account int i; for(i = 0; i < MAXACCOUNTS; i++) { if(bank->accounts[i].name[0] == '\0'){ // free account; end of list break; } if(strcmp(bank->accounts[i].name, commandArg) == 0) // found bank { // wait till account is free to use while(bank->accounts[i].in_use){ write(clientSocket, "Account currently in use, waiting to connect", 44); sleep(2); // wait 2 seconds } // start session bank->accounts[i].in_use = 1; *in_session = i; write(clientSocket, "Connected successfully!", 23); break; } } if(i >= MAXACCOUNTS){ // bank not found write(clientSocket, "Error: bank with specified account name not found.", 50); } sem_post(bankLock); return; }
/* * PosixSemaphoreCreate * * Attempt to create a new___ named semaphore. * * If we fail with a failure code other than collision-with-existing-sema, * print out an error and abort. Other types of errors suggest nonrecoverable * problems. */ static sem_t * PosixSemaphoreCreate(void) { int semKey; char semname[64]; sem_t *mySem; for (;;) { semKey = nextSemKey++; snprintf(semname, sizeof(semname), "/pgsql-%d", semKey); mySem = sem_open(semname, O_CREAT | O_EXCL, (mode_t) IPCProtection, (unsigned) 1); #ifdef SEM_FAILED if (mySem != (sem_t *) SEM_FAILED) break; #else if (mySem != (sem_t *) (-1)) break; #endif /* Loop if error indicates a collision */ if (errno == EEXIST || errno == EACCES || errno == EINTR) continue; /* * Else complain and abort */ elog(FATAL, "sem_open(\"%s\") failed: %m", semname); } /* * Unlink the semaphore immediately, so it can't be accessed externally. * This also ensures that it will go away if we crash. */ sem_unlink(semname); return mySem; }
int main(int argc, char *argv[]) { int value; sem_t *sem; if (argc != 2) usageErr("%s sem-name\n", argv[0]); sem = sem_open(argv[1], 0); if (sem == SEM_FAILED) errExit("sem_open"); if (sem_getvalue(sem, &value) == -1) errExit("sem_getvalue"); printf("%d\n", value); exit(EXIT_SUCCESS); }
sem_t* sem_open_anon() { static const char* kSemTempNameBase = "semtemp"; sem_t* sem; char sem_name[255]; snprintf(sem_name, 254, "%s%d", kSemTempNameBase, getpid()); sem = sem_open(sem_name, O_CREAT|O_EXCL, S_IRWXU, 0); if (sem == SEM_FAILED) { perror("sem_open"); return NULL; } if (sem_unlink(sem_name) < 0) { perror("WARN: sem_unlin"); } return sem; }
struct slock * s_alloc() { struct slock *sp; static int cnt; /* 引用计数 */ if ((sp = malloc(sizeof(struct slock))) == NULL) return(NULL); do { snprintf(sp-name, sizeof(sp->name), "/%ld.%d", (long)getpid(), cnt++); sp->semp = sem_open(sp->name, O_CREAT | O_EXCL, S_IRWXU, 1); } while ((sp->semp == SEM_FAILED) && (errno == EEXIST)); if (sp->semp == SEM_FAILED) { free(sp); return(NULL); } /* 销毁了名字,导致进程不能再次访问它,简化了进程结束时的清理工作 */ sem_unlink(sp->name); /* 引用为0时才delete */ return(sp); }
/** @details -# Set the SIGALRM signal handler to the ITimer signal handler routine. -# Open a unique named semaphore */ int Trick::ITimer::init() { static struct sigaction sigact; std::stringstream sem_name_stream ; sigact.sa_handler = (void (*)(int)) it_handler; if (sigaction(SIGALRM, &sigact, NULL) < 0) { perror("sigaction() failed for SIGALRM"); } // Initialize the itimer semaphore used by SIGALRM handler to // wake up the sim_exec_loop from its underrun sleepy time. sem_name_stream << "itimersepmaphore_" << getpid() ; sem_name = sem_name_stream.str() ; semaphore = sem_open(sem_name.c_str(), O_CREAT, S_IRWXU , 0); return (0) ; }
int main(int argc, char *argv[]) { if (argc != 4) err_quit("Usage: client1 <shmname> <semname> <#loops>"); int nloops = atoi(argv[3]); int fd = shm_open(argv[1], O_RDWR, FILE_MODE); struct shmstruct *ptr = mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); sem_t *mutex = sem_open(argv[2], 0); int pid = getpid(); for (int i = 0; i < nloops; i++) { sem_wait(mutex); printf("pid = %d: %d\n", pid, ptr->count++); sem_post(mutex); } }
int am_agent_instance_init_init(int id) { int status = AM_ERROR; #if defined(_WIN32) ic_sem = CreateSemaphoreA(NULL, 1, 1, get_global_name("Global\\"AM_CONFIG_INIT_NAME, id)); if (ic_sem != NULL) { status = AM_SUCCESS; } #elif defined(__APPLE__) kern_return_t rv = semaphore_create(mach_task_self(), &ic_sem, SYNC_POLICY_FIFO, 1); if (rv == KERN_SUCCESS) { status = AM_SUCCESS; } #else ic_sem = sem_open(get_global_name(AM_CONFIG_INIT_NAME, id), O_CREAT, 0600, 1); if (ic_sem != SEM_FAILED) { status = AM_SUCCESS; } #endif return status; }
sem_t open_sample_semaphore(int32_t ant, int32_t swing, int32_t dir ) { char sem_name[80]; sem_t *sem = NULL; if(dir == TXDIR) { sprintf(sem_name,"/semaphore_tx_ant_%d_swing_%d", ant, swing); } else { sprintf(sem_name,"/semaphore_rx_ant_%d_swing_%d", ant, swing); } fprintf(stderr, "usrp_driver opening: %s\n", sem_name); sem = sem_open(sem_name, 0); if (sem == SEM_FAILED) { sem = NULL; fprintf(stderr, "Getting a handle to the semaphore failed; errno is %d", errno); } return *sem; }
int main() { sem_t *mysemp; char semname[50]; sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 1); if (( mysemp == SEM_FAILED ) || (mysemp == NULL )) { puts("TEST FAILED"); return PTS_FAIL; } else { puts("TEST PASSED"); sem_close(mysemp); sem_unlink(semname); return PTS_PASS; } }
int main(int argc, char** argv) { if (2 != argc) { printf("%s name\n", argv[0]); return -1; } int flags = O_CREAT | O_RDWR; const char* _name = argv[1]; sem_t* _newsem = sem_open(_name, flags, 0644, 1); if (SEM_FAILED == _newsem) { perror("sem_open fail"); return -1; } sem_close(_newsem); return 0; }
static sem_t *IM_PosixSemOpen(const SINT8 *pSemName) { sem_t *pSem; if (NULL == pSemName) { IM_SHM_LOG(IM_SHM_LOG_FLAG, IM_SHM_LOG_ERR, "Semaphore name is null!"); return NULL; } pSem = sem_open(pSemName, 0); if (SEM_FAILED == pSem) { IM_SHM_LOG(IM_SHM_LOG_FLAG, IM_SHM_LOG_ERR, "Open semaphore %s failed. Reason: %s", pSemName, strerror(errno)); return NULL; } return pSem; }
FskErr FskSemaphoreNew_(FskSemaphore *sem, UInt32 value, FSK_SYNCHRONIZATION_DEBUG_ARGS) #endif { FskErr err; err = FskMemPtrNewClear(sizeof(FskSemaphoreRecord), (FskMemPtr *)sem); BAIL_IF_ERR(err); #if TARGET_OS_MAC { FskSemaphore s = *sem; snprintf(s->name, sizeof(s->name)-1, "/tmp/sem%lx", (unsigned long)s); if ((s->hSem = sem_open(s->name, O_CREAT|O_EXCL, 666, value)) == NULL) { BAIL(kFskErrOperationFailed); } } #else if (sem_init(&(*sem)->hSem, 0, value) != 0) { BAIL(kFskErrOperationFailed); } #endif FskInstrumentedItemNew(*sem, NULL, &gFskSemaphoreTypeInstrumentation); #if SUPPORT_INSTRUMENTATION && SUPPORT_SYNCHRONIZATION_DEBUG if (FskInstrumentedItemHasListeners(*sem)) { FskSynchronizationInstrMsgRecord msg; msg.file = file; msg.line = line; msg.function = function; FskInstrumentedItemSendMessage(*sem, kFskSynchronizationInstrMsgSemaphoreNew, &msg); } #endif bail: if ((err != kFskErrNone) && (*sem != NULL)) { FskMemPtrDispose(*sem); *sem = NULL; } return err; }
int main(void) { char semname[NAME_MAX - 4]; sem_t *mysemp; int val; snprintf(semname, sizeof(semname), "/" FUNCTION "_" TEST "_%ld", (long)getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 1); if (mysemp == SEM_FAILED || mysemp == NULL) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } /* Lock Semaphore */ if (sem_trywait(mysemp) == -1) { perror(ERROR_PREFIX "trywait"); return PTS_UNRESOLVED; } if (sem_getvalue(mysemp, &val) < 0) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } /* printf("Current value is: %d\n", val); */ if (val <= 0) { puts("TEST PASSED"); sem_close(mysemp); sem_unlink(semname); return PTS_PASS; } else { puts("TEST FAILED"); return PTS_FAIL; } }
/** * Create a new semaphore * @return the new condition variable */ sem_type Thread_create_sem() { sem_type sem = NULL; int rc = 0; FUNC_ENTRY; #if defined(WIN32) || defined(WIN64) sem = CreateEvent( NULL, // default security attributes FALSE, // manual-reset event? FALSE, // initial state is nonsignaled NULL // object name ); #elif defined(USE_NAMED_SEMAPHORES) if (named_semaphore_count == 0) memset(named_semaphores, '\0', sizeof(named_semaphores)); char* name = &(strrchr(tempnam("/", "MQTT"), '/'))[1]; /* skip first slash of name */ if ((sem = sem_open(name, O_CREAT, S_IRWXU, 0)) == SEM_FAILED) rc = -1; else { int i; named_semaphore_count++; for (i = 0; i < MAX_NAMED_SEMAPHORES; ++i) { if (named_semaphores[i].name[0] == '\0') { named_semaphores[i].sem = sem; strcpy(named_semaphores[i].name, name); break; } } } #else sem = malloc(sizeof(sem_t)); rc = sem_init(sem, 0, 0); #endif FUNC_EXIT_RC(rc); return sem; }
int main(int argc, char **argv) { int fd, nloops; sem_t *sem; int *ptr; if(argc != 4) { printf("Usage: %s <semname> <shmname> <#nloops>\n", argv[0]); exit(1); } nloops = atoi(argv[3]); /* create semaphore */ sem = sem_open(argv[1], O_RDWR); if(sem == SEM_FAILED) { perror("sem_open error"); exit(1); } /* create shared memory object */ fd = shm_open(argv[2], O_RDWR, 0); if(fd < 0) { perror("shm_open error"); exit(1); } ptr = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(ptr == MAP_FAILED) { perror("mmap error"); exit(1); } close(fd); for(int i = 0; i < nloops; ++i) { sem_wait(sem); printf("pid=%ld, value=%d\n", (long)getpid(), (*ptr)++); sem_post(sem); } exit(0); }
int main(int argc, char **argv) { sem_t *sem; int val; int ret = 0; if (argc != 2) { printf("usage: semgetvalue <name>\n"); return -1; } sem = sem_open(argv[1], 0); assert(SEM_FAILED != sem); ret = sem_getvalue(sem, &val); assert(-1 != ret); printf("value = %d\n", val); return 0; }
void* mfilter_init() { //TODO: MailFilter * filter = new MailFilter; if(filter) { filter->isJunk = -1; } tm * ltm; time_t tt = time(NULL); ltm = localtime(&tt); char szFileName[1024]; sprintf(szFileName, "/var/log/erisemail/MTA-%04d-%02d-%02d.log", 1900 + ltm->tm_year, ltm->tm_mon + 1, ltm->tm_mday); filter->semLog = sem_open("/.erisemail-mta-log.sem", O_CREAT | O_RDWR, 0644, 1); filter->fLog = fopen(szFileName, "ab+"); return (void*)filter; }
int main(int argc,char *argv[]) { int n=0; sem=sem_open(argv[1],O_CREAT,0644,3); sem_getvalue(sem,&val); printf("The value have %d\n",val); while(n++<5) { if(fork()==0) { sem_wait(sem); print(getpid()); sleep(1); sem_post(sem); printf("I'm finished,my pid is %d\n",getpid()); return 0; } } wait(); return 0; }
// Client side : get the published semaphore from server bool JackPosixSemaphore::ConnectInput(const char* name, const char* server_name) { BuildName(name, server_name, fName, sizeof(fName)); jack_log("JackPosixSemaphore::Connect name = %s", fName); // Temporary... if (fSemaphore) { jack_log("Already connected name = %s", name); return true; } if ((fSemaphore = sem_open(fName, O_CREAT)) == (sem_t*)SEM_FAILED) { jack_error("Connect: can't connect named semaphore name = %s err = %s", fName, strerror(errno)); return false; } else { int val = 0; sem_getvalue(fSemaphore, &val); jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val); return true; } }
static inline void subprocess_leak(void) { struct sigevent sevt; pthread_mutex_t mutex; pthread_cond_t cond; pthread_t thread; sem_t sem, *psem; timer_t tm; int fd; check_pthread(pthread_create(&thread, NULL, empty, NULL)); check_pthread(pthread_mutex_init(&mutex, NULL)); check_pthread(pthread_cond_init(&cond, NULL)); check_unix(sem_init(&sem, 0, 0)); check_unix(-!(psem = sem_open(SEM_NAME, O_CREAT, 0644, 1))); sevt.sigev_notify = SIGEV_THREAD_ID; sevt.sigev_signo = SIGALRM; sevt.sigev_notify_thread_id = syscall(__NR_gettid); check_unix(timer_create(CLOCK_MONOTONIC, &sevt, &tm)); check_unix(fd = mq_open(MQ_NAME, O_RDWR | O_CREAT, 0644, NULL)); }
static void test_sem_init_too_large_inital_value(void) { sem_t sem; sem_t *sem2; int rv; errno = 0; rv = sem_init( &sem, 0, (unsigned int) SEM_VALUE_MAX + 1 ); rtems_test_assert( rv == -1 ); rtems_test_assert( errno == EINVAL ); errno = 0; sem2 = sem_open( "sem", O_CREAT | O_EXCL, 0777, (unsigned int) SEM_VALUE_MAX + 1 ); rtems_test_assert( sem2 == SEM_FAILED ); rtems_test_assert( errno == EINVAL ); }
int sim_open_sem(char * sem_name, sem_t ** mutex_sync, int max_attempts) { int iter = 0, max_iter = max_attempts; if (!max_iter) max_iter = 10; while ((*mutex_sync) == SEM_FAILED && iter < max_iter) { (*mutex_sync) = sem_open(sem_name, 0, 0755, 0); if ((*mutex_sync) == SEM_FAILED && max_iter > 1) { int err = errno; info("ERROR! Could not open semaphore (%s)-- %s", sem_name, strerror(err)); sleep(1); } ++iter; } if ((*mutex_sync) == SEM_FAILED) return -1; else return 0; }
int main(int argc, char **argv) { int ret = 0; sem_t *sem; CHECK_ARGS(2, "<path>"); sem = sem_open(argv[1], 0); if (sem == (sem_t *)SEM_FAILED) { perror("sem_open"); exit(1); } if ((ret = sem_post(sem)) < 0) perror("sem_post"); sem_close(sem); exit(ret); }
int notification_imp::notification_thread_init() { int rc; sem_unlink("/notify_waiting_sem"); if ((notify_waiting = sem_open("/notify_waiting_sem", O_CREAT | O_EXCL, 0644, 0)) == SEM_FAILED) { perror("sem_open"); exit(-1); } rc = pthread_create(&h_thread, NULL, ¬ification_imp::dispatch_thread, (void *)this); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } return 0; }
int main(void) { sem_t *mysemp; char semname[50]; sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid()); mysemp = sem_open(semname, O_CREAT, 0777, 1); /* Checking if mysemp has a value returned. From sem_open */ if (&mysemp != NULL) { puts("TEST PASSED"); sem_close(mysemp); sem_unlink(semname); return PTS_PASS; } else { puts("TEST FAILED"); return PTS_FAIL; } }
bool init() { if(lbhData != MAP_FAILED) return true; fd = shm_open(LBH_MEM_NAME, O_RDWR, S_IRUSR | S_IWUSR); if(fd == -1) return false; sem = sem_open(LBH_SEM_NAME, O_RDWR, S_IRUSR | S_IWUSR, 0); if(sem == SEM_FAILED) { close(fd); fd = -1; return false; } VERIFY((lbhData = (LBHData*)mmap(NULL, sizeof(LBHData), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) != MAP_FAILED); lbhData->state = okState; return true; }
int main() { sem_t *mysemp; char semname[50]; int value = 10; int val; sprintf(semname, "/" FUNCTION "_" TEST "_%ld", (long)getpid()); /* Initial value of Semaphore is 10 */ mysemp = sem_open(semname, O_CREAT, 0777, value); if( mysemp == SEM_FAILED || mysemp == NULL ) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } while (value) { // checking the value if zero yet. if( sem_wait(mysemp) == -1 ) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } else { value--; } } if( sem_getvalue(mysemp, &val) == -1 ) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } else if( val == 0 ) { puts("TEST PASSED"); sem_unlink(semname); sem_close(mysemp); return PTS_PASS; } else { puts("TEST FAILED: Semaphore is not locked"); return PTS_FAIL; } }
int main (void) { sem_t *s; int i; on_exit (remove_sem, (void *) "/glibc-tst-sem8"); for (i = 0; i < 3; ++i) { s = sem_open ("/glibc-tst-sem8", O_CREAT, 0600, 1); if (s == SEM_FAILED) { if (errno == ENOSYS) { puts ("sem_open not supported. Oh well."); return 0; } /* Maybe the shm filesystem has strict permissions. */ if (errno == EACCES) { puts ("sem_open not allowed. Oh well."); return 0; } printf ("sem_open: %m\n"); return 1; } /* Now close the handle. */ if (sem_close (s) != 0) { puts ("sem_close failed"); return 1; } } return 0; }
int main() { char semname[50]; int val; sprintf(semname, "/" FUNCTION "_" TEST "_%ld", (long)getpid()); gsemp = sem_open(semname, O_CREAT, 0777, SEMINITVAL); if( gsemp == SEM_FAILED || gsemp == NULL ) { perror(ERROR_PREFIX "sem_open"); return PTS_UNRESOLVED; } signal(SIGALRM, sighdl); sleep(1); alarm(1); if( sem_post(gsemp) == -1 ) { perror(ERROR_PREFIX "sem_post"); exit(PTS_UNRESOLVED); } /* Checking if the value of the Semaphore incremented by one */ if( sem_getvalue(gsemp, &val) == -1 ) { perror(ERROR_PREFIX "sem_getvalue"); return PTS_UNRESOLVED; } if (val != SEMINITVAL+1) { #ifdef DEBUG printf("semaphore value was not incremented\n"); #endif printf("TEST FAILED\n"); return PTS_FAIL; } #ifdef DEBUG printf("semaphore value was %d\n", val); #endif printf("TEST PASSED\n"); sem_close(gsemp); sem_unlink(semname); return PTS_PASS; }