2024.02.22作业

1. 将互斥机制的代码实现重新敲一遍

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

#define MAXSIZE 128

int num = 520;
pthread_mutex_t mutex;

void* task1(void* arg)
{
    printf("111111111111111\n");

    pthread_mutex_lock(&mutex);

    num = 1314;

    sleep(3);

    printf("task1:num = %d\n", num);

    pthread_mutex_unlock(&mutex);
}

void* task2(void* arg)
{
    printf("222222222222222\n");

    pthread_mutex_lock(&mutex);

    num++;

    sleep(1);

    printf("task2:num = %d\n", num);

    pthread_mutex_unlock(&mutex);
}

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

    pthread_t thread1, thread2;
    if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \
        pthread_create(&thread2, NULL, task2, NULL) != 0)
    {
        perror("thread create error");
        return -1;
    }

    printf("thread1:%#lx, thread2:%#lx\n", thread1, thread2);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    pthread_mutex_destroy(&mutex);

    return 0;
}

2. 将无名信号量的代码实现重新敲一遍

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

#define MAXSIZE 1024

sem_t sem1;
sem_t sem2;
sem_t sem3;

void* task1(void* arg)
{
    int num = 5;
    while (num--)
    {
        sem_wait(&sem1);
        printf("A");
        sem_post(&sem2);
    }

    pthread_exit(NULL);
}

void* task2(void* arg)
{
    int num = 5;
    while (num--)
    {
        sem_wait(&sem2);
        printf("B");
        sem_post(&sem3);
    }

    pthread_exit(NULL);
}

void* task3(void* arg)
{
    int num = 5;
    while (num--)
    {
        sem_wait(&sem3);
        printf("C\n");
        sem_post(&sem1);
    }

    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    sem_init(&sem1, 0, 1);
    sem_init(&sem2, 0, 0);
    sem_init(&sem3, 0, 0);

    pthread_t thread1, thread2, thread3;
    if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \
        pthread_create(&thread2, NULL, task2, NULL) != 0 || \
        pthread_create(&thread3, NULL, task3, NULL) != 0)
    {
        puts("thread create error");
        return -1;
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);

    sem_destroy(&sem1);
    sem_destroy(&sem2);
    sem_destroy(&sem3);

    return 0;
}

3. 将条件变量的代码实现重新敲一遍

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

#define MAXSIZE 128

pthread_cond_t cond;
pthread_mutex_t mutex;

void* task1(void* arg)
{
    int num = 5;

    while (num--)
    {
        sleep(1);
        printf("%#lx:生产一辆车\n", pthread_self());
    }

    pthread_cond_broadcast(&cond);
    pthread_exit(NULL);
}

void* task2(void* arg)
{
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond, &mutex);

    printf("%#lx:获得一辆车\n", pthread_self());

    pthread_mutex_unlock(&mutex);
}

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

    pthread_t thread1, thread2, thread3, thread4, thread5, thread6;
    if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \
        pthread_create(&thread2, NULL, task2, NULL) != 0 || \
        pthread_create(&thread3, NULL, task2, NULL) != 0 || \
        pthread_create(&thread4, NULL, task2, NULL) != 0 || \
        pthread_create(&thread5, NULL, task2, NULL) != 0 || \
        pthread_create(&thread6, NULL, task2, NULL) != 0)
    {
        perror("thread create error");
        return -1;
    }

    printf("thread1:%#lx, thread2:%#lx, thread3:%#lx, thread4:%#lx, thread5:%#lx, thread6:%#lx\n", 
            thread1, thread2, thread3, thread4, thread5, thread6);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);
    pthread_join(thread5, NULL);
    pthread_join(thread6, NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return 0;
}

4. 将无名管道的代码实现重新敲一遍

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

#define MAXSIZE 1024

int main(int argc, char const *argv[])
{
    int pipefd[2] = { 0 };
    if (pipe(pipefd) == -1)
    {
        perror("pipe error");
        return -1;
    } 
    printf("pipefd[0]=%d, pipefd[1]=%d\n", pipefd[0], pipefd[1]);

    pid_t pid = fork();
    if (pid > 0)
    {
        close(pipefd[0]);

        char wbuf[128] = "";
        while (1)
        {
            bzero(wbuf, sizeof(wbuf));

            fgets(wbuf, sizeof(wbuf), stdin);
            wbuf[strlen(wbuf) - 1] = 0;

            write(pipefd[1], wbuf, strlen(wbuf));
            
            if (strcmp(wbuf, "quit") == 0)
            {
                break;
            }
        }

        close(pipefd[1]);

        wait(NULL);
    }
    else if (pid == 0)
    {
        close(pipefd[1]);

        char rbuf[128] = "";
        while (1)
        {
            bzero(rbuf, sizeof(rbuf));

            read(pipefd[0], rbuf, sizeof(rbuf));
            printf("父进程传入的字符串是:%s\n", rbuf);
            
            if (strcmp(rbuf, "quit") == 0)
            {
                break;
            }
        }

        close(pipefd[0]);

        exit(EXIT_SUCCESS);
    }

    return 0;
}

5. 将有名管道的代码实现重新敲一遍

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

