20230403作业(互斥锁、信号量、条件变量)

作业1:信号量的方式,实现打印一次,倒置一次

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

char buf[]="1234567";
sem_t sem1,sem2;
void* Output(void* arg)  	//分支线程1,循环打印
{
	while(1)
	{
		if(sem_wait(&sem1)<0)
		{
			perror("sem_wait");
			break;
		}
		printf("%s\n",buf);		
		if(sem_post(&sem2)<0)
		{
			perror("sem_post(&sem)");
			break;
		}
	}
}

void* Inversion(void* arg)  //分支线程2,字符串逆置
{
	while(1)
	{
		if(sem_wait(&sem2)<0)
		{
			perror("sem_wait");
			break;
		}
		int i=0,j=strlen(buf)-1;
		while(i<j)
		{
			char t=buf[i];
			buf[i]=buf[j];
			buf[j]=t;
			i++;j--;
		}
		if(sem_post(&sem1)<0)
		{
			perror("sem_post");
			break;
		}
	}
	return NULL;
}


int main(int argc, const char *argv[])
{
	//创建信号量
	if(sem_init(&sem1,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}
	if(sem_init(&sem2,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}

	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,Output,NULL)!=0) //创建分支线程1
	{
		printf("line=%d\n",__LINE__);
		printf("pthread_create failed!!!\n");
		return -1;
	}


	if(pthread_create(&tid2,NULL,Inversion,NULL)!=0)  //创建分支线程2
	{
		printf("line=%d\n",__LINE__);
		printf("pthread_create failed!!!\n");
		return -1;
	}

	pthread_join(tid2,NULL);
	//销毁信号量
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	printf("over\n");
	return 0;
}

作业2:文件中的数据打印到终端,类似cat功能

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//初始化互斥锁
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;//初始化条件变量

ssize_t res=0;
char str[50]="";
int ss;
int flag=0;

void* callback1(void* arg)
{
	while(1)
	{
		/********临界区**********/
		pthread_mutex_lock(&mutex); 	//互斥锁

		//不是当前线程要访问的时机,让当前线程休眠
		if(0!=flag)
		{
			pthread_cond_wait(&cond,&mutex);
		}
		bzero(str,sizeof(str));
		res=read(ss,str,sizeof(str)); //读文件内容
		if(0==res)
		{
			break;
		}
		flag=1; 	//修改运行时机,设置为非0;
		//通过制定条件变量唤醒指定线程
		pthread_cond_signal(&cond); 	
		pthread_mutex_unlock(&mutex); 	//解锁
		/*********临界区***********/
	}
	pthread_exit(NULL);
}

void* callback2(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(1!=flag)
		{
			pthread_cond_wait(&cond,&mutex);
		}
		for(int i=0;i<res;i++)
		{
			printf("%c",str[i]);
		}
		flag=0;
		pthread_cond_signal(&cond);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	ss=open("./01.c",O_RDONLY);
	if(ss<0)
	{
		perror("read");
		return -1;
	}
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
	{
		printf("pthread_create failed line=%d\n",__LINE__);
		return -1;
	}

	if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
	{
		printf("pthread_create failed line=%d\n",__LINE__);
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_mutex_destroy(&mutex);//销毁互斥锁
	pthread_cond_destroy(&cond);//销毁条件变量
	return 0;
}

作业3:三个线程,循环打印自己的id号,顺序为abc

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3=PTHREAD_COND_INITIALIZER;

int flag=0;

void* tid_a(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);

		if(0!=flag)
		{
			pthread_cond_wait(&cond1,&mutex);
		}
		printf("a\n");
		flag=1;

		pthread_cond_signal(&cond2);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* tid_b(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);

		if(1!=flag)
		{
			pthread_cond_wait(&cond2,&mutex);
		}
		printf("b\n");
		flag=2;

		pthread_cond_signal(&cond3);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}
void* tid_c(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&mutex);

		if(2!=flag)
		{
			pthread_cond_wait(&cond3,&mutex);
		}
		printf("c\n");
		flag=0;

		pthread_cond_signal(&cond1);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}




int main(int argc, const char *argv[])
{
	pthread_t a,b,c;
	if(pthread_create(&a,NULL,tid_a,NULL)!=0)
	{
		printf("pthread_create failed ,line=%d\n",__LINE__);
		return -1;
	}
	if(pthread_create(&b,NULL,tid_b,NULL)!=0)
	{
		printf("pthread_create failed ,line=%d\n",__LINE__);
		return -1;
	}
	if(pthread_create(&c,NULL,tid_c,NULL)!=0)
	{
		printf("pthread_create failed ,line=%d\n",__LINE__);
		return -1;
	}

	pthread_join(a,NULL);
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	pthread_cond_destroy(&cond3);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
跨进程条件变量互斥锁的实现稍微复杂一些,需要使用操作系统提供的特定机制来实现进程间的通信和同步。以下是一种可能的实现方式: 1. 使用共享内存:创建一个共享内存区域,用于存储条件变量互斥锁的状态信息。这个共享内存区域可以被多个进程共享访问。 2. 使用信号量:为了确保多个进程之间能够正确地访问共享内存区域,可以使用信号量来实现互斥访问。例如,可以使用一个二进制信号量来控制对共享内存的互斥访问。 3. 实现条件变量:在共享内存中创建一个条件变量结构体,它包含一个条件变量的状态和等待队列。当一个进程需要等待某个条件时,它可以将自己加入条件变量的等待队列中,并释放互斥锁。当条件满足时,其他进程可以通过发送信号唤醒等待队列中的进程。 4. 实现互斥锁:在共享内存中创建一个互斥锁结构体,它包含一个锁状态和当前持有锁的进程标识符。当一个进程需要获取互斥锁时,它可以检查锁状态并将自己的进程标识符设置为持有锁的进程标识符。如果锁已经被其他进程持有,则该进程需要等待。 通过这种方式,多个进程可以通过共享内存和信号量来实现条件变量互斥锁的跨进程访问和同步。但是需要注意的是,这种实现方式需要考虑并发访问的竞争条件和死锁等问题,并使用合适的同步机制来解决。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值