【操作系统】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);
}
相关推荐
tryCbest32 分钟前
CentOS部署Docker容器
linux·docker·centos
my rainy days2 小时前
C++:友元
开发语言·c++·算法
haoly19892 小时前
数据结构和算法篇-归并排序的两个视角-迭代和递归
数据结构·算法·归并排序
微笑尅乐2 小时前
中点为根——力扣108.讲有序数组转换为二叉搜索树
算法·leetcode·职场和发展
qyhua2 小时前
【Linux运维实战】彻底修复 CVE-2011-5094 漏洞
linux·运维·安全
坠金2 小时前
linux/centos迁移conda文件夹
linux·centos·conda
im_AMBER3 小时前
算法笔记 05
笔记·算法·哈希算法
夏鹏今天学习了吗3 小时前
【LeetCode热题100(46/100)】从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
吃着火锅x唱着歌3 小时前
LeetCode 2389.和有限的最长子序列
算法·leetcode·职场和发展
纳于大麓3 小时前
Kotlin基础语法
linux·windows·kotlin