• 作业-11.11


    1、要求创建两个线程,以及一个全局变量,char str[] = "123456";要求如下:

            (1)一个线程专门用于打印str;

            (2)另外一个线程专门用于倒置str字符串,不使用辅助数组。

            (3)要求打印出来的结果必须是123456或者654321,不能出现乱序情况,

                    例如623451 653421

    #include
    #include
    #include
    #include

    //int *p = NULL;

    char *tmp = NULL;

    void* call_back(void *arg)
    {
        int *p = (int *)arg;
        while(1)
        {
            if(1 == *p)
            {
                while(1)
                {

                    if(NULL == tmp)
                    {
                        sleep(1);
                    }else
                    {
                        int len = strlen(tmp);
                        int i=0,j=len-1;
                        while(i                     {
                            if(tmp[i] != tmp[j])
                            {
                                tmp[i] ^= tmp[j];
                                tmp[j] ^= tmp[i];
                                tmp[i] ^= tmp[j];
                            }
                            i++;
                            j--;
                        }
                        printf("逆序并打印\n");
                        printf("%s\n", tmp);
                        *p = 1;
                    }
                    if(*p)
                    {
                        break;
                    }
                }
                *p = 0;
            }
        }
    }

    int main(int argc, const char *argv[])
    {
        //创建一个线程
        pthread_t tid;

        int flag = 0;
        if(pthread_create(&tid, NULL, call_back, (void *)&flag) != 0)
        {
            perror("pthread_create");
            return -1;
        }
        printf("线程创建成功\n");

        char arr_char[7] = "123456";
        tmp = arr_char;

        while(1)
        {
            if(0 == flag)
            {
                printf("----------顺序打印---------\n");
                printf("%s\n", tmp);
                flag = 1;
            }
        }

        return 0;
    }

     

    2、要求用线程拷贝一张图片,一个线程拷贝前半部分,另一个线程拷贝后半部分

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int count = 0;
    int flag = 0;
    int fd = 0;
    int fd_w = 0;

    void* call_back(void *arg)
    {
        ssize_t res = 0;
        ssize_t res_w = 0;

        char arr[128] = "0";
        int len = sizeof(arr);

        while(1)
        {
            if(flag != 1)
            {
                sleep(1);
            }else if(1 == flag)
            {
                printf("coun = %d.\n", count);
                lseek(fd, count*len, SEEK_SET);
                lseek(fd_w, count*len, SEEK_SET);
                while(1)
                {
                    bzero(arr, sizeof(arr));
                    res = read(fd, arr , len);
                    if(!res)
                    {
                        flag = 2;
                        break;
                    }
                    else if(res < 0)
                    {
                        fprintf(stderr, "__%d__\n", __LINE__);
                        perror("read");

                        return NULL;
                    }
                    res_w = write(fd_w, arr, res);
                    if(res_w < 0)
                    {
                        perror("write");
                        return NULL;
                    }
                }

                close(fd);
                close(fd_w);
                putchar(10);

            }
        }


    }

    int main(int argc, const char *argv[])
    {
        if(argc < 3)
        {
            fprintf(stderr, "请从命令行输入图片名和复制后的文件名\n");
            return -1;
        }
        if(!strcmp(argv[1], argv[2]))
        {
            fprintf(stderr, "复制后的文件名和原文件名不能相同!!!\n");
            return -1;
        }

        //打开文件
        fd = open(argv[1], O_RDONLY);
        fd_w = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0777);
        if(fd < 0)
        {
            perror("open");
            return -1;
        }
        if(fd_w < 0)
        {
            perror("open");
            return -1;
        }

        ssize_t res = 0;
        ssize_t res_w = 0;

        char arr[128] = "0";
        int len = sizeof(arr);


        count = (lseek(fd, 0, SEEK_END)/len);
        count++;
        printf("count = %d.\n", count);


        //创建一个线程
        pthread_t tid;
        if(pthread_create(&tid, NULL, call_back, NULL) != 0)
        {
            perror("pthread_create");
            return -1;
        }
        printf("线程创建成功\n");

        if(0 == flag)
        {
            printf("主进程前半\n");
            int tmp = count;

            //将指针偏移到文件开头
            lseek(fd ,  0, SEEK_SET);
            lseek(fd_w, 0, SEEK_SET);

            while(1)
            {
                bzero(arr, sizeof(arr));
                res = read(fd, arr , len);

                if(res < 0)
                {
                    fprintf(stderr, "__%d__\n", __LINE__);
                    perror("read");

                    return -1;
                }

                //写入数据
                res_w = write(fd_w, arr, res);
                if(res_w < 0)
                {
                    perror("write");
                    return -1;
                }

                //所需要的拷贝次数--
                count--;

                //如果拷贝的数据比总数据的一半就退出循环
                if(count < (tmp/2))
                {
                    flag = 1;
                    break;
                }
            }
        }
        while(1)
        {
            sleep(1);
            if(2 == flag)
            {
                break;
            }
        }

        return 0;
    }

     

  • 相关阅读:
    使用jdbcTemplate的new BeanPropertyRowMapper<>查不到数据
    程序员做自己的产品 “在线客服系统” 之:种子用户的重要性
    前端工作小结80-title写活
    【前沿技术RPA】 一文学会用UiPath实现自动检索电子邮件(Email Automation)
    海外动态IP代理可以用来批量注册邮箱吗?
    2023年9款好用的在线流程图软件推荐!
    前后端分离项目,vue+uni-app+php+mysql电影院售票系统(H5移动项目) 开题报告
    c#使用log4net的3种调用方法
    干货!一文搞定无头浏览器的概念以及在selenium中的应用
    UnityAPI学习之 播放游戏音频的类(AudioSource)
  • 原文地址:https://blog.csdn.net/MisakaMikotto/article/details/127814808