作业-11.15
创始人
2024-01-22 05:29:42
0

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

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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...