当前位置: 首页>>代码示例>>C++>>正文


C++ sem_open函数代码示例

本文整理汇总了C++中sem_open函数的典型用法代码示例。如果您正苦于以下问题:C++ sem_open函数的具体用法?C++ sem_open怎么用?C++ sem_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了sem_open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: PosixSemaphoreCreate

/*
 * 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;
}
开发者ID:EccentricLoggers,项目名称:peloton,代码行数:51,代码来源:posix_sema.cpp

示例2: main

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;
}
开发者ID:SummerSnail2014,项目名称:haiku,代码行数:42,代码来源:5-1.c

示例3: startAccount

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;
}
开发者ID:bigolu,项目名称:rutgers-spring-2016,代码行数:42,代码来源:BankServer.c

示例4: sem_open_anon

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;
}
开发者ID:chrspeich,项目名称:kommunikation-netze,代码行数:20,代码来源:helper.c

示例5: main

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;
}
开发者ID:yfchenggithub,项目名称:UNP,代码行数:20,代码来源:sem_create.c

示例6: main

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);
}
开发者ID:possientis,项目名称:Prog,代码行数:20,代码来源:psem_getvalue.c

示例7: perror

/**
@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) ;
}
开发者ID:Arafatk,项目名称:trick,代码行数:25,代码来源:ITimer.cpp

示例8: s_alloc

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);
}
开发者ID:YuanFanBin,项目名称:learn,代码行数:20,代码来源:figure-15.35.c

示例9: main

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);
  }
}
开发者ID:mahaicheng,项目名称:network-program,代码行数:20,代码来源:client1.c

示例10: am_agent_instance_init_init

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;
}
开发者ID:jhoeflaken,项目名称:OpenAM,代码行数:20,代码来源:log.c

示例11: open_sample_semaphore

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;
}
开发者ID:loxodes,项目名称:SuperDARN_UHD_Server,代码行数:20,代码来源:usrp_driver.cpp

示例12: main

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;
	}
}
开发者ID:chathhorn,项目名称:posixtestsuite,代码行数:20,代码来源:1-1.c

示例13: IM_SHM_LOG

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;
}
开发者ID:mrtos,项目名称:route_base,代码行数:20,代码来源:shm.c

示例14: FskSemaphoreNew_

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;
}
开发者ID:Kazu-zamasu,项目名称:kinomajs,代码行数:41,代码来源:FskSynchronization.c

示例15: main

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;
	}
}
开发者ID:haiku,项目名称:open_posix_testsuite,代码行数:41,代码来源:2-1.c


注:本文中的sem_open函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。