IO day6作业

1.

1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 pthread_mutex_t bbb;
  6 struct aaa
  7 {
  8  int fd1;
  9  int fd2;
 10  off_t a;
 11 };
 12 void* zxc1(void *p)
 13 {
 14     pthread_mutex_lock(&bbb);
 15     int fd1 = ((struct aaa*)p)->fd1;
 16     int fd2 = ((struct aaa*)p)->fd2;
 17     off_t a = ((struct aaa*)p)->a;
 18     char buf;
 19     ssize_t res = 0;
 20     ssize_t b = 0;
 21     if(lseek(fd2,0,SEEK_SET) < 0)                                                 
 22     {
 23     ERR_MSG("lseek");
 24     return NULL;
 25     }
 26     if(lseek(fd1,0,SEEK_SET) < 0)
 27     {
 28      ERR_MSG("lseek");
 29      return NULL;
 30     }
 31     while(1)
 32     {
 33         res=read(fd1,&buf,1);                                                     
 34         b+=res;
 35         if(b == ((a/2)+1))
 36         {
 37             break;
 38         }
 39         write(fd2,&buf,1);
 40     }
 41     printf("前半部分拷贝完成\n");
 42     pthread_mutex_unlock(&bbb);
 43     pthread_exit(NULL);
 44 }
 45 void* zxc2(void *p)
 46 {
 47     pthread_mutex_lock(&bbb);
 48     int fd1 = ((struct aaa*)p)->fd1;
 49     int fd2 = ((struct aaa*)p)->fd2;
 50     off_t a = ((struct aaa*)p)->a;
 51     char buf;
 52     ssize_t res = 0;
 53     if(lseek(fd1,a/2,SEEK_SET) < 0)                                               
 54     {
 55      ERR_MSG("lseek");
 56      return NULL;
 57     }
 58     if(lseek(fd2,a/2,SEEK_SET) < 0)
 59     {
 60     ERR_MSG("lseek");
 61     return NULL;
 62     }
 63     while(1)
 64     {
 65         res=read(fd1,&buf,1);
 66         if(0 == res)
 67         {
 68             break;
 69         }
 70         write(fd2,&buf,1);
 71     }
 72     printf("后半部分拷贝完成\n");
 73     pthread_mutex_unlock(&bbb);
 74     pthread_exit(NULL);
 75 }
 76 int main(int argc, const char *argv[])
 77 {                                                                                 
 78     int fd1 = open("a.c",O_RDONLY);
 79     int fd2 = open("b.c",O_WRONLY | O_CREAT | O_TRUNC,0664);
 80     if(fd1 < 0)
 81     {
 82         ERR_MSG("open");
 83         return -1;
 84     }
 85     if(fd2 < 0)
 86     {
 87         ERR_MSG("open");
 88         return -1;
 89     }
 90     off_t a = lseek(fd1,0,SEEK_END);
 91     struct aaa p;
 92     p.fd1 = fd1;
 93     p.fd2 = fd2;
 94     p.a = a;
 95     pthread_mutex_init(&bbb,NULL);
 96     pthread_t tid1;
 97     pthread_t tid2;
 98     if(pthread_create(&tid1,NULL,zxc1,(void*)&p) != 0)
 99     {
100         printf("子线程1错误\n");
101         return -1;                                                                
102     }
103     if(pthread_create(&tid2,NULL,zxc2,(void*)&p) != 0)
104     {
105      printf("子线程2错误\n");
106      return -1;
107     }
108     pthread_join(tid1,NULL);
109     pthread_join(tid2,NULL);
110     pthread_mutex_destroy(&bbb);
111      if(close(fd1) < 0)
112     {
113      ERR_MSG("close");
114      return -1;
115     }
116     if(close(fd2) < 0)
117     {
118     ERR_MSG("close");
119     return -1;
120     }
121                                                                                   
122         return 0;
123 }

2.xmid

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值