文件IO第6天作业

文章描述了使用C语言中的线程和条件变量实现线程间的同步,以及通过FIFO(命名管道)实现实时的进程间通信。ABC线程示例展示了如何确保按顺序打印,而AB进程对话则演示了如何创建和使用FIFO进行双向通信直到一方退出。
摘要由CSDN通过智能技术生成

1、有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。要求打印的结果为ABC.

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

int flag = 0;//0:A, 1:B, 2:C
pthread_cond_t cond1, cond2, cond3;
pthread_mutex_t mutex;

void *func1(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 0)
            pthread_cond_wait(&cond1, &mutex);
        printf("A");
        flag = 1;
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

void *func2(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 1)
            pthread_cond_wait(&cond2, &mutex);
        printf("B");
        flag = 2;
        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

void *func3(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 2)
            pthread_cond_wait(&cond3, &mutex);
        printf("C\n");
        flag = 0;
        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    pthread_mutex_init(&mutex, NULL);

    pthread_cond_init(&cond1, NULL);
    pthread_cond_init(&cond2, NULL);
    pthread_cond_init(&cond3, NULL);

    pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, NULL, func1, NULL);
    pthread_create(&tid2, NULL, func2, NULL);
    pthread_create(&tid3, NULL, func3, NULL);

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

    pthread_mutex_destroy(&mutex);

    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    return 0;
}

2、实现AB进程对话。
a.A进程发送一句话后,B进程接收到打印。然后B进程发送一句话,A进程接收后打印

b.重复上述步骤。直到AB接收或者发送完quit后,结束AB进程

A代码

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>



int main(int argc, char const *argv[])
{
    umask(0);
    if(mkfifo("./fifo1", 0777) < 0)
    {
        if(errno != 17)
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("创建成功\n");
	if(mkfifo("./fifo2", 0664) < 0)
	{
    	if(errno != 17)
    	{
        	perror("mkfifo");
        	return -1;
    	}
	}
	printf("创建成功\n");


    ssize_t fd1 = open("./fifo1", O_RDONLY);
    if(fd1 < 0)
    {
        perror("open");
        return -1;
    }
	ssize_t fd2 = open("./fifo2", O_WRONLY); 
	if(fd2 < 0)
	{
		perror("open2");
		return -1;
	}
	char buf1[32]="";
	char buf2[32]="";
	int res1 = 0;
	while(1)
	{

		res1 = read(fd1, buf1, sizeof(buf1));
		if(res1 < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res1)
		{
			printf("进程退出\n");
		}
		printf("B:%s\n", buf1); 
		if(strcmp(buf1, "quit") == 0)
			break;
		
		bzero(buf2, sizeof(buf2));
		fgets(buf2, sizeof(buf2), stdin);
		buf2[strlen(buf2)-1] = '\0';
		write(fd2, buf2, sizeof(buf2));
		if(strcmp(buf2, "quit") == 0)
			break;
	
	}
	close(fd1);
	close(fd2);
   return 0;
}

B代码

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>



int main(int argc, char const *argv[])
{
    umask(0);
    if(mkfifo("./fifo1", 0777) < 0)
    {
        if(errno != 17)
        {
            perror("mkfifo");
            return -1;
        }
    }
    printf("创建成功\n");
	if(mkfifo("./fifo2", 0664) < 0)
	{
    	if(errno != 17)
    	{
        	perror("mkfifo");
        	return -1;
    	}
	}
	printf("创建成功\n");


    ssize_t fd1 = open("./fifo1", O_WRONLY);
    if(fd1 < 0)
    {
        perror("open");
        return -1;
    }
	ssize_t fd2 = open("./fifo2", O_RDONLY); 
	if(fd2 < 0)
	{
		perror("open2");
		return -1;
	}
	char buf1[32]="";
	char buf2[32]="";
	int res1 = 0;
	while(1)
	{
		bzero(buf2, sizeof(buf2));
		fgets(buf2, sizeof(buf2), stdin);
		buf2[strlen(buf2)-1] = '\0';
		write(fd1, buf2, sizeof(buf2));
		if(strcmp(buf2, "quit") == 0)
    		break;

		res1 = read(fd2, buf1, sizeof(buf1));
		if(res1 < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == res1)
		{
			printf("进程退出\n");
		}
		printf("A:%s\n", buf1); 
		if(strcmp(buf1, "quit") == 0)
			break;
	
	}
	close(fd1);
	close(fd2);
   return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值