240401 6.12 作业

1、将父子进程拷贝图片的那题目进行修改:父子进程的代码单独分离到其他的可执行二进制程序用,用exec函数族进行组合。

当前进程

#include <stdio.h>                                               
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    //以读的方式打开图片文件
    int fd = open("./1.jpg",O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }

    //以写的方式打开要写入的图片文件
    int fd1 = open("4_copy.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
    if(fd1 < 0)
    {
        perror("open");
        return -1;
    }

    off_t size = lseek(fd,0,SEEK_END);

    //创建子进程
    pid_t pid = fork();
    if(pid >0)   //说明是父进程
    {
        char arr1[10];
        sprintf(arr1,"%d",fd);
        char arr2[10];
        sprintf(arr2,"%d",fd1);
        char arr3[10];
        sprintf(arr3,"%ld",size);

        execl("b",arr1,arr2,arr3,NULL);
    }
    if(0 == pid)   //说明是子进程
    {
        lseek(fd,size/2,SEEK_SET);
        lseek(fd1,size/2,SEEK_SET);
        char c;
        for(int i =0;i<size/2;i++)
        {
            read(fd,&c,1);
            write(fd1,&c,1); //读一个写一个
        }
        printf("图片后半部分拷贝完毕\n");
    }
    else
    {
        perror("fork");
        return -1;
    }


    close(fd);
    close(fd1);
    return 0;
}

替换进程

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

int main(int argc, const char *argv[])
{
    int fd = atoi(argv[0]);
    int fd1= atoi(argv[1]);
    int size = atoi(argv[2]);

    sleep(4);
    lseek(fd,0,SEEK_SET);
    lseek(fd1,0,SEEK_SET);
    char c;
    for(int i =0;i<size/2;i++)
    {
        read(fd,&c,1);
        write(fd1,&c,1); //读一个写一个
    }
    printf("图片前半部分拷贝完毕\n");

    close(fd);
    close(fd1);
    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<unistd.h>
 #include<pthread.h>
 #include<string.h>
 #include <stdlib.h>
 
 char buf[]="1234567";
 int x=1;
  
 void*A(void*arg)
 {
     while(1)
     {
         if(x==1)
         {
             printf("%s\n",buf);
             x=0;
         }
     }
     pthread_exit(NULL);
 }
 
 void*B(void*arg){
     char s;
     int len,i;
     while(1)
     {
         if(x==0)
         {
             for(i=0,len=strlen(buf)-1;i<len;i++,len--)
             {
                 s=buf[i];
                 buf[i]=buf[len];
                 buf[len]=s;
             }
             x=1;
         }
     }
     pthread_exit(NULL);
 }
 int main(int argc, const char *argv[])
 {
     pthread_t tid1,tid2;
     pthread_create(&tid1,NULL,aaa,NULL);
     pthread_create(&tid2,NULL,bbb,NULL);
 
     pthread_join(tid1,NULL);
     return 0;
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值