同步互斥机制

1.要求用信号量的万式实现,打印一次倒置一次。不允许使用flag.
提示:用多个信号量
 

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

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

void *task1(void *arg){   //A  print buff

	while(1){
		//p
		sem_wait(&sem1);
		printf("%s\n",buff);
		//v
		sem_post(&sem2);
	}
	pthread_exit(NULL);
}

void *task2(void *arg){   //B  invert buff
	while(1){
		//p
		sem_wait(&sem2);
		for(int i=0;i<strlen(buff)/2;i++){
			char temp = buff[i];
			buff[i] = buff[strlen(buff)-1-i];
			buff[strlen(buff)-1-i] = temp;
		}
		//v
		sem_post(&sem1);
	}
	pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{
	sem_init(&sem1,0,1);	
	sem_init(&sem2,0,2);	
	//create pthread
	//A
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,NULL) != 0){
		printf("pthread_create failed!    __%d__\n",__LINE__);
		return -1;
	}
	//B
	if(pthread_create(&tid2,NULL,task2,NULL) != 0){
		printf("pthread_create failed!    __%d__\n",__LINE__);
		return -1;
	}
	//
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	//destroy semaphore
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	return 0;
}

2.将一个文件中的数据打印到终端,类似cat一个文件,要求如下:
a.一个线程读取文件中的数据
b.另外—个线程打印文件中的数据

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
pthread_mutex_t lock;
pthread_cond_t cond;
int fd;
char c;
off_t size;
int flag = 0;//为0读取,为1打印

void * task_read(void*arg){
	lseek(fd,0,SEEK_SET);
	for(int i =0;i < size;i++){
		pthread_mutex_lock(&lock);
		if(1 == flag){
			pthread_cond_wait(&cond,&lock);
		}
		if(read(fd,&c,1) < 0){
			perror("read");
			return NULL;
		}
		flag = 1;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&lock);
	}
	pthread_exit(NULL);
}

void * task_print(void*arg){
	for(int i =0;i < size;i++){
		pthread_mutex_lock(&lock);
		if(0 == flag){
			pthread_cond_wait(&cond,&lock);
		}
		printf("%c",c);
		flag = 0;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&lock);
	}
		pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{
	if(argc < 2){
		printf("input the file name\n");
		return -1;
	}
	//open a file
	if((fd = open(argv[1],O_RDONLY)) < 0){
		perror("open");
		return -1;
	}
	//文件大小
	size = lseek(fd,0,SEEK_END);
	//create lock and cond
	pthread_mutex_init(&lock,NULL);
	pthread_cond_init(&cond,NULL);

	//创建两个线程
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task_read,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid2,NULL,task_print,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	


	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	close(fd);
	pthread_mutex_destroy(&lock);
	pthread_cond_destroy(&cond);
	return 0;
}

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <pthread.h>
#include<unistd.h>
pthread_mutex_t lock;
pthread_cond_t cond1,cond2,cond3;
int flag = 0;

void* task1(void* arg){
	while(1){ 
		pthread_mutex_lock(&lock);
		if(flag != 0){
			pthread_cond_wait(&cond1,&lock);
		}
		printf("a");
		flag = 1;
		pthread_cond_signal(&cond2);
		pthread_mutex_unlock(&lock);
	}
	pthread_exit(NULL);
}

void* task2(void* arg){
	while(1){
		pthread_mutex_lock(&lock);
		if(flag != 1){
			pthread_cond_wait(&cond2,&lock);
		}
		printf("b");
		flag = 2;
		pthread_cond_signal(&cond3);
		pthread_mutex_unlock(&lock);
	}
	pthread_exit(NULL);
}

void* task3(void* arg){
	while(1){
		pthread_mutex_lock(&lock);
		if(flag != 2){
			pthread_cond_wait(&cond3,&lock);
		}
		printf("c");
		flag = 0;
		pthread_cond_signal(&cond1);
		pthread_mutex_unlock(&lock);
	}
	pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{
	pthread_mutex_init(&lock,NULL);
	pthread_cond_init(&cond1,NULL);
	pthread_cond_init(&cond2,NULL);
	pthread_cond_init(&cond3,NULL);

	pthread_t tid1,tid2,tid3;
	if(pthread_create(&tid1,NULL,task1,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid2,NULL,task2,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid3,NULL,task3,NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	pthread_mutex_destroy(&lock);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	pthread_cond_destroy(&cond3);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值