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

1.将父子进程拷贝图片的那题目进行修改:

父子进程的代码单独分离到其他的可执行二进制程序用,用exec函数族进行组合。

  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 int main(int argc, const char *argv[])
  7 {
  8      int path = open("./3.jpg", O_RDONLY);
  9      {
 10         perror("open");
 11         return -1;
 12      }
 13      printf("文件打开成功\n");
 14 
 15      int path1= open("./4.jpg",O_WRONLY | O_CREAT | O_TRUNC, 0777);
 16      if(path1 < 0)
 17      {
 18         perror("open");
 19         return -1;
 20      }
 21     printf("文件打开成功\n");
 22 
 23      off_t res = lseek(path,0,SEEK_END);
 24      pid_t pid =fork();
 25 
 26      /*int path = atoi(argv[0]);
 27      int path1 = atoi(argv[1]);
 28      off_t p_res = atoi(argv[2]);
 29      */
 30 
 31 
 32 
 33     if (pid >0)
 34     {
 35         char brr[10];
 36         sprintf(brr,"%d",path);
 37         char brr1[10];                                              
 38         sprintf(brr1,"%d",path1);
 39         char brr2[10];
 40         sprintf(brr2,"%ld",res);
 41 
 42         execl("half",brr,brr1,brr2,NULL);
 43     }
 44     else if ( pid ==0)
 45     {
 46         lseek(path,res / 2,SEEK_SET);
 47         lseek(path1,res / 2,SEEK_SET);
 48         char picture;
 49         for(int i =res / 2;i < res ; i++)
 50     {
 51         read(path,&picture,1);
 52         write(path1,&picture,1);
 53     }
 54         printf("子进程读取完毕\n");
 55     }
 56     else
 57     {
 58         perror("fork");
 59         return -1;
 60     }
 61 
 62     close(path);
 63     close(path1);
 64     return 0;
 65 }
  1 #include <stdio.h>
  2 #include <stdio.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <fcntl.h>
  6 #include <unistd.h>
  7 #include <stdlib.h>
  8 
  9 int main(int argc, const char *argv[])
 10 {
 11     int path = atoi(argv[0]);
 12     int path1 = atoi(argv[1]);
 13     off_t res = atoi(argv[2]);
 14 
 15     sleep(2);
 16     lseek(path,0,SEEK_SET);
 17     lseek(path1,0,SEEK_SET);
 18 
 19 
 20     char picture;
 21     for(int i =0;i<res/2;i++)
 22     {
 23         read(path,&picture,1);
 24         write(path1,&picture,1);
 25     }                                               
 26 
 27     printf("父进程读取完毕\n");
 28 
 29     close(path);
 30     close(path1);
 31 
 32 
 33     return 0;
 34 }

运行结果

2.

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

A线程循环打印buf字符串,

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

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

不允许使用sleep函数

分析出现错误的原因。

  1 #include <stdio.h>
  2 #include <pthread.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 
  7 char buf[]="12324567";
  8 void * order(void *arg)
  9 {
 10     while(1)
 11     {
 12         pthread_mutex_lock((pthread_mutex_t *)arg);//上锁
 13         printf("%s\n",buf);//打印字符串
 14         pthread_mutex_unlock((pthread_mutex_t *)arg);//解锁
 15     }
 16 
 17     pthread_exit(NULL);
 18 }
 19 void * over_order(void *arg)
 20 {
 21     int len,i;
 22     char temp;
 23     len = strlen(buf);
 24     while(1)
 25     {
 26         pthread_mutex_lock((pthread_mutex_t *)arg);//上锁
 27         for(i = 0;i<len/2;i++)
 28         {
 29             temp = buf[i];
 30             buf[i] = buf[len - i-1];
 31             buf[len -i-1]= temp;
 32         }
 33         pthread_mutex_unlock((pthread_mutex_t *)arg);//解锁
 34     }
 35 
 36 
 37     pthread_exit(NULL);
 38 }
 39 
 40 int main(int argc, const char *argv[])
 41 {
 42     //创建互斥锁
 43     pthread_mutex_t mutex;                                                    
 44     pthread_mutex_init(&mutex,NULL);//互斥锁初始化
 45 
 46 
 47     pthread_t tid1;
 48     if(pthread_create(&tid1,NULL,order,(void*)&mutex) < 0)
 49     {
 50         fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
 51         return -1;
 52     }
 53     pthread_t tid2;
 54     if(pthread_create(&tid2,NULL,over_order,(void*)&mutex) < 0)
 55     {
 56         fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
 57         return -1;
 58     }
 59     pthread_detach(tid2);//分离线程
 60 
 61     pthread_join(tid1,NULL);//阻塞,防止主线程退出
 62     pthread_mutex_destroy(&mutex);//销毁互斥锁
 63 
 64 
 65 
 66 
 67     return 0;
 68 }

运行结果

3用线程完成图片拷贝,要求一个线程拷贝一半,另一个线程拷贝另一半

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值