• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ sem_open函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: main

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);
}
开发者ID:WuXibin,项目名称:practiceCode,代码行数:41,代码来源:shm_client.c


示例17: Thread_create_sem

/**
 * 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;
}
开发者ID:Adamwuming,项目名称:paho.mqtt.c,代码行数:45,代码来源:Thread.c


示例18: mfilter_init

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;
}
开发者ID:uplusware,项目名称:erisemail,代码行数:21,代码来源:antijunk.cpp


示例19: main

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;
}
开发者ID:AlexShiLucky,项目名称:demo,代码行数:21,代码来源:semgetvalue.c


示例20: BuildName

// 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;
    }
}
开发者ID:KimJeongYeon,项目名称:jack2_android,代码行数:22,代码来源:JackPosixSemaphore.cpp



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ sem_p函数代码示例发布时间:2022-05-30
下一篇:
C++ sem_init函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap