【嵌入式学习】IO进程线程-Day5-进程线程通信

本文详细介绍了C语言中如何使用互斥锁和信号量实现互斥机制和同步机制,包括线程间的协作与资源管理。通过实例展示了多个场景,如线程间的交替执行、文件复制和字符序列输出。
摘要由CSDN通过智能技术生成

笔记&&思维导图

见我的博客:地址

作业:

1> 将互斥机制代码重新实现一遍

/*
 * Filename: 1.c
 * Author: linus
 * Date: 2024-01-05
 * Version: 1.0
 *
 * Description: 互斥机制
 */
 
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <string.h>

//创建一个互斥锁
pthread_mutex_t mutex;

void* task(void *arg)
{
	while (1)
	{
		pthread_mutex_lock(&mutex);

		printf("子线程");

		pthread_mutex_unlock(&mutex);
	}
	

}

int main(int argc, const char *argv[])
{	
	//定义线程号变量
	pthread_t tid;

	//初始化互斥锁
	pthread_mutex_init(&mutex, NULL);

	if(pthread_create(&tid,NULL,task,NULL)!=0)
	{
		perror("tid create error\n");
		return -1;
	}

	while (1)
	{
		//获取锁资源
		pthread_mutex_lock(&mutex);

		printf("主线程\n");

		//释放锁资源
		pthread_mutex_unlock(&mutex);
	}
	



	return 0;
}


2> 将同步机制代码重新实现一遍

/*
 * Filename: 2.c
 * Author: linus
 * Date: 2024-01-05
 * Version: 1.0
 *
 * Description: The purpose of this code.
 */
 
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>

sem_t sem;

void* task1(void* arg)
{
	while(1)
	{
		sem_post(&sem);
		printf("post了sem\n");
		sleep(2);
	}
	pthread_exit(NULL);
}

void* task2(void* arg)
{
	while(1)
	{
		sem_wait(&sem);
		printf("收到了\n");

	}

	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2;

	sem_init(&sem,0,0);

	if((pthread_create(&tid1,NULL,task1,NULL))!=0)
	{
		perror("thread creat error");
		return -1;
	}
	if((pthread_create(&tid2,NULL,task2,NULL))!=0)
	{
		perror("thread creat error");
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	return 0;
}

3> 使用三个线程完成两个文件的拷贝,线程1完成拷贝前一半,线程2完成拷贝后一半,主线程回收两个分支线程的资源

/*
 * Filename: 3.c
 * Author: linus
 * Date: 2024-01-05
 * Version: 1.0
 *
 * Description: 使用三个线程完成两个文件的拷贝,
 * 				线程1完成拷贝前一半,线程2完成拷贝后一半,
 * 				主线程回收两个分支线程的资源
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>

sem_t sem;
int len,half_hen;
const char *srcFile,*destFile;

int file_len(const char *file)
{
	FILE* fp=NULL;
	if((fp=(fopen(file,"r")))==NULL)
	{
		perror("open fire error");
		return -1;
	}
	fseek(fp,0,SEEK_END);
	return ftell(fp);
}

int copy_file(const char *srcFile, const char *destFile, int begin, int end)
{
	FILE* srcfp=NULL;
	FILE* destfp=NULL;
	if ((srcfp=(fopen(srcFile, "r"))) == NULL)
	{
		perror("open srcFile error");
		return -1;
	}
	if ((destfp=(fopen(destFile, "w"))) == NULL)
	{
		perror("open destFile error");
		return -1;
	}

	fseek(destfp,begin,SEEK_SET);
	fseek(srcfp,begin,SEEK_SET);
	char buff;
	while(ftell(srcfp)<end)
	{
		int res=fread(&buff,1,sizeof(buff),srcfp);	
		fwrite(&buff,1,res,destfp);
	}
	fclose(srcfp);
	fclose(destfp);
	return 0;
}

void *task1(void *arg)
{
	copy_file(srcFile,destFile,0,half_hen);
	pthread_exit(NULL);
}

void *task2(void *arg)
{
	copy_file(srcFile,destFile,half_hen,len);
	pthread_exit(NULL);

}

int main(int argc, const char *argv[])
{
	if(argc!=3)
	{
		printf("输入有误!\n");
		return -1;
	}
	

	srcFile=argv[1];
	destFile=argv[2];

	len=file_len(argv[1]);
	half_hen=len/2;
	pthread_t tid1, tid2;
	
	if ((pthread_create(&tid1, NULL, task1, NULL)) != 0)
	{
		perror("thread creat error");
		return -1;
	}
	if ((pthread_create(&tid2, NULL, task2, NULL)) != 0)
	{
		perror("thread creat error");
		return -1;
	}


	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);

	return 0;
}

4> 使用三个线程完成:线程1输出字符’A’,线程2输出字符’B’,线程3输出字符’C’,要求输出结果为:ABCABCABCABCABC…

/*
 * Filename: 4.c
 * Author: linus
 * Date: 2024-01-05
 * Version: 1.0
 *
 * Description: 使用三个线程完成:线程1输出字符’A’,线程2输出字符’B’,
 *				线程3输出字符’C’,
 *				要求输出结果为:
 *				ABCABCABCABCABC…
 *				思路:使用三个互斥信号量来实现排队输出
 */
 
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>

sem_t sem1,sem2,sem3;

void* task1(void *arg)
{
	while (1)
	{
		sem_wait(&sem2);
		printf("B");
		fflush(stdout);//遇到的坑,没有刷新缓存区
		sem_post(&sem3);
	}
}

void* task2(void *arg)
{
	while (1)
	{
		sem_wait(&sem3);
		printf("C");
		fflush(stdout);
		sem_post(&sem1);
	}
}

int main(int argc, const char *argv[])
{
	// 初始化匿名信号量
	sem_init(&sem1,0,1);
	sem_init(&sem2,0,0);
	sem_init(&sem2,0,0);
	// 创建两个线程
	pthread_t tid1, tid2;
	
	if ((pthread_create(&tid1, NULL, task1, NULL)) != 0)
	{
		perror("thread creat error");
		return -1;
	}
	if ((pthread_create(&tid2, NULL, task2, NULL)) != 0)
	{
		perror("thread creat error");
		return -1;
	}
	printf("线程创建成功\n");
	
	// 主线程
	
	while (1)
	{
		sem_wait(&sem1);
		printf("A");
		fflush(stdout);
		sem_post(&sem2);
		sleep(1);
	}
	// 释放线程
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	return 0;
}


实现结果

5> 周末完成C语言基础测试题
没写完,哈哈

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值