io_day05

作业1

题目

在这里插入图片描述

结果

在这里插入图片描述

代码

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

// 输出屏幕
void print_data()
{
    char buf[1024];
    char ch;

    while (1) {
        fgets(buf, sizeof(buf), stdin);
        if (!strncmp(buf, "quit", 4))
            break;
        fputs(buf, stdout);
    }
}

// 子线程
void *thread_func(void *arg)
{
    char *p = "0x78";
    while (1) {
        printf("child thread is running\n");
        print_data();
        pthread_exit(p);
    }
}

int main(int argc, char const *argv[])
{
    pthread_t tid;
    int ret;
    int cnt = 0;
    char *retval = NULL;

    // 开启子线程
    ret = pthread_create(&tid, NULL, thread_func, NULL);
    if (ret != 0) {
        errno = ret;
        perror("pthread_create()");
        return -1;
    }

    // 主线程
    while (1) {
        printf("%d====================\n", cnt++);
        printf("main thread is running\n");
        sleep(1);
        pthread_join(tid, (void **)&retval);
        printf("%s\n", retval);
    }

    return 0;
}

作业2

题目

在这里插入图片描述

结果

在这里插入图片描述

代码

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

pthread_t tid;   // 读线程
pthread_t tid2;  // 写线程

// 读文件
void read_file(int fd)
{
    char buf[1024];
    int n;

    while (1) {
        memset(buf, 0, sizeof(buf));
        n = read(fd, buf, sizeof(buf));

        if (n == 0) {
            continue;
        }

        write(STDOUT_FILENO, buf, sizeof(buf));
    }
}

// 写文件
void write_file(int fd)
{
    char buf[1024];
    int n;

    while (1) {
        memset(buf, 0, sizeof(buf));
        fgets(buf, sizeof(buf), stdin);
        if (!strncmp(buf, "quit", 4)) {
            break;
        }
        n = write(fd, buf, sizeof(buf));
        lseek(fd, -n, SEEK_CUR);
    }
}

// 读线程
void *read_thread(void *arg)
{
    int fd = *(int *)arg;

    printf("read_thread is running\n");
    while (1) {
        sleep(2);
        read_file(fd);
    }
}

// 写线程
void *write_thread(void *arg)
{
    int fd = *(int *)arg;

    printf("write_thread is running\n");
    while (1) {
        write_file(fd);
        sleep(1);
        pthread_cancel(tid2);
        pthread_exit(NULL);
    }
}

int main(int argc, char const *argv[])
{
    int ret;   // 读线程
    int ret2;  // 写线程
    int fd;
    char *retval = NULL;

    fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);

    // 开启子线程
    ret = pthread_create(&tid, NULL, read_thread, (void *)&fd);
    ret2 = pthread_create(&tid2, NULL, write_thread, (void *)&fd);
    if (ret != 0 || ret2 != 0) {
        errno = ret;
        perror("pthread_create()");
        return -1;
    }

    // 主线程
    printf("main thread is running\n");
    while (1) {
        pthread_join(tid, (void **)&retval);
        sleep(1);
    }

    close(fd);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值