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多线程——使用信号量同步线程中,我们留下了一个如何使用互斥量来进行线程同步的问题,本文将会给出互斥量的详细解说,并用一个互斥量解决上一篇文章中,要使用两 ...
随机推荐
- TortoiseGit密钥的配置(转)
add by zhj:说到密钥,就不得不提非对称加密.目前使用最广泛的非对称加密算法是rsa,它是美国三位科学家于1977年发明的. 一对密钥对有两个密钥,其中一个为私钥,一个为公钥,两者没有什么区别 ...
- mysql5.6编译遇到错误
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)CMake Error at cmake/readline ...
- android 网络监测
public class NetWorkStateReceiver extends BroadcastReceiver { @Override public void onReceive(Contex ...
- 【剑指offer】斐波那契数列
一.题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 二.思路: 式子: n=0时,f=0:n=1或者n=2时f=1:否则f=f(n-1)+f(n ...
- 使用idea maven开发spring boot 分布式开发入门
1:使用idea创建一个maven工程 bos2 2:删除bos2的src 文件夹,向pom.xml文件 中添加版本号 <packaging>pom</packaging> & ...
- [LeetCode] 1. Two Sum_Easy tag: Hash Table
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- testNG入门详解
TestNG 的注释: @DataProvider @ExpectedExceptions @Factory @Test @Parameters <suite name="Parame ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- VUE路由去除#问题
最近自己在写一个vue的小型管理系统,在浏览器中看到的路由都是带有#的,很是不好看.为了解决此问题,大家一般都会想到:mode: 'history'.可是在开发阶段没有问题,但是一旦build打包后, ...
- GCC编译器ABI
ABI与EABI 1)ABI(Application Binary Interface for the ARM Architecture),描述了应用程序与cpu内核的低级接口. ABI允许编译好的目 ...