作业-11.15

1、编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推

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

//临界资源
char id[] = "ABC";
//互斥锁的变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int flag = 0;
int count;

void *call_back_a(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 0)
        {
            pthread_cond_wait(&cond ,&mutex);
        }
        count++;
        printf("[%d]A", count);
        flag = 1;

        pthread_cond_signal(&cond);
        sleep(1);

        pthread_mutex_unlock(&mutex);

        if(10 == count)
        {
            pthread_exit(NULL);
        }

    }

    pthread_exit(NULL);
}

void *call_back_b(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 1)
        {
            pthread_cond_wait(&cond ,&mutex);
        }
        printf("B");
        flag = 2;

        pthread_cond_signal(&cond);
        sleep(1);

        pthread_mutex_unlock(&mutex);

        if(10 == count)
        {
            pthread_exit(NULL);
        }
    
    }

    pthread_exit(NULL);
}

void *call_back_c(void *arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 2)
        {
            pthread_cond_wait(&cond ,&mutex);
        }
        printf("C\n");
        flag = 3;

        pthread_cond_signal(&cond);
        sleep(1);

        pthread_mutex_unlock(&mutex);

    
        if(10 == count)
        {
            pthread_exit(NULL);
        }
    }

    pthread_exit(0);
}

int main(int argc, const char *argv[])
{
    //创建三个线程
    pthread_t tid1, tid2, tid3;
    if(pthread_create(&tid1, NULL, call_back_a, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&tid2, NULL, call_back_b, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    if(pthread_create(&tid3, NULL, call_back_c, NULL) != 0)
    {
        perror("pthread_create");
        return -1;
    }

    //阻塞
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    //销毁互斥锁
    pthread_mutex_destroy(&mutex);
    
    //销毁条件变量
    pthread_cond_destroy(&cond);


    return 0;
}

2、创建父子进程,实现父子进程的通话。

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

int flag = 1;
int fa_send, child_send;

int main(int argc, const char *argv[])
{
    int fa_fd[2] = {0};
    int ch_fd[2] = {0};
    //创建一个管道

    if(pipe(fa_fd) < 0)
    {
        perror("pipe");
        return -1;
    }
    if(pipe(ch_fd) < 0)
    {
        perror("pipe");
        return -1;
    }

    char buf[128] = "";
    ssize_t res = 0;

    //创建一个子进程
    pid_t pid = fork();
    if(pid > 0)
    {
        //父进程

        while(1)
        {
            bzero(buf, sizeof(buf));
            printf("父进程写>>>");
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf)-1] = '\0';
            res = write(fa_fd[1], buf, strlen(buf));
            if(res < 0)
            {
                perror("write");
                return -1;
            }else if(0 == strcmp(buf, "quit"))
            {
                break;
            }

            bzero(buf, sizeof(buf));
            res = read(ch_fd[0], buf, sizeof(buf));
            if(res < 0)
            {
                perror("read");
                return -1;
            }
            if(0 == read)
            {
                printf("子进程退出了\n");
                break;
            }
            else if(0 != read)
            {
                printf("从子进程收到\n");
                printf("%s.\n", buf);
            }
            if(0 == strcmp(buf, "quit"))
            {
                printf("父进程退出\n");
                break;
            }

        }
        wait(NULL);
        close(fa_fd[1]);
        close(ch_fd[0]);
        close(fa_fd[0]);
        close(ch_fd[1]);
        

    }
    else if(0 == pid)
    {
        //子进程

        while(1)
        {

            bzero(buf, sizeof(buf));
            res = read(fa_fd[0], buf, sizeof(buf));
            if(res < 0)
            {
                perror("read");
                return -1;
            }
            if(0 == read)
            {
                break;
            }
            else if(0 != read)
            {
                printf("从父进程收到\n");
                printf("%s.\n", buf);
            }
            if(0 == strcmp(buf, "quit"))
            {
                printf("父进程退出\n");
                break;
            }

            bzero(buf, sizeof(buf));
            printf("子进程写>>>");
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf)-1] = '\0';
            if(0 == strcmp(buf, "quit"))
            {
                break;
            }
            res = write(ch_fd[1], buf, strlen(buf));
            if(res < 0)
            {
                perror("write");
                return -1;
            }

        }
        wait(NULL);
        close(fa_fd[1]);
        close(ch_fd[0]);
        close(fa_fd[1]);
        close(ch_fd[0]);
        
        

    }
    else
    {
        perror("fork");
        return -1;
    }

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值