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 线程的同步 三 (内存信号量的使用)的更多相关文章
- Linux并发与同步专题 (3) 信号量
关键词:Semaphore.down()/up(). <Linux并发与同步专题 (1)原子操作和内存屏障> <Linux并发与同步专题 (2)spinlock> <Li ...
- linux 线程的同步 一 (互斥量和信号量)
互斥量(Mutex) 互斥量表现互斥现象的数据结构,也被当作二元信号灯.一个互斥基本上是一个多任务敏感的二元信号,它能用作同步多任务的行为,它常用作保护从中断来的临界段代码并且在共享同步使用的资源. ...
- linux线程间同步方式总结梳理
线程间一般无需特别的手段进行通信,由于线程间能够共享数据结构,也就是一个全局变量能够被两个线程同时使用.只是要注意的是线程间须要做好同步! 使用多线程的理由: 1. 一个是和进程相比,它是一种非常&q ...
- Linux线程间同步的几种方式
信号量 信号量强调的是线程(或进程)间的同步:"信号量用在多线程多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程再进行某些动作(大家都在sem_wait的时候,就阻塞 ...
- 线程间同步之 semaphore(信号量)
原文地址:http://www.cnblogs.com/yuqilin/archive/2011/10/16/2214429.html semaphore 可用于进程间同步也可用于同一个进程间的线程同 ...
- linux线程(二)内存释放
linux线程有两种模式joinable和unjoinable. joinable线程:系统会保存线程资源(栈.ID.退出状态等)直到线程退出并且被其他线程join. unjoinable线程:系统会 ...
- linux线程间同步方式汇总
抽空做了下linux所有线程间同步方式的汇总(原生的),包含以下几个: 1, mutex 2, condition variable 3, reader-writer lock 4, spin loc ...
- linux 线程的同步 二 (互斥锁和条件变量)
互斥锁和条件变量 为了允许在线程或进程之间共享数据,同步时必须的,互斥锁和条件变量是同步的基本组成部分. 1.互斥锁 互斥锁是用来保护临界区资源,实际上保护的是临界区中被操纵的数据,互斥锁通常用于保护 ...
- linux线程间同步(1)读写锁
读写锁比mutex有更高的适用性,能够多个线程同一时候占用读模式的读写锁.可是仅仅能一个线程占用写模式的读写锁. 1. 当读写锁是写加锁状态时,在这个锁被解锁之前,全部试图对这个锁加锁的线程都会被堵塞 ...
随机推荐
- 【javascript】数据结构-队列
<!DOCTYPE html> <html> <head> <title>queue</title> <meta charset=&q ...
- 什么是OPTEE-OS
1. 为什么会出现这种技术? 为了安全,例如:保护指纹虹膜的生物特征数据 2. 为了确保数据安全各家公司都做了些什么? Arm公司提出的了trustzone技术,用一根安全总线(称为NS位)来判断当前 ...
- 【Java----字符串转义与反转义】
apache工具包common-lang中有一个很有用的处理字符串的工具类,其中之一就是StringEscapeUtils,这个工具类是在2.3版本以上加上的去的,利用它能很方便的进行html,xml ...
- 用 Rprof 进行性能分析
R 提供了内置函数 Rprof( ) 对代码的性能进行分析.在分析过程中,会有一个抽样程序,并且是和后续代码一起运行的,直到分析结束.默认情况下,抽样程序基本上每隔20 毫秒就会记录一下当前 R 在运 ...
- DB.使用Oracle时的遇到的问题
1.(20190225)ojdbc14.jar 来自“E:\ZC_DB\_Connector\Oracle\10g\Oracle Database 10g Release 2 (10.2.0.4) J ...
- 递归--练习8--noi1788Pell数列
递归--练习8--noi1788Pell数列 一.心得 5 1. 6 //直接递归Time Limit Exceeded 7 //那就记忆化递归 8 2. 9 直接记忆化递归后还有问题 10 a[k] ...
- RabbitMQ 消息传递的可靠性
生产者保证消息可靠投递 消费者保证消息可靠消费 RabbitMQ持久化 参考:https://blog.csdn.net/RobertoHuang/article/details/79605185
- BeginInit与EndInit的实践总结
在项目中,遇到这种情况,总结随便如下: 初始化时:添加操作,BeginInit{flag=true} 警情是一条条加入的,全部都加入后,图表再一次性生成 EndInit{flag=false} ...
- python基础教程笔记 第1单元 && 第2单元
1.http://docs.python.org/dev/3.0/whatsnew/3.0.htmlpython-announce-listpython-help2.交互式编译器3.非整数除法 .1. ...
- iOS笔记之线程
dispatch_after dispatch_after能让我们添加进队列的任务延时执行,比如想让一个Block在10秒后执行: var time = dispatch_time(DISPATCH_ ...