【操作系统】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);
}
相关推荐
THGML1 天前
排序算法解析
数据结构·算法·排序算法
周杰伦_Jay1 天前
【计算机网络核心】TCP/IP模型与网页解析全流程详解
网络·网络协议·tcp/ip·计算机网络·算法·架构·1024程序员节
额呃呃1 天前
对信号的理解
linux·运维·算法
OKkankan1 天前
模板的进阶
开发语言·数据结构·c++·算法
weixin_307779131 天前
Linux 下 Docker 与 ClickHouse 的安装配置及 MySQL 数据同步指南
linux·数据库·mysql·clickhouse·运维开发
qq_297075671 天前
vmware和kali linux安装和搭建
linux·安全测试
zhaotiannuo_19981 天前
【Linux kali 更换yum源】
linux·运维·服务器
会灭火的程序员1 天前
银河麒麟V10 SP3 升级GCC环境
linux·c++·supermap
RTC老炮1 天前
webrtc弱网-PccBitrateController类源码分析与算法原理
网络·算法·webrtc
草莓熊Lotso1 天前
模板进阶:从非类型参数到分离编译,吃透 C++ 泛型编程的核心逻辑
linux·服务器·开发语言·c++·人工智能·笔记·后端