【操作系统】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);
}
相关推荐
写代码的【黑咖啡】15 小时前
Linux系统简介及常用命令分类详解
linux·运维·服务器
roman_日积跬步-终至千里15 小时前
【计算机算法与设计(10)】习题:苹果买卖问题——分治法的经典应用
算法
❀͜͡傀儡师15 小时前
docker一键部署夜莺监控
运维·docker·容器
deepdata_cn15 小时前
模型预测控制(MPC)算法
算法
0思必得015 小时前
[Web自动化] HTML列表标签
运维·python·自动化·html·web自动化
独自破碎E15 小时前
如何用最短替换让字符串变平衡?
java·开发语言·算法·leetcode
Jasmine_llq15 小时前
《P1082 [NOIP 2012 提高组] 同余方程》
算法·数学建模·质因数分解(试除法)·快速幂(模幂运算)·欧拉函数计算·基于质因数分解
YFLICKERH15 小时前
【Linux系统】ubuntu 25.04 虚拟机联网与DNS域名问题排查案例
linux·ubuntu25.04
松涛和鸣15 小时前
DAY27 Linux File IO and Standard IO Explained: From Concepts to Practice
linux·运维·服务器·c语言·嵌入式硬件·ubuntu
算家计算15 小时前
AI真的懂你!阿里发布Qwen3-Omni-Flash 全模态大模型:超强交互,人设任选
人工智能·算法·机器学习