• 作业-11.15


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

    #include
    #include <pthread.h>
    #include
    #include

    //临界资源
    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
    #include
    #include
    #include
    #include
    #include
    #include

    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;
    }

  • 相关阅读:
    springboot-自动装配原理
    Redis 官方可视化工具,功能真的强大
    在TypeScript项目中进行BDD测试
    如何用VS2015新建一个空白项目
    数据优化 | CnOpenData中国工业企业专利及引用被引用数据
    Linux 扩展篇 YUM+Shell编程
    Python实战项目5-Git远程仓库/分支合并/冲突解决
    集合框架7个问答
    论文阅读_大语言模型_Llama2
    BusyBox编译时选择合适的编译器
  • 原文地址:https://blog.csdn.net/MisakaMikotto/article/details/127876272