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 系统中信号量互斥编程,这篇文章正好是前一篇的姊妹篇----信号量同步.说它们是姊妹篇是因为它们都是利用了内核的信号量机制实现了进程间的通信.因为两者所解决的问题不同,因此它 ...
随机推荐
- RegularExpression正则表达式
1.必須為大寫字母,小寫字母,數字和符號的至少其中3種組合 The new password should contain any three of different syntax which is ...
- CF316G3 Good Substrings 广义后缀自动机
太累了,刷刷水~ code: #include <bits/stdc++.h> #define N 500005 #define LL long long #define setIO(s) ...
- learning java AWT MenuBar Menu MenuItem菜单
import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java ...
- HTTP文件上传
看到网上很多链接文件(word.pdf...)可以下载,想制作http下载链接. 其实是将某文件直接放在服务器上搭建的网站上某目录下即可,例如:http://xxx:port/UpgradePack/ ...
- leetcode解题报告(32):Teemo Attacking
描述 In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poison ...
- 《挑战30天C++入门极限》入门教程:实例详解C++友元
入门教程:实例详解C++友元 在说明什么是友元之前,我们先说明一下为什么需要友元与友元的缺点: 通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为pu ...
- SpringBoot获取Freemarker模板引擎,生成HTML代码
今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...
- Android 开发常用工具合集
在 Android 开发中经常使用到的小功能,用于记录开发的那些事^_^ 1. 获取 release 和 debug 版本的 SHA1 public static String getSHA1(Con ...
- laravel 存储base64格式图片
laravel 存储base64格式图片 一.总结 一句话总结: 用正则替换base64图片编码的编码头即可 存储图片的话,用laravel可以用Storage的put方法,原生php可以用file_ ...
- Java-Maven(九):Maven 项目pom文件引入工程根目录下lib文件夹下的jar包
由于项目一些特殊需求,pom依赖的包可能是非Maven Repository下的包文件,因此无法自己从网上下载.此时,我们团队git上对该jar使用. Maven项目pom引入lib下jar包 在ec ...