sem_getvalue函数例程

#include <stdint.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
 
sem_t sem;
 
void *testfunc1(void *arg)
{
    while(1)
    {
        sem_wait(&sem);
    }
}
 
 void *testfunc2(void *arg)
{
    while(1)
    {
        sem_wait(&sem);
    }
}
void *testfunc3(void *arg)
{
    while(1)
    {
        sem_wait(&sem);
    }
}

void *testfunc4(void *arg)
{
    while(1)
    {
        sem_wait(&sem);
    }
}

void *testfunc5(void *arg)
{
    while(1)
    {
        sem_wait(&sem);
    }
}

int main()
{
    int  value = 0;
    pthread_t ps1;
    pthread_t ps2;
    pthread_t ps3;
    pthread_t ps4;
    pthread_t ps5;
    sem_init(&sem, 0, 0);
    pthread_create(&ps1,NULL,testfunc1,NULL);
    pthread_create(&ps2,NULL,testfunc2,NULL);
    pthread_create(&ps3,NULL,testfunc3,NULL);
    pthread_create(&ps3,NULL,testfunc4,NULL);
    pthread_create(&ps4,NULL,testfunc5,NULL);
    sleep(3);
    sem_getvalue(&sem, &value); //Ubuntu6.04  
    printf("value = %d\n",value); //0
    
    pthread_join(ps1,NULL);
    pthread_join(ps2,NULL);
    pthread_join(ps3,NULL);
    pthread_join(ps4,NULL);
    pthread_join(ps5,NULL);
    return 0;
}


/*
sem_getvalue:

获取信号量的值,不知道什么系统下,会输出负数。。

*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值