【操作系统】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);
}
相关推荐
EelBarb5 分钟前
seafile:ubuntu搭建社区版seafile12.0
linux·运维·ubuntu
Xam_d_LM8 分钟前
【Latex】Windows/Ubuntu 绘制 eps 矢量图通用方法(drawio),支持插入 Latex 数学公式
linux·ubuntu·科研·矢量图·drawio
402 Payment Required20 分钟前
serv00 ssh登录保活脚本-邮件通知版
运维·chrome·ssh
小柏ぁ25 分钟前
calico/node is not ready: BIRD is not ready: BGP not established with xxx
运维·docker·kubernetes
Mintimate1 小时前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器
RussellFans1 小时前
Linux 环境配置
linux·运维·服务器
嘉陵妹妹1 小时前
深度优先算法学习
学习·算法·深度优先
GalaxyPokemon2 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展
高冷的肌肉码喽2 小时前
Linux-进程间的通信
linux·运维·服务器
乖乖是干饭王2 小时前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu