Linux多线程--使用信号量同步线程【转】
- int sem_init(sem_t *sem, int pshared, unsigned int value);
- int sem_wait(sem_t *sem);
- int sem_post(sem_t *sem);
- int sem_destroy(sem_t *sem);
- #include <unistd.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- //线程函数
- void *thread_func(void *msg);
- sem_t sem;//信号量
- #define MSG_SIZE 512
- int main()
- {
- int res = -1;
- pthread_t thread;
- void *thread_result = NULL;
- char msg[MSG_SIZE];
- //初始化信号量,其初值为0
- res = sem_init(&sem, 0, 0);
- if(res == -1)
- {
- perror("semaphore intitialization failed\n");
- exit(EXIT_FAILURE);
- }
- //创建线程,并把msg作为线程函数的参数
- res = pthread_create(&thread, NULL, thread_func, msg);
- if(res != 0)
- {
- perror("pthread_create failed\n");
- exit(EXIT_FAILURE);
- }
- //输入信息,以输入end结束,由于fgets会把回车(\n)也读入,所以判断时就变成了“end\n”
- printf("Input some text. Enter 'end'to finish...\n");
- while(strcmp("end\n", msg) != 0)
- {
- fgets(msg, MSG_SIZE, stdin);
- //把信号量加1
- sem_post(&sem);
- }
- printf("Waiting for thread to finish...\n");
- //等待子线程结束
- res = pthread_join(thread, &thread_result);
- if(res != 0)
- {
- perror("pthread_join failed\n");
- exit(EXIT_FAILURE);
- }
- printf("Thread joined\n");
- //清理信号量
- sem_destroy(&sem);
- exit(EXIT_SUCCESS);
- }
- void* thread_func(void *msg)
- {
- //把信号量减1
- sem_wait(&sem);
- char *ptr = msg;
- while(strcmp("end\n", msg) != 0)
- {
- int i = 0;
- //把小写字母变成大写
- for(; ptr[i] != '\0'; ++i)
- {
- if(ptr[i] >= 'a' && ptr[i] <= 'z')
- {
- ptr[i] -= 'a' - 'A';
- }
- }
- printf("You input %d characters\n", i-1);
- printf("To Uppercase: %s\n", ptr);
- //把信号量减1
- sem_wait(&sem);
- }
- //退出线程
- pthread_exit(NULL);
- }

- printf("Input some text. Enter 'end'to finish...\n");
- while(strcmp("end\n", msg) != 0)
- {
- if(strncmp("TEST", msg, 4) == 0)
- {
- strcpy(msg, "copy_data\n");
- sem_post(&sem);
- }
- fgets(msg, MSG_SIZE, stdin);
- //把信号量加1
- sem_post(&sem);
- }

- #include <unistd.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- //线程函数
- void *thread_func(void *msg);
- sem_t sem;//信号量
- sem_t sem_add;//增加的信号量
- #define MSG_SIZE 512
- int main()
- {
- int res = -1;
- pthread_t thread;
- void *thread_result = NULL;
- char msg[MSG_SIZE];
- //初始化信号量,初始值为0
- res = sem_init(&sem, 0, 0);
- if(res == -1)
- {
- perror("semaphore intitialization failed\n");
- exit(EXIT_FAILURE);
- }
- //初始化信号量,初始值为1
- res = sem_init(&sem_add, 0, 1);
- if(res == -1)
- {
- perror("semaphore intitialization failed\n");
- exit(EXIT_FAILURE);
- }
- //创建线程,并把msg作为线程函数的参数
- res = pthread_create(&thread, NULL, thread_func, msg);
- if(res != 0)
- {
- perror("pthread_create failed\n");
- exit(EXIT_FAILURE);
- }
- //输入信息,以输入end结束,由于fgets会把回车(\n)也读入,所以判断时就变成了“end\n”
- printf("Input some text. Enter 'end'to finish...\n");
- sem_wait(&sem_add);
- while(strcmp("end\n", msg) != 0)
- {
- if(strncmp("TEST", msg, 4) == 0)
- {
- strcpy(msg, "copy_data\n");
- sem_post(&sem);
- //把sem_add的值减1,即等待子线程处理完成
- sem_wait(&sem_add);
- }
- fgets(msg, MSG_SIZE, stdin);
- //把信号量加1
- sem_post(&sem);
- //把sem_add的值减1,即等待子线程处理完成
- sem_wait(&sem_add);
- }
- printf("Waiting for thread to finish...\n");
- //等待子线程结束
- res = pthread_join(thread, &thread_result);
- if(res != 0)
- {
- perror("pthread_join failed\n");
- exit(EXIT_FAILURE);
- }
- printf("Thread joined\n");
- //清理信号量
- sem_destroy(&sem);
- sem_destroy(&sem_add);
- exit(EXIT_SUCCESS);
- }
- void* thread_func(void *msg)
- {
- char *ptr = msg;
- //把信号量减1
- sem_wait(&sem);
- while(strcmp("end\n", msg) != 0)
- {
- int i = 0;
- //把小写字母变成大写
- for(; ptr[i] != '\0'; ++i)
- {
- if(ptr[i] >= 'a' && ptr[i] <= 'z')
- {
- ptr[i] -= 'a' - 'A';
- }
- }
- printf("You input %d characters\n", i-1);
- printf("To Uppercase: %s\n", ptr);
- //把信号量加1,表明子线程处理完成
- sem_post(&sem_add);
- //把信号量减1
- sem_wait(&sem);
- }
- sem_post(&sem_add);
- //退出线程
- pthread_exit(NULL);
- }
其运行结果如下:

