①分文件利用execl函数重启调用进程实现图片拷贝;②分析两个线程运行的错误原因;

分文件拷贝打印现象:

子文件代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>                                                  
#include <time.h>

int main(int argc, const char *argv[])
{

    int pa = open(argv[1],O_RDONLY);
    int pb = open(argv[2],O_WRONLY | O_CREAT |O_TRUNC,0777);

    char buf;
    size_t t = sizeof(buf);
    ssize_t re;
    int i;
    off_t L = lseek(pa,0,SEEK_END);

    lseek(pa,0,SEEK_SET);//初始偏移量
    lseek(pb,0,SEEK_SET);
    for(i = 0;i < L/2; i++)
    {
        buf = '0';
        re = read(pa,&buf,t);
        if(re < 0)
        {
            perror("read");
            return -1;
        }
        write(pb,&buf,t);
    }
    printf("父进程完成...\n");

    return 0;
}
                                                                    
                                                                    
                                                                    
                                                                    

2、要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。

a:A线程循环打印buf字符串

b:B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321。B线程中不打印!!倒置不允许使用辅助数组。

c:要求A线程打印出来的结果只能为 1234567 或者 7654321 不允许出现7634521 7234567等乱序情况

d:不允许使用sleep函数

e:分析出现错误的原因。

分析:线程在进程分配下的时间片内轮询工作,倒置未完成就会到打印,就会出现乱码的现象;

解决:我下面的代码使用了阻塞实现,等一等其他线程,以保证内容的完整性,这样就不会出现乱码;当然还可以使用互斥锁、信号灯、条件变量来保证分配时间内完成的任务的完整性。

线程数组更正后打印现象:

代码:

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

// void* afunc(void*arg);


char buf[] = "1234567";
void *thread01(void*arg)
{
    printf("this is thread01 func...\n");

    for(int i = 0; i < 7; i++)
    {
        printf("%c",buf[i]);
    }

    static int a = 1;
    pthread_exit((void*)&a);

    return NULL;
}


void *thread02(void*arg)
{
    printf("this is thread02 func...\n");

    int storage;
    for(int i = 0; i < 7-1; i++)
    {
        for(int j = 0; j < 7-1-i; j++)
        {
            if(buf[j] < buf[j+1])
            {
                storage = buf[j];
                buf[j] = buf[j+1];
                buf[j+1] = storage;
            }
        }
    }

    printf("排序完成...\n");
    static int b = 2;
    pthread_exit((void*)&b);

    return NULL;
}


int main(int argc, const char *argv[])
{
    pthread_t A;
    if(pthread_create(&A,NULL,thread01,NULL) != 0)
    {
        fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
        return -1;
    }
                                                                                  
    void*ptr;
    pthread_join(A,&ptr);
    putchar(10);

    pthread_t B;
    if(*(int*)ptr == 1)
    {
        //pthread_t B;
        if(pthread_create(&B,NULL,thread02,NULL) != 0)
        {
            fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
            return -1;
        }
    }

    void*ptr2;
    pthread_join(B,&ptr2);

    printf("\nthis is main func...\n");

    for(int i = 0; i < 7; i++)
    {
        printf("%c",buf[i]);
    }

    putchar(10);

    return 0;
}

                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值