#define MAXSIZE 128

void menu()
{
    printf("请选择:\n");
    printf("1.创建管道\n");
    printf("2.输入\n");
    printf("3.输出\n");
    printf("4.删除管道\n");
    printf("选择:");
}

int init_fifo(char* filename)
{
    if (mkfifo(filename, 0664) == -1)
    {
        return -1;
    }
    return 0;
}

void* send(void* arg)
{
    char* filename = (char*)arg;
    
    int wfd = -1;
    if ((wfd = open(filename, O_WRONLY)) == -1)
    {
        perror("open error");
        exit(EXIT_FAILURE);
    }

    char wbuf[MAXSIZE] = "";
    while (1)
    {
        printf("输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf) - 1] = 0;

        write(wfd, wbuf, strlen(wbuf));

        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }
    }

    close(wfd);
    exit(EXIT_SUCCESS);
}

void* receive(void* arg)
{
    int rfd = -1;
    if ((rfd = open("./myfifo", O_RDONLY)) == -1)
    {
        perror("open error");
        exit(EXIT_FAILURE);
    }

    char rbuf[MAXSIZE] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));
        read(rfd, rbuf, sizeof(rbuf));

        printf("收到的数据为:%s\n", rbuf);

        if (strcmp(rbuf, "quit") == 0)
        {
            break;
        }
    }

    close(rfd);
    exit(EXIT_SUCCESS);
}

int main(int argc, char const *argv[])
{
    int n;
    menu();
    scanf("%d", &n);
    getchar();

    char filename[] = "./myfifo";
    if (n == 1)
    {
        if (init_fifo(filename) == 0)
        {
            puts("创建成功!");
        }
        else
        {
            puts("创建失败!");
        }
        return 0;
    }

    if (n == 4)
    {
        char cmd[MAXSIZE];
        snprintf(cmd, sizeof(cmd), "rm %s", filename);
        system(cmd);
        return 0;
    }

    pthread_t thread = -1;
    if (n == 2)
    {
        if (pthread_create(&thread, NULL, send, filename) != 0)
        {
            perror("create thread error");
            return -1;
        }
    }

    if (n == 3)
    {
        if (pthread_create(&thread, NULL, receive, filename) != 0)
        {
            perror("create thread error");
            return -1;
        }
    }

    pthread_join(thread, NULL);

    return 0;
}

6. 使用有名管道完成两个进程的相互通信(可使用多进程或多线程)

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

#define MAXSIZE 128

void menu()
{
    printf("请选择:\n");
    printf("1.创建管道\n");
    printf("2.输入输出\n");
    printf("3.输出输入\n");
    printf("4.删除管道\n");
    printf("选择:");
}

int init_fifo(char* filename)
{
    if (mkfifo(filename, 0664) == -1)
    {
        return -1;
    }
    return 0;
}

void* send(void* arg)
{
    char* filename = (char*)arg;
    
    int wfd = -1;
    if ((wfd = open(filename, O_WRONLY)) == -1)
    {
        perror("open error");
        exit(EXIT_FAILURE);
    }

    char wbuf[MAXSIZE] = "";
    while (1)
    {
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf) - 1] = 0;

        write(wfd, wbuf, strlen(wbuf));

        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }
    }

    close(wfd);
    exit(EXIT_SUCCESS);
}

void* receive(void* arg)
{
    char* filename = (char*)arg;

    int rfd = -1;
    if ((rfd = open(filename, O_RDONLY)) == -1)
    {
        perror("open error");
        exit(EXIT_FAILURE);
    }

    char rbuf[MAXSIZE] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));
        read(rfd, rbuf, sizeof(rbuf));

        printf("收到的数据为:%s\n", rbuf);

        if (strcmp(rbuf, "quit") == 0)
        {
            break;
        }
    }

    close(rfd);
    exit(EXIT_SUCCESS);
}

int main(int argc, char const *argv[])
{
    int n;
    menu();
    scanf("%d", &n);
    getchar();

    char f1[] = "./myfifo0";
    char f2[] = "./myfifo1";
    if (n == 1)
    {
        if (init_fifo(f1) == 0 && init_fifo(f2) == 0)
        {
            puts("创建成功!");
        }
        else
        {
            puts("创建失败!");
        }
        return 0;
    }

    if (n == 4)
    {
        char cmd[MAXSIZE];
        snprintf(cmd, sizeof(cmd), "rm %s %s", f1, f2);
        system(cmd);
        return 0;
    }

    pthread_t thread1 = -1;
    pthread_t thread2 = -1;
    if (n == 2)
    {
        if (pthread_create(&thread1, NULL, send, f1) != 0)
        {
            perror("create thread error");
            return -1;
        }
        if (pthread_create(&thread2, NULL, receive, f2) != 0)
        {
            perror("create thread error");
            return -1;
        }
    }

    if (n == 3)
    {
        if (pthread_create(&thread1, NULL, send, f2) != 0)
        {
            perror("create thread error");
            return -1;
        }
        if (pthread_create(&thread2, NULL, receive, f1) != 0)
        {
            perror("create thread error");
            return -1;
        }
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值