day7 线程同步互斥机制

 

将一个文件中的数据打印到终端,类似cat一个文件,要求如下:

一个线程读取文件中的数据

另外一个线程打印文件中的数据

#include <stdio.h>	
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
char c[1] = "";

int flag = 1;
int s = 1;
//互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *Read(void *arg)
{
	while(1)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//当前不是访问时机,让线程休眠
		if(1 != flag)
		{
			//设置条件变量,同时解开互斥锁
			//休眠
			pthread_cond_wait(&cond,&mutex);
			//尝试上锁,如果上锁成功则唤醒成功
			//失败继续休眠
		}
		s = read(*(int *)arg,c,sizeof(c));
		if(s <= 0)
		{
			pthread_mutex_unlock(&mutex);
			pthread_cond_signal(&cond);
			break;
		}
		flag = 0;

		//通过制定条件唤醒线程
		pthread_cond_signal(&cond);
		//解锁
		pthread_mutex_unlock(&mutex);

	}
}

void *Print(void *arg)
{
	while(1)
	{
		//锁
		pthread_mutex_lock(&mutex);
		//当前不是访问时机,让线程休眠
		if(0 != flag)
		{
			//设置条件变量,同时解开互斥锁
			//休眠
			pthread_cond_wait(&cond,&mutex);
			//尝试上锁,如果上锁成功则唤醒成功
			//失败继续休眠
		}
		fprintf(stderr,c,sizeof(c));
		if(s == 0)
		{
			pthread_mutex_unlock(&mutex);
			break;
		}
		flag = 1;
		//通过指定条件唤醒指定程序
		pthread_cond_signal(&cond);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
}


int main(int argc, const char *argv[])
{
	int fd = open("./1.txt",O_RDONLY);
	if(0 > fd)
	{
		perror("open");
		return -1;
	}
	//创建一个分支线程
	pthread_t pid;
	if(pthread_create(&pid,NULL,Print,NULL) != 0)
	{
		return -1;
	}
	pthread_detach(pid);

	pthread_t pid1;
	
	if(pthread_create(&pid1,NULL,Read,&fd) != 0)
	{
		return -1;
	}

	pthread_join(pid1,NULL);
	//销毁互斥锁
	pthread_mutex_destroy(&mutex);
	//销毁条件变量
	pthread_cond_destroy(&cond);
	close(fd);

	return 0;
}

要求用信号量的方式实现,打印一次倒置一次。不允许使用flag。

提示:用多个信号量

#include <stdio.h>	
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>

char buf[] = "1234567";
sem_t sem1,sem2;

void *Reverse(void *arg)
{

	while(1)
	{
		//p
		if(sem_wait(&sem1) < 0)
		{
			perror("sem_wait");
			break;
		}
		int left = 0;
		int right = strlen(buf)-1;
		while(left < right)
		{
			buf[left] = buf[left] ^ buf[right];
			buf[right] = buf[left] ^ buf[right];
			buf[left] = buf[left] ^ buf[right];
			left++;
			right--;
		}
		//V
		if(sem_post(&sem2) < 0)
		{
			perror("sem_post");
			break;
		}

	}
	return NULL;
}

void *Print(void *arg)
{
	while(1)
	{	
		//p
		if(sem_wait(&sem2) < 0)
		{
			perror("sem_wait");
			break;
		}
		printf("%s\n",buf);

		//V
		if(sem_post(&sem1) < 0)
		{
			perror("sem_post");
			break;
		}

	}
}

int main(int argc, const char *argv[])
{	
	//创建信号量
	if(sem_init(&sem1,0,1) < 0)
	{
		perror("sem_init");
		return -1;
	}
	if(sem_init(&sem2,0,2) < 0)
	{
		perror("sem_init");
		return -1;
	}
	//创建一个分支线程
	pthread_t pid;
	if(pthread_create(&pid,NULL,Print,NULL) != 0)
	{
		return -1;
	}
	pthread_detach(pid);

	pthread_t pid1;
	//创建线程排序
	if(pthread_create(&pid1,NULL,Reverse,NULL) != 0)
	{
		return -1;
	}
	pthread_join(pid1,NULL);

	//销毁信号量
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	return 0; 
}

现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc

#include <stdio.h>	
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>

int flag = 0;
//互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;

void *Print1(void *arg)
{

 	while(1)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//当前不是访问时机,让线程休眠
		if(0 != flag)
		{
			//设置条件变量,同时解开互斥锁
			//休眠
			pthread_cond_wait(&cond,&mutex);
			//尝试上锁,如果上锁成功则唤醒成功
			//失败继续休眠
		}

		printf("1");

		flag = 1;

		//通过制定条件唤醒线程
		pthread_cond_signal(&cond1);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
}

void *Print2(void *arg)
{
	while(1)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//当前不是访问时机,让线程休眠
		if(1 != flag)
		{
			//设置条件变量,同时解开互斥锁
			//休眠
			pthread_cond_wait(&cond1,&mutex);
			//尝试上锁,如果上锁成功则唤醒成功
			//失败继续休眠
		}
		printf("2");

		flag = 2;

		//通过制定条件唤醒线程
		pthread_cond_signal(&cond2);
		//解锁
		pthread_mutex_unlock(&mutex);
	}
}


void *Print(void *arg)
{
	while(1)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//当前不是访问时机,让线程休眠
		if(2 != flag)
		{
			//设置条件变量,同时解开互斥锁
			//休眠
			pthread_cond_wait(&cond2,&mutex);
			//尝试上锁,如果上锁成功则唤醒成功
			//失败继续休眠
		}

		printf("3\n");

		flag = 0;

		//通过制定条件唤醒线程
		pthread_cond_signal(&cond);
		//解锁
		pthread_mutex_unlock(&mutex);

	}
}


int main(int argc, const char *argv[])
{

	//创建一个分支线程
	pthread_t pid;
	if(pthread_create(&pid,NULL,Print,NULL) != 0)
	{
		return -1;
	}
	pthread_detach(pid);

	pthread_t pid1;
	
	if(pthread_create(&pid1,NULL,Print1,NULL) != 0)
	{
		return -1;
	}
	pthread_detach(pid1);
	pthread_t pid2;
	
	if(pthread_create(&pid2,NULL,Print2,NULL) != 0)
	{
		return -1;
	}

	pthread_join(pid,NULL);
	pthread_join(pid1,NULL);
	pthread_join(pid2,NULL);
	//销毁互斥锁
	pthread_mutex_destroy(&mutex);
	//销毁条件变量
	pthread_cond_destroy(&cond);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值