Linux多线程--使用信号量同步线程【转】的更多相关文章
- 【2017-06-20】Linux应用开发工程师C/C++面试问题记录之一:Linux多线程程序的同步问题
参考之一:Linux 线程同步的三种方法 链接地址:http://www.cnblogs.com/eleclsc/p/5838790.html 简要回答: Linux下线程同步最常用的三种方法就是互斥 ...
- Linux多线程编程-信号量
在Linux中.信号量API有两组.一组是多进程编程中的System V IPC信号量.另外一组是我们要讨论的POSIX信号量. 这两组接口类似,但不保证互换.POSIX信号量函数都已sem_开头,并 ...
- Linux 多线程条件变量同步
条件变量是线程同步的另一种方式,实际上,条件变量是信号量的底层实现,这也就意味着,使用条件变量可以拥有更大的自由度,同时也就需要更加小心的进行同步操作.条件变量使用的条件本身是需要使用互斥量进行保护的 ...
- Linux多线程实践(9) --简单线程池的设计与实现
线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收.所以 ...
- linux 共享内存 信号量 同步
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信——使用信号.下面 ...
- 多线程&定时器Timer&同步&线程通信&ThreadLocal
1.多线程 线程状态分为:新建状态.就绪状态.运行状态.阻塞状态.死亡状态 对象等待池的阻塞状态:运行状态执行了wait方法 对向锁池的阻塞状态:试图获得某个同步锁,已经被其他线程占用,就会放到对象的 ...
- linux多线程学习笔记五--线程安全【转】
转自:http://blog.csdn.net/kkxgx/article/details/7506085 版权声明:本文为博主原创文章,未经博主允许不得转载. 一,线程安全基础 一个函数被称为线程安 ...
- Linux 多线程环境下 进程线程终止函数小结(转)
pthread_kill: pthread_kill与kill有区别,是向线程发送signal.,大部分signal的默认动作是终止进程的运行,所以,我们才要用signal()去抓信号并加上处理函数. ...
- Linux多线程——使用互斥量同步线程
前文再续,书接上一回,在上一篇文章: Linux多线程——使用信号量同步线程中,我们留下了一个如何使用互斥量来进行线程同步的问题,本文将会给出互斥量的详细解说,并用一个互斥量解决上一篇文章中,要使用两 ...
随机推荐
- sql查询两条记录的时间差
今天突然想到了一个需求,即在一张带有id和time字段的表中,查询相邻时间的时间差. 表的记录如下: 表名为wangxin id是一个不重复的字符串,time是一个时间戳. 现在的需求如下: 比如id ...
- sublime 使用总结
不管你用什么编辑,sublime是首选编辑器,就是sublime淘汰,但已成为标准.例如:atom,几乎等同于sublime,及其他可以几乎调成到sublime操作方式. 一.常用插件 插件搜索地址: ...
- MySQL多线程备份工具:mydumper
MySQL多线程备份工具:mydumper http://www.orczhou.com/index.php/2011/12/how-to-split-mysqldump-file/ Mydumper ...
- 用lua扩展你的Nginx(整理)
首先得声明.这不是我的原创,是在网上搜索到的一篇文章,原著是谁也搞不清楚了.按风格应该是属于章亦春的文章. 整理花了不少时间,所以就暂写成原创吧. 一. 概述 Nginx是一个高性能.支持高并发的,轻 ...
- Java序列化总结
什么是序列化? 序列化是将对象的状态信息转化为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后可以通过存储区中读取或反序列化对象的状态重新创建对象. 为什么要序 ...
- 机器学习理论基础学习3.4--- Linear classification 线性分类之Gaussian Discriminant Analysis高斯判别模型
一.什么是高斯判别模型? 二.怎么求解参数?
- 软件包管理:rpm命令管理-校验和文件提取
校验主要用于判断文件是否做了更改 修改标志: 会用-V,会看输出结果即可. 当有误操作,比如删了某一个文件,只需知道他属于哪一个rpm包,可用提取找回覆盖就行.并不把整个rpm包安装,而是提取其中的某 ...
- Javascript-逻辑或(||)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- liunx anacoda 安装pyltp
anacoda 默认的gcc是4.7需要更新 https://anaconda.org/nlesc/gcc 更新之后再安装即可. 报错: /usr/lib64/libstdc++.so.6: vers ...
- 教你玩转产品管理系统iClap(基础功能篇)
距iClap这款宇宙级产品的推出已经有一段时间了,相信不少小伙伴们都已经开始使用上了,多好用多方便,就不用说了,可不想违反广告法呢!不过还是有用户反映说某些功能不太了解,或者还有一些不清楚的操作方式, ...