`
realnicky
  • 浏览: 60336 次
  • 来自: 杭州
社区版块
存档分类
最新评论

关于信号量sem_wait的整理(转)

 
阅读更多

SYNOPSIS
       #include <semaphore.h>

       int sem_init(sem_t *sem, int pshared, unsigned int value);
//初始化信号量
       int sem_wait(sem_t * sem);
//等待信号,获取拥有权
       int sem_trywait(sem_t * sem);

       int sem_post(sem_t * sem);
//发出信号即释放拥有权
       int sem_getvalue(sem_t * sem, int * sval);

       int sem_destroy(sem_t * sem);
//注销信号量,在linux中其本质是没有任何作用的,它不做任何事情。
DESCRIPTION
       This manual page documents POSIX 1003.1b semaphores, not to be confused with SystemV semaphores as described in ipc(5),
       semctl(2) and semop(2).

       Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the
       counter atomically, and wait until the counter is non-null and decrement it atomically.
//信号量是在多线程环境中共享资源的计数器。对信号量的基本操作无非有三个:对信号量的增加;然后阻塞线程等待,直到信号量不为空才返回;然后就是对信号量的减少。
// 在编程中,信号量最常用的方式就是一个线程A使用sem_wait阻塞,因为此时信号量计数为0,直到另外一个线程B发出信号post后,信号量计数加 1,此时,线程A得到了信号,信号量的计数为1不为空,所以就从sem_wait返回了,然后信号量的计数又减1变为零。
       sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set initially to
       value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to
       be shared between several processes ( pshared is not zero). LinuxThreads currently does not support process-shared
       semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.
//在使用信号量之前,我们必须初始化信号。第三个参数通常设置为零,初始化信号的计数为0,这样第一次使用sem_wait的时候会因为信号计数为0而等待,直到在其他地方信号量post了才返回。除非你明白你在干什么,否则不要将第三个参数设置为大于0的数。
//第二个参数是用在进程之间的数据共享标志,如果仅仅使用在当前进程中,设置为0。如果要在多个进程之间使用该信号,设置为非零。但是在Linux线程中,暂时还不支持进程之间的信号共享,所以第二个参数说了半天等于白说,必须设置为0.否则将返回ENOSYS错误。
       sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically
       decreases the semaphore count.
//当信号的计数为零的时候,sem_wait将休眠挂起当前调用线程,直到信号量计数不为零。在sem_wait返回后信号量计数将自动减1.

       sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is
       atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately
       returns with error EAGAIN.
//sem_trywait是一个立即返回函数,不会因为任何事情阻塞。根据其返回值得到不同的信息。如果返回值为0,说明信号量在该函数调用之前大于0,但是调用之后会被该函数自动减1.至于调用之后是否为零则不得而知了。如果返回值为EAGAIN说明信号量计数为0。
       sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be
       used in asynchronous signal handlers.
//解除信号量等待限制。让信号量计数加1.该函数会立即返回不等待。
       sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.
//获得当前信号量计数的值。
       sem_destroy destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the
       semaphore at the time sem_destroy is called. In the LinuxThreads implementation, no resources are associated with
       semaphore objects, thus sem_destroy actually does nothing except checking that no thread is waiting on the semaphore.
// 销毁信号量对象,释放信号量内部资源。然而在linux的线程中,其实是没有任何资源关联到信号量对象需要释放的,因此在linux中,销毁信号量对象的作用仅仅是测试是否有线程因为该信号量在等待。如果函数返回0说明没有,正常注销信号量,如果返回EBUSY,说明还有线程正在等待该信号量的信号。

CANCELLATION
       sem_wait is a cancellation point.

ASYNC-SIGNAL SAFETY
       On processors supporting atomic compare-and-swap (Intel 486, Pentium and later, Alpha, PowerPC, MIPS II, Motorola 68k),
       the sem_post function is async-signal safe and can therefore be called from signal handlers. This is the only thread syn-
       chronization function provided by POSIX threads that is async-signal safe.

       On the Intel 386 and the Sparc, the current LinuxThreads implementation of sem_post is not async-signal safe by lack of
       the required atomic operations.
//现在sem_post被POSIX所规范,当它改变信号量计数器值的时候是线程安全的。

RETURN VALUE
       The sem_wait and sem_getvalue functions always return 0. All other semaphore functions return 0 on success and -1 on
       error, in addition to writing an error code in errno.
//通常返回值往往都为0,表示成功,如果有误将返回-1,并且将错误的代码号赋值给errno。

ERRORS
       The sem_init function sets errno to the following codes on error:
//sem_init失败时,常见错误有:
              EINVAL value exceeds the maximal counter value SEM_VALUE_MAX
   //第三个参数value值超过了系统能够承受的最大值SEM_VALUE_MAX.

              ENOSYS pshared is not zero
   //你将第二参数设置为了非零,如果是linux系统,请将第二个参数设置为零

       The sem_trywait function sets errno to the following error code on error:

              EAGAIN the semaphore count is currently 0

       The sem_post function sets errno to the following error code on error:

              ERANGE after incrementation, the semaphore value would exceed SEM_VALUE_MAX (the semaphore count is left unchanged
                     in this case)
   //信号量的计数超过了系统能够承受的最大值SEM_VALUE_MAX。

       The sem_destroy function sets errno to the following error code on error:

              EBUSY some threads are currently blocked waiting on the semaphore.
   //某些线程正在使用该信号量等待。

其实线程临界区可以使用信号量来实现,将信号量的信号初始化为1,然后在临界区使用完毕后再置信号量为1我们就可以轻松实现mutex了。

具体实现,自己慢慢琢磨一下吧。

分享到:
评论

相关推荐

    linux信号量机制

    只有当信号量值大于0时,才能使用公共资源,使用后,函数sem_wait()减少信号量。函数sem_trywait()和函数pthread_ mutex_trylock()起同样的作用,它是函数sem_wait()的非阻塞版本。它们都在头文件/usr/...

    linux中进程问题

    //定义信号量 sem_t applefull; sem_t orangefull; void *procf(void *arg) //father线程 { while(1){ sem_wait(&empty;); //P操作 printf("%s\n",(char *)arg); sem_post(&applefull;); //V操作 Sleep(7)...

    用信号量解决生产者-消费者问题.zip

    在 Ubuntu 下编写程序,用信号量解决生产者-消费者...pc.c 中将会用到 sem_open(),sem_unlink(),sem_wait(),sem_post() 等信号量相关的系统调用,详细介绍参考;https://blog.csdn.net/newlw/article/details/122684192

    基于 C语言 的信号量实现与应用【100010138】

    在 Ubuntu 下编写程序,用信号量解决生产者-消费者问题 在 Ubuntu 下...缓冲区同时最多只能保存 10 个数 pc.c 中将会用到 sem_open(),sem_unlink(),sem_wait(),sem_post() 等信号量相关的系统调用,请查阅相关文档。

    Simple-Windows-Posix-Semaphore:一个简单的窗口 POSIX 信号量库

    如果信号量的值当前为零,则sem_wait操作将阻塞,直到该值变得大于零。 semaphore.c和semaphore.h文件可替代 Windows XP 或更高版本上的 POSIX 信号量。 提供的sem_函数与它们的 POSIX 等价函数的行为不同,但它们...

    LwIP+freertos工程(LwIP2.1.2最新版本)

    if(xSemaphoreTake(*sem, wait_tick) == pdTRUE) return ((xTaskGetTickCount()-start_tick)*portTICK_RATE_MS); else return SYS_ARCH_TIMEOUT; } void sys_sem_signal(sys_sem_t *sem) { if(xSemaphoreGive...

    Linux下的pv操作

     b)等待信号量 sem_wait  c)释放信号量 sem_pos  d)删除信号量 sem_destroy  (2)编写pv操作函数  之前在编写pv操作的时候,没有考虑到消息处理的时序问题,所以在某些极端的情况下可能会造成一些...

    PV操作 文档

    只有将 sem_flg 指定为 SEM_UNDO 标志后,semadj (所指定信号量针对调用进程的调整值)才会更新。 此外,如果此操作指定SEM_UNDO,系统更新过程中会撤消此信号灯的计数(semadj)。此操作可以随时进行---它永远不会...

    linux下的生产者消费者问题

    // 同步信号量, 当满了时阻止生产者放产品 sem_t full_sem; // 同步信号量, 当没产品时阻止消费者消费 pthread_mutex_t mutex; // 互斥信号量, 一次只有一个线程访问缓冲 int product_id = 0; //生产者id int ...

    java线程源码-Dinning-Philosopher-Problem:用线程和信号量概念的Dinning哲学家问题解决方案Java源代码

    信号量是整数变量,用于通过使用两个原子操作(用于过程同步的wait和signal)来解决关键部分问题。 等待和信号的定义如下: Wait-等待操作减小其参数s的值(如果为正)。 如果s为负或零,则不执行任何操作。 sem_...

    操作系统实验报告.docx

    《操作系统实验》报告 实验四:利用信号量实现进程控制 指导教师: ________ 班级: _________ 学号: _______ 姓名: _________ 操作系统实验报告全文共4页,当前为第1页。 操作系统实验报告全文共4页,当前为第1页...

    UNIX网络编程 卷2:进程间通信

     10.16 使用System V信号量实现Posix信号量 218  10.17 小结 224  习题 225  第11章 System V 信号量 226  11.1 概述 226  11.2 semget函数 227  11.3 semop函数 229  11.4 semctl函数 231  11.5 简单的...

    UNIX网络编程 卷2 进程间通信 带完整书签,完整目录

    10.16 使用System V信号量实现Posix信号量 218 10.17 小结 224 习题 225 第11章 System V信号量 226 11.1 概述 226 11.2 semget函数 227 11.3 semop函数 229 11.4 semctl函数 231 11.5 简单的程序 232 ...

    lwip轻量级协议栈源码,已移植成功

     LwIP中需要使用信号量进行通信,所以在sys_arch中应实现相应的信号量结构体 struct sys_semt和处理函数sys_sem_new() 、sys_sem_free() 、sys_sem_signal ( ) 和sys_arch_sem_wait ( ) 。由于μC/OS已经实现了信号...

    《UNIX网络编程 第2版. 第2卷, 进程间通信(中文版)》(W·Richard Stevens[美] 著)

    10.16 使用System V信号量实现Posix信号量 218 10.17 小结 224 习题 225 第11章 System V 信号量 226 11.1 概述 226 11.2 semget函数 227 11.3 semop函数 229 11.4 semctl函数 231 11.5 简单的程序 232 11.6 文件上...

    UNIX网络编程 第2卷 进程间通信

    10.16 使用System V信号量实现Posix信号量 218 10.17 小结 224 习题 225 第11章 System V 信号量 226 11.1 概述 226 11.2 semget函数 227 11.3 semop函数 229 11.4 semctl函数 231 11.5 简单的程序 232 11.6 文件上...

    嵌入式实验(消息队列)

    负责系统启动时同步系统中其他Task的启动同步,利用信号量的semFlush()完成。同时接收各*/ /*Task的告警信息,告警信息需编号以logmsg方式输出。本task负责系统结束时的Task删除处理*/ int task1(void) { int ...

    计算机操作系统课程设计报告《生产者---消费者问题》.doc

    3.2.2 数据结构 producer_semaphore//生产者的资源信号量(初始值为缓冲区的大小) Buffer[pn] //有界缓冲区 Pn ///缓冲区目标位置 MAX_BUFFER//缓冲区上限 buffer_mutex//互斥信号量 Wait()//等待操作,用于申请...

    基于DOS的多任务系统的实现

    /*生产者消费者中的信号量*/ void InitDos(void) { union REGS regs; struct SREGS segregs; regs.h.ah = GET_INDOS; intdosx(&regs,&regs,&segregs); indos_ptr = MK_FP(segregs.es,regs.x.bx); if(_...

    嵌入式实时操作系统small RTOS51原理及应用

    第6章 任务之间的通信和同步之信号量 6.1 概述 6.2 使Keil C51函数具有重入性的特殊方法 6.3 数据结构 6.4 IN_OS_SEM_CHK宏及相关代码 6.5 初始化一个信号量 6.6 等待一个信号量 6.7 发送一个信号量 6.8 无等待地...

Global site tag (gtag.js) - Google Analytics