linux线程信号量 函数,linux C线程信号量(转)

#include

#include /* For O_CREAT constants */

#include /* For sem_ constants */

#include /* For pthread_create constants */

void pthreadRun(void *arg);

void pthreadStop(void *arg);

sem_t* semStop;

sem_t* semRun;

int main()

{

pthread_t pID1, pID2;

int nRetP1, nRetP2;

printf("\n\n");

// 创建信号量

semStop = sem_open("semStop", /*O_RDWR |*/ O_CREAT, 0644, 0); //

semRun = sem_open("semRun,", /*O_RDWR |*/ O_CREAT, 0644,0); //

if(SEM_FAILED == semStop || SEM_FAILED == semRun)

{

printf("Opened error\n");

return 1;

}

// 初始化信号量

sem_init(semStop, 0, 0);

// int sem_init(sem_t *sem, int pshared, unsigned int value);

// If pshared has the value 0, then the semaphore is shared between the threads of a process

// The value argument specifies the initial value for the semaphore.

sem_init(semRun, 0, 0);

// 创建 信号量同步的 线程

printf("now create two threads for synchronization. \n");

nRetP2 = pthread_create(&pID2, NULL, (void*) pthreadStop, NULL);

usleep(2*1000);

nRetP1 = pthread_create(&pID1, NULL, (void*) pthreadRun, NULL);

if(nRetP1 != 0) {

printf("nRetP1 pthread_create failed! \n");

return 2;

}

if(nRetP2 != 0) {

printf("nRetP2 pthread_create failed! \n");

return 2;

}

// 关闭信号量

sem_close(semStop);

sem_close(semRun);

// 暂停 60 秒, 观看两线程同步

usleep(60*1000);

printf("\n\n");

return 0;

}

void pthreadRun(void *arg)

{

while(1)

{

// 等待开车

printf("wait for semRun\n");

sem_wait(semRun);

// 正常行车

printf("runing\n");

// 停车

printf("post semStop\n");

sem_post(semStop);

}

}

void pthreadStop(void *arg)

{

while(1)

{

// 上乘客

printf("get on the bus.\n");

// 关车门,通知司机开车

printf("post semRun.\n");

sem_post(semRun);

// 卖车票

printf("Selling tickets.\n");

// 等待停车

printf("wait for semStop\n");

sem_wait(semStop);

// 下乘客

printf("get off\n");

}

}

/* 编译:(gcc 编译选项需要加入一个多线程选项: -pthread)

hangken@ubuntu:~$ gcc -o bus/bus -g bus/bus.c -pthread

*/

/* 运行结果:

hangken@ubuntu:~$ sudo ./bus/bus

now create two threads for synchronization.

get on the bus.

post semRun.

Selling tickets.

wait for semStop

wait for semRun

runing

post semStop

Segmentation fault

hangken@ubuntu:~$

*/

/* 主要用到的信号量函数有:

sem_init:初始化信号量sem_t,初始化的时候可以指定信号量的初始值,以及是否可以在多进程间共享。

sem_wait:一直阻塞等待直到信号量>0。

sem_timedwait:阻塞等待若干时间直到信号量>0。 //等待信号量变成>0,

// int sem_wait_i( sem_t *psem, int mswait ) mswait为等待时间,若mswait<0则无穷等待,否则等待若干mswait毫秒

sem_post:使信号量加1。

sem_destroy:释放信号量。和sem_init对应。

关于各函数的具体参数请用man查看。如man sem_init可查看该函数的帮助。

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值