精华内容
下载资源
问答
  • 但是在libc库中,函数sem_open、sem_close、sem_unlink只有声明,并未实现。 可以从一下代码中找到证据。 代码路径: bionic/libc/include/semaphore.h /* * Copyright (C) 2008 The Android Open Source Project ...

    在android下使用信号量时,需要链接libc库。

    但是在libc库中,函数sem_open、sem_close、sem_unlink只有声明,并未实现。

    可以从一下代码中找到证据。

    代码路径:
    bionic/libc/include/semaphore.h

    /*
     * Copyright (C) 2008 The Android Open Source Project
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *  * Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *  * Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in
     *    the documentation and/or other materials provided with the
     *    distribution.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     * SUCH DAMAGE.
     */
    
    #ifndef _SEMAPHORE_H
    #define _SEMAPHORE_H
    
    #include <sys/cdefs.h>
    
    __BEGIN_DECLS
    
    struct timespec;
    
    typedef struct {
      unsigned int count;
    #ifdef __LP64__
      int __reserved[3];
    #endif
    } sem_t;
    
    #define SEM_FAILED __BIONIC_CAST(reinterpret_cast, sem_t*, 0)
    
    int sem_destroy(sem_t* __sem);
    int sem_getvalue(sem_t* __sem, int* __value);
    int sem_init(sem_t* __sem, int __shared, unsigned int __value);
    int sem_post(sem_t* __sem);
    int sem_timedwait(sem_t* __sem, const struct timespec* __ts);
    /*
     * POSIX only supports using sem_timedwait() with CLOCK_REALTIME, however that is typically
     * inappropriate, since that clock can change dramatically, causing the timeout to either
     * expire earlier or much later than intended.  This function is added to use a timespec based
     * on CLOCK_MONOTONIC that does not suffer from this issue.
     */
    int sem_timedwait_monotonic_np(sem_t* __sem, const struct timespec* __ts) __INTRODUCED_IN(28);
    int sem_trywait(sem_t* __sem);
    int sem_wait(sem_t* __sem);
    
    /* These aren't actually implemented. */
    sem_t* sem_open(const char* __name, int _flags, ...);
    int sem_close(sem_t* __sem);
    int sem_unlink(const char* __name);
    
    __END_DECLS
    
    #endif
    
    

    后记:
    安卓中通信这块和linux还是有很大不同的。坑很多。。。。

    展开全文
  • 关于sem_unlink什么时候删除信号量

    千次阅读 2015-07-14 09:55:44
    sem_unlink在man手册里有这么一段话: sem_unlink() removes the named semaphore referred to by name. The semaphore name is removed immediately. The semaphore isdestroyed once all other processes that ...

    sem_unlink在man手册里有这么一段话:

    sem_unlink() removes the named semaphore referred to by name.  
    The semaphore name
    is removed immediately. The semaphore is
    destroyed once all other processes that have the semaphore open close it.

    这段话的意思是说,sem_unlink会马上删除指定的信号量名,但要等到所有打开该信号量的进程关闭该信号量后才删除该信号。详细地说,当进程创建一个有名信号量,会在/dev/shm下生成一个sem.xxx的文件,所有打开该信号量的进程(包括创建它的进程)都会增加该文件的引用计数,并且这个计数由内核管理。当调用sem_unlink时,/dev/shm下的sem.xxx文件会马上被删除,但是信号量本身并没有被删除,所有已打开该信号量的进程仍能正常使用它。直到所有打开该信号量的进程关闭该信号量后,内核才真正删除信号量。

      因此,信号量跟sem.xxx并不是共存亡的。下面来测试下:

    复制代码
    /* sem_create.cpp */
    #include <iostream>
    #include <fcntl.h>           /* For O_* constants */
    #include <sys/stat.h>        /* For mode constants */
    #include <semaphore.h>
    #include <unistd.h>
    #include <cerrno>
    #include <cstring>
    
    int main()
    {
        sem_t *psem = sem_open( "/sem_test",O_CREAT,S_IRWXU,1 );
        if ( SEM_FAILED == psem )
        {
            std::cout << "sem_open fail:" << strerror(errno) << std::endl;
            return 0;
        }
        
        int sval = 0;
        sem_getvalue( psem,&sval );
        
        std::cout << "sleep now ....value is " << sval << std::endl;
        sleep( 30 );
        
        sem_getvalue( psem,&sval );
        std::cout << "wake up ....value is " << sval << std::endl;
        
        sem_post( psem );
        sem_getvalue( psem,&sval );
        std::cout << "raise value and try again ....value is " << sval << std::endl;
        
        sem_close( psem );
    }
    复制代码
    复制代码
    /**
     *
     * sem_destruct.cpp
     */
    
    #include <iostream>
    #include <fcntl.h>           /* For O_* constants */
    #include <sys/stat.h>        /* For mode constants */
    #include <semaphore.h>
    #include <cerrno>
    #include <cstring>
    
    int main()
    {
        sem_t *psem = sem_open( "/sem_test",O_CREAT,S_IRWXU,1 );
        if ( SEM_FAILED == psem )
        {
            std::cout << "sem_open fail:" << strerror(errno) << std::endl;
            return 0;
        }
        
        std::cout << "decrease value and remove sem now..." << std::endl;
        sem_wait ( psem );
        
        sem_close( psem );
        sem_unlink( "/sem_test" );
        
        std::cout << "sem should remove from /dev/shm,but not really remove" << std::endl;
    }
    复制代码

    分别编译上面的文件:

    g++ -pthread -o sem_create sem_create.cpp
    g++ -pthread -o sem_destruct sem_destruct.cpp

    先运行sem_create,再运行sem_destruct这时个可以看到输出为:

    复制代码
    xzc@xzc-HP-ProBook-4446s:~/code/sem$ ./sem_create
    sleep now ....value is 1
    xzc@xzc-HP-ProBook-4446s:~/code/sem$ ./sem_destruct
    decrease value and remove sem now...
    sem should remove from /dev/shm,but not really remove
    wake up ....value is 0
    raise value and try again ....value is 1
    复制代码

    可以看到,即使sem_destruct从/dev/shm把信号量删除了,ls /dev/shm也确认对应的文件删除了。但是经过sleep的sem_create进程在wake up后仍能对信号量进行操作。

      可以看到,有名信号量一旦打开后,跟名字并没有多大关系。下面再来测试有名信号量调用sem_unlink但仍有进程在使用,这时另外一个进程又要创建同名信号量的情况

    复制代码
    /**
      *
      * sem_again.cpp
      */
    
    #include <iostream>
    #include <fcntl.h>           /* For O_* constants */
    #include <sys/stat.h>        /* For mode constants */
    #include <semaphore.h>
    #include <unistd.h>
    #include <cerrno>
    #include <cstring>
    
    int main()
    {
        sem_t *psem = sem_open( "/sem_test",O_CREAT|O_EXCL,S_IRWXU,1 );
        if ( SEM_FAILED == psem )
        {
            std::cout << "sem_open fail:" << strerror(errno) << std::endl;
            return 0;
        }
        
        int sval = 0;
        sem_getvalue( psem,&sval );
        
        //std::cout << "sleep now ....value is " << sval << std::endl;
        //sleep( 30 );
        
        sem_getvalue( psem,&sval );
        std::cout << "value is " << sval << std::endl;
        
        sem_post( psem );
        sem_getvalue( psem,&sval );
        std::cout << "raise value and try again ....value is " << sval << std::endl;
        
        sem_close( psem );
        sem_unlink( "/sem_test" );
    }
    复制代码

    编译一下:g++ -pthread -o sem_again sem_again.cpp,然后依次运行sem_create、sem_destruct、sem_again

    复制代码
    xzc@xzc-HP-ProBook-4446s:~/code/sem$ ./sem_create
    sleep now ....value is 1
    
    xzc@xzc-HP-ProBook-4446s:~/code/sem$ ./sem_destruct
    decrease value and remove sem now...
    sem should remove from /dev/shm,but not really remove
    
    xzc@xzc-HP-ProBook-4446s:~/code/sem$ ./sem_again
    value is 1
    raise value and try again ....value is 2
    
    wake up ....value is 0
    raise value and try again ....value is 1
    复制代码

    在sem_again中特意使用了O_CREATE|O_EXCL标志,如果已存在同名信号量,则创建失败。但从上面的结果来看,创建成功并且互不干扰。也就是说,一个有名信号量,在打开并调用sem_unlink后可以当作无名信号量来使用(但这时新的进程还想使用该信号量,不可能了)。

      注:上面只做了简单测试,并未测试sem_unlink后各进程的竞争情况,在生产环境请自行测试。

    PS:在debian 6上,不用unistd.h也能调用sleep函数,在ubuntu 14.04上不行。在debian 6上是链接的库是-lrt,在ubuntu 14.04上是-pthread,-lrt不行。linux这是要闹哪样。

    转自:http://www.cnblogs.com/coding-my-life/p/4215862.html?utm_source=tuicool

    展开全文
  • sem_unlink的行为不合文档说明

    千次阅读 2014-01-05 00:17:41
    If one or more processes have the semaphore open when sem_unlink() is called, destruction of the semaphore is postponed until  all references to the semaphore have been destroyed by calls to sem_
    If one or more processes have the semaphore open when sem_unlink() is called, destruction of the semaphore is postponed until 
    


    all references to the semaphore have been destroyed by calls to sem_close(), _exit(), or exec.


    如果一个或多个进程打开了该信号量,当调用sem_unlink后,要所有的打开该信号量的应用都关闭(调用sem_close,_exit或exec)该信号量


    后,该信号量才能得到删除。(如果程序异常退出呢???)


    实际上并不是,只要一调用sem_unlink,该信号量就被删了!!!


    Calls to sem_open() to recreate or reconnect to the semaphore refer to a  new  semaphore after sem_unlink() is called.

    如果使用sem_open再次创建或链接改信号量,对应的将是一个新的信号量在调用sem_unlink后


    如果在使用sem_wait等待信号,这是使用sem_close或sem_unlink,依然会阻塞在sem_wait处,无法退出


    如果在使用sem_wait等待信号,这时使用sem_unlink删除信号量,然后再新建一个同名信号量,使用sem_post发送信号,sem_wait是不会有响应的

    展开全文
  • sem_open、sem_close、sem_unlink

    万次阅读 2016-08-09 21:51:24
    sem_t *sem_open(const char *name, int oflag,..,/*mode_t mode,unsigned int value*/) :打开有名信号量 1.当打开一个一定存在的有名信号量时,ofalg设置为0. int sem_close(sem_t *sem):关闭有名信号量 2.一个进程...

    UNP2 P180

    sem_t  *sem_open(const char *name, int oflag,..,/*mode_t mode,unsigned int value*/) :打开有名信号量

    1.当打开一个一定存在的有名信号量时,ofalg设置为0.

    int sem_close(sem_t *sem):关闭有名信号量

    2.一个进程终止时,内核对其上仍打开的所有有名信号量自动执行关闭操作。不论进程是自愿还是非自愿终止。

    3.关闭一个信号量并没有将他从系统中删除。POSIX 有名信号量是随内核持续的:即使当前没有进程打开着某个信号量,他的值仍保持。

    int sem_unlink(const char *name):从系统中删除有名信号量

    4.每个信号量有一个引用计数器记录打开的次数,当引用计数大于0时,name 就能从文件系统中删除,然而其信号量的析构(不同于将他的名字从文件系统中删除)却要等到最后一个sem_close发生时为止.

    sem_t *sem;
    	int val;
    	if((sem=sem_open(SEM_NAME,OFLAG,MODE,SEM_VALUE))==SEM_FAILED)
    	{
    		perror("sem_open failed");
    		exit(EXIT_FAILURE);
    	}
    
    	if(-1==sem_close(sem))
    	{
    		perror("sem_close failed");
    		exit(EXIT_FAILURE);
    	}
    	if(-1==sem_getvalue(sem,&val))  //sem_close() 后sem_getvalue() 错误:segmentation fault
    	{
    		perror("sem_getvalue failed");
    		exit(EXIT_FAILURE);
    	}
    	sem_t *sem;
    	int val;
    	if((sem=sem_open(SEM_NAME,OFLAG,MODE,SEM_VALUE))==SEM_FAILED)
    	{
    		perror("sem_open failed");
    		exit(EXIT_FAILURE);
    	}
    	if(-1==sem_unlink(SEM_NAME))
    		{
    		perror("sem_unlink failed");
    		exit(EXIT_FAILURE);
    	}
    	
    	if(-1==sem_getvalue(sem,&val)) //sem_unlink() 后sem_getvalue()可以获取到信号量的值(此时,name删除,信号量并未析构(上述第4))
    	{
    		perror("sem_getvalue failed");
    		exit(EXIT_FAILURE);
    	}
    	if(-1==sem_close(sem))
    	{
    		perror("sem_close failed");
    		exit(EXIT_FAILURE);
    	}
    
    
    



    展开全文
  •  函数sem_open创建一个新的有名信号灯或打开一个已存在的有名信号灯。有名信号灯总是既可用于线程间的同步,又能用于进程间的同步。 1. sem_open 名称:: sem_open 功能: 创建并初始化有名信号灯 头文件: #...
  • sem_init sem_post sem_destroy信号量介绍

    千次阅读 2011-08-25 14:28:33
    信号量的数据类型为结构sem_t,它本质上是一个长整型的数。函数sem_init()用来初始化一个信号量。它的原型为:  extern int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value
  • shm_unlink()函数

    千次阅读 2014-11-08 12:44:13
    shm_unlink 功能: 删除一个共享内存区 头文件: #include  函数原形: int shm_unlink(const char *name); 参数: name 共享内存区的名字 返回值: 成功返回0,出错返回-1    shm_unlink函数...
  • NAME semaphore.h - semaphores (REALTIME) ...[SEM]#include <semaphore.h> DESCRIPTION The<semaphore.h>header shall define thesem_ttype, used in performing semaphore o...
  • 有名信号量sem_open和内存信号量sem_init创建信号量的区别  分类: C/C++ sem_t *sem sem_open(const char *name, int oflag, .../*mode_t mode,unsinged int value) ; int sem_init(sem_t *sem,int ...
  • sem_close(), _exit(),exec 关闭 int sem_unlink(const char *name); 锁定一个信号量 #include //sem_wait()锁定 sem 引用的一个信号量,对该信号进行锁定操作 //如 信号量 = 0,调用进程将不会返回直到锁定了这个...
  • 没有 sem_ 的手册页条目 有时候需要去查找一个函数的定义时。 man sem,却得到了 没有 sem 的手册页条目,此时需要安装一些编程开发文档。 sudo apt-get install manpages-posix-dev 安装成功,打开终端 man sem_ ...
  • Linux信号量sem_t

    2019-09-03 13:36:52
    #include<semaphore.h> 信号量的数据类型为结构sem_t...int sem_init (sem_t *sem, int pshared, unsigned int value); pshared不为0时, 此信号量在进程间进享, 否则只能为当前进程所有线程共享, value给出...
  • sem_open中文件名参数使用说明

    千次阅读 2014-12-09 17:11:01
    #include /* For O_* constants */ #include /* For mode constants */ #include sem_t *sem_open(const char *name, int oflag);...sem_t *sem_open(const char *name, int oflag,mode_t mode, unsigned int val
  • Linux信号量 sem_t简介

    2017-03-15 23:22:51
    函数介绍 ...它的原型为:int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value));sem为指向信号量结构的一个指针;pshared不为0时此信号量在进程间共享,否则只能为当前进程的所有线
  • sem_overview(7)

    2011-10-30 16:22:00
    SEM_OVERVIEW(7) 2009-02-25 NAME sem_overview —— POSIX信号量概述 DESCRIPTION POSIX信号量允许进程和线程同步它们的动作。 一个信号量是一个整数,它的值永远不会降到0以下。仅能在信号量上执行两个操作:将...
  • #include /* For O_* constants */ #include /* For mode constants */ #include #include #include ...#define SEM_NAME "MY_SEM_NAME" bool bExitThread = false; sem_t *g_sem; pthread_t g
  • 信号量 信号量的概念 信号量广泛用于进程或线程间的同步和互斥, 信号量本质上是一个非负的整数计数器, 它被用来控制... PV原语是对信号量的操作,一次P操作使信号量 sem 减1,一次V 操作使信号量 sem 加1...
  • 在OS X下,sem_init失败

    千次阅读 2013-01-23 14:55:26
    在开发Iphone上的项目时,原本运行的好好的程序,移植到Iphone上就死锁了,最后发现是创建信号量失败。 sem_init返回-1 ..." declares sem_init so that it compiles properly on OS X, but it returns -1 w
  • 对‘sem_init’未定义的引用

    千次阅读 2019-04-23 19:18:02
    编译出错: 对‘sem_init’未定义的引用 原因: 一般出错的原因是没有链接库导致的 解决方法: 在编译最后添加 -lpthread 例如: gcc -o test test.c -lpthread ...
  • 原因是:Mac OS X不支持创建无名的信号量,只能使用sem_open创建有名的信号量。 " declares sem_init so that it compiles properly on OS X, but it returns -1 with errno set to ENOSYS (function not ...
  • 文章目录编程环境:信号量(信号灯):使用步骤:“生产者-消费者”例子:理论模型:代码分析:代码实现:运行结果:Mac 下对 sem_init()/sem_destory() 不支持:下载地址: 简 述: 上一篇中讲解了“条件变量 + ...
  • 版权声明:本文为博主原创文章,未经博主允许不得转载。 一、sem_open函数name参数的构造 Linux的有名信号量的创建是通过调用sem_open函数创建的,函数原型...我刚开始用以为是想一般的指定一个路径名给sem_op...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,031
精华内容 1,212
关键字:

sem_unlink