【操作系统】Linux之线程同步二(头歌作业)

第1关:信号量

cpp 复制代码
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>

//全局信号量  sem1已被初始化为1,sem2被初始化为0
extern sem_t sem1, sem2;

//全局共享变量
extern char *ch;

/************************
 * 参数arg: 是线程函数的参数
*************************/
void *ThreadHandler1(void *arg)
{
	int i = 0;
	for(i = 0; i < 3; i++)
	{
		/********** BEGIN **********/
		sem_wait(&sem1);

		
		/********** END **********/
		printf("%c", *ch);
		usleep(100);
		ch++;
		
		/********** BEGIN **********/
		sem_post(&sem2);
		
		/********** END **********/
	}
	pthread_exit(NULL);
}
/************************
 * 参数arg: 是线程函数的参数
*************************/
void *ThreadHandler2(void *arg)
{
	int i = 0;
	for(i = 0; i < 3; i++)
	{
		/********** BEGIN **********/
		sem_wait(&sem2);

		/********** END **********/
		printf("%c", *ch);
		ch++;
		
		/********** BEGIN **********/
		sem_post(&sem1);
		
		/********** END **********/
	}
	
	pthread_exit(NULL);
}

第2关:读写锁

cpp 复制代码
#include <stdio.h>
#include <pthread.h>

//全局读写锁
extern pthread_rwlock_t rwlock;

//全局共享变量
extern char buffer[3];
extern int position;

/************************
 * 参数arg: 是线程函数的参数
*************************/
void *ReadHandler(void *arg)
{
	int i;
	for(i = 0; i < 3; i++)
	{
		/********** BEGIN **********/
		 pthread_rwlock_rdlock(&rwlock);

		/********** END **********/
		printf("%c\n", buffer[i]);
		

		/********** BEGIN **********/
		pthread_rwlock_unlock(&rwlock);
		
		/********** END **********/
		
		usleep(800);
	}
	pthread_exit(NULL);
}

/************************
 * 参数arg: 是线程函数的参数
*************************/
void *WriteHandler(void *arg)
{
	/********** BEGIN **********/
	pthread_rwlock_wrlock(&rwlock);

	/********** END **********/

	buffer[position] = *(char*)arg;
	sleep(1);
	position++;
	
	/********** BEGIN **********/
	pthread_rwlock_unlock(&rwlock);
	
	/********** END **********/

	pthread_exit(NULL);
}

第3关:项目实战

cpp 复制代码
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

//记录读线程的个数
extern int reader;

//全局的信号量变量
extern sem_t sem_read, sem_write;

//读写锁初始化函数
void sem_rwlock_init()
{
	reader = 0;
	//初始化信号量个1
	sem_init(&sem_read, 0, 1);
	sem_init(&sem_write, 0, 1);
}

//读写锁注销函数
void sem_rwlock_destroy()
{
	sem_destroy(&sem_read);
	sem_destroy(&sem_write);
}

//读模式下的加锁操作
void sem_rwlock_rdlock()
{
	//读模式下加锁操作
	/********** BEGIN **********/
	sem_wait(&sem_read);
    if (reader == 0)
    {
        sem_wait(&sem_write); // 第一个读者要去读取数据,禁止写者进行写数据操作
    }
    reader++;
    sem_post(&sem_read);
	
	/********** END **********/
}

//读模式下的解锁操作
void sem_rwlock_unrdlock()
{
	//读模式下解锁操作
	/********** BEGIN **********/
	sem_wait(&sem_read);
    reader--;
    if (reader == 0)
    {
        sem_post(&sem_write); // 当读者数量为0时,允许写者进行写数据
    }
    sem_post(&sem_read);
	
	/********** END **********/
}

//写模式下的加锁操作
void sem_rwlock_wrlock()
{
	sem_wait(&sem_write);
}

//写模式下的解锁操作
void sem_rwlock_unwrlock()
{
	sem_post(&sem_write);
}
相关推荐
挺菜的8 分钟前
【算法刷题记录(简单题)002】字符串字符匹配(java代码实现)
java·开发语言·算法
开开心心就好17 分钟前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
m0_653031361 小时前
腾讯云认证考试报名 - TDSQL数据库交付运维专家(TCCE PostgreSQL版)
运维·数据库·腾讯云
IC 见路不走2 小时前
LeetCode 第91题:解码方法
linux·运维·服务器
没有名字的小羊2 小时前
8.Docker镜像讲解
运维·docker·容器·tomcat
翻滚吧键盘2 小时前
查看linux中steam游戏的兼容性
linux·运维·游戏
小能喵2 小时前
Kali Linux Wifi 伪造热点
linux·安全·kali·kali linux
Code季风2 小时前
深入理解微服务中的服务注册与发现(Consul)
java·运维·微服务·zookeeper·架构·go·consul
java1234_小锋2 小时前
解释一下NGINX的反向代理和正向代理的区别?
运维·nginx
汀沿河2 小时前
8.1 prefix Tunning与Prompt Tunning模型微调方法
linux·运维·服务器·人工智能