linux信号量例子
semaphore.h 提供的是 POSIX 标准定义的 semaphore 接口,而 sys/sem.h 里 提供的是符合 System V 标准的 semaphore接口 (semget, semop, ...),这些接口都比较老了, linux提供主要是为了兼容老代码
/** @file main628.cpp
* @note System Technology Co., Ltd. All Right Reserved.
* @brief
* @author
* @date 2019-6-28
* @note v1.0.0 Created
* @history
* @warning
*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;
void *producer(void *arg)
{
;
) {
sem_wait(&blank_number);
queue[p] = rand() % + ;
printf("Produce %d\n", queue[p]);
sem_post(&product_number);
p = (p+)%NUM;
sleep(rand()%);
}
}
void *consumer(void *arg)
{
;
) {
sem_wait(&product_number);
printf("Consume %d\n", queue[c]);
queue[c] = ;
sem_post(&blank_number);
c = (c+)%NUM;
sleep(rand()%);
}
}
int main(int argc, char *argv[])
{
pthread_t pid, cid;
sem_init(&blank_number, , NUM);
sem_init(&product_number, , );
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
sem_destroy(&blank_number);
sem_destroy(&product_number);
;
}
/*!
* Email: xmain@gmail.com
* Auth: c
* Date: 2019-6-30
* File: semaphoreTest.cpp
* Class: %{Cpp:License:ClassName} (if applicable)
* Brief:
* Note:
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t bin_sem;
void *thread_function1(void *arg)
{
printf("thread_function1--------------sem_wait\n");
sem_wait(&bin_sem);
printf("sem_wait\n");
)
{
printf("thread_function1\n");
}
}
void *thread_function2(void *arg)
{
printf("thread_function2--------------sem_post\n");
sem_post(&bin_sem);
printf("sem_post\n");
)
{
printf("thread_function2\n");
}
}
int main()
{
int res;
pthread_t a_thread, a_thread2;
void *thread_result;
res = sem_init(&bin_sem, , );
)
{
perror("Semaphore initialization failed");
}
printf("sem_init\n");
res = pthread_create(&a_thread, NULL, thread_function1, NULL);
)
{
perror("Thread creation failure");
}
//printf("thread_function1\n");
sleep ();
printf("sleep\n");
res = pthread_create(&a_thread2, NULL, thread_function2, NULL);
)
{
perror("Thread creation failure");
}
)
{
printf("main\n");
}
}
#include <iostream>
#include<stdio.h>
#include <pthread.h>
#include<stdlib.h>
#include <string.h>
#include <semaphore.h>
using namespace std;
//用户从终端输入任意字符然后统计个数显示,输入end则结束
//使用多线程实现:主线程获取用户输入并判断是否退出,子线程计数
]={};
;
sem_t sem;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void*arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程激活的时候,子线程被激活后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
printf("func before 1th wait\n");
sem_wait(&sem);
printf("func after 1th wait\n");
)
{
printf("长度为:%d.\n",strlen(buf));
memset(buf, , sizeof(buf));
printf("func before 2th wait\n");
sem_wait(&sem);
printf("func after 2th wait\n");
}
pthread_exit(NULL);
}
int main(void)
{
;
pthread_t th;
sem_init(&sem,,);
ret=pthread_create(&th,NULL,func,NULL);
)
{
printf("pthread_create error.\n");
;
}
printf("输入一个字符串,以回车结束.\n");
while(scanf("%s",buf))
{
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
))
{
printf("输入的字符串为:%s",buf);
flag=;
printf("main before 1th wait\n");
sem_post(&sem);
printf("main after 1th wait\n");
break;
}
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号激活子线程来计数。
// 子线程被阻塞,主线程可以激活,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
printf("main before 2th wait\n");
sem_post(&sem);
printf("main after 2th wait\n");
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
)
{
printf("pthread_join error.\n");
exit(-);
}
printf("子线程回收成功\n");
sem_destroy(&sem);
;
}
linux信号量例子的更多相关文章
- [转]一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程
一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程 希望此文能给初学多线程编程的朋友带来帮助,也希望牛人多多指出错误. 另外感谢以下链接的作者给予,给我的学习带来了很大帮助 http ...
- Linux信号量详解
1.什么是信号量信号量是一种特殊的变量,访问具有原子性.只允许对它进行两个操作:1)等待信号量当信号量值为0时,程序等待:当信号量值大于0时,信号量减1,程序继续运行.2)发送信号量将信号量值加1. ...
- Java中处理Linux信号量
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5976361. ...
- ossfs工具将OSS挂载到阿里云linux系统目录例子
ossfs 是基于 aliyun OSS 的 fuse 客户端了,所以我们把它用在阿里云上肯定是没有错了,下面来看一篇关于ossfs工具将OSS挂载到阿里云linux系统目录例子,具体的细节如下文介绍 ...
- Linux信号量同步共享内存实验.
Linux信号量同步共享内存实验. Linux信号量同步共享内存实验. 简述 程序流程 信号量和共享内存的系统函数 信号量系统函数及接口 共享内存系统函数及接口 写程序 读程序 简述 本文主要内容是自 ...
- linux信号量(转载)
本文转载自http://blog.csdn.net/qinxiongxu/article/details/7830537 信号量一.什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个时刻只 ...
- 最全面的linux信号量解析
信号量 一.什么是信号量 信号量的使用主要是用来保护共享资源,使得资源在一个时刻只有一个进程(线程) 所拥有. 信号量的值为正的时候,说明它空闲.所测试的线程可以锁定而使用它.若为0,说明 它被占用, ...
- linux信号量之进程间同步
概念 linux信号量: 允许多个线程同时进入临界区,可以用于进程间的同步. 和互斥锁(mutex)的区别: 互斥锁只允许一个线程进入临界区. 所在头文件: semaphore.h 主要函数 初始化函 ...
- Linux 信号量同步编程
前一篇文章概述了Linux 系统中信号量互斥编程,这篇文章正好是前一篇的姊妹篇----信号量同步.说它们是姊妹篇是因为它们都是利用了内核的信号量机制实现了进程间的通信.因为两者所解决的问题不同,因此它 ...
随机推荐
- BZOJ 2502 清理雪道(有源汇上下界最小流)
题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...
- Zookeeper中的watcher监听和leader选举机制
watcher监听 什么是watcher接口 同一个事件类型在不同的通知状态中代表的含义有所不同,下图列举了常见的通知状态和事件类型. Watcher通知状态与事件类型一览 上图列举了ZooKeepe ...
- Kylin 架构模块简介
Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop/Spark之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由eBay Inc. 开发并贡献至开源社区.它能 ...
- [hdu contest 2019-07-29] Azshara's deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法
今天hdu的比赛的第一题,凸包+区间dp. 给出n个点m个圆,n<400,m<100,要求找出凸包然后给凸包上的点连线,连线的两个点不能(在凸包上)相邻,连线不能与圆相交或相切,连线不能相 ...
- template里面要做数据渲染,但是数据还没有出来
<el-dialog title="企业详情" :visible.sync="showEditPayment" @close="closeDia ...
- 浅析HTTP/2的多路复用
HTTP/2有三大特性:头部压缩.Server Push.多路复用.前两个特性意思比较明确,也好理解,唯有多路复用不太好理解,尤其是和HTTP1.1进行对比的时候,这个问题我想了很长时间,也对比了很长 ...
- HugeGraph入门
一.HugeGraph简介 最近在搞好友推荐方便的工作,选择了图数据的方法,使用并学习了HugeGraph,再次记录一下. HugeGraph是百度在2018年中旬开源的一款图数据库(Graph Da ...
- Mybatis传参- 被逗号分割的字符串
String ids = "1,2,3,4,5,6",如ids作为参数传递,查询list返回.mybatis用foreach处理并返回. SELECT * FROM yp_popu ...
- python 输出‘\xe8\xb4\x9d\xe8\xb4\x9d’, ‘\xe6\x99\xb6\xe6\x99\xb6’, ‘\xe6\xac\xa2\xe6\xac\xa2’]
如上代码块,结果输出为: [‘\xe8\xb4\x9d\xe8\xb4\x9d’, ‘\xe6\x99\xb6\xe6\x99\xb6’, ‘\xe6\xac\xa2\xe6\xac\xa2’] 北京 ...
- Win10安装PyQt5与Qt Designer【转】
https://blog.csdn.net/u011342224/article/details/78879633 1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 ...