信号量的使用

使用两个信号量的实例

#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);
}

作者:ljianhui
来源:CSDN
原文:https://blog.csdn.net/ljianhui/article/details/10813469
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值