IO day7作业

1.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 char buf[] = "1234567";
  6 sem_t sem1;
  7 sem_t sem2;
  8 void* zxc1()
  9 {
 10     while(1)
 11     {
 12         sleep(1);
 13 
 14         if(sem_wait(&sem1) < 0)
 15         {
 16         ERR_MSG("sem_wait");
 17         return NULL;
 18         }
 19 
 20         printf("%s\n",buf);
 21 
 22         if(sem_post(&sem1) < 0)
 23         {
 24          ERR_MSG("sem_post");
 25          return NULL;
 26         }
 27 
 28         if(sem_post(&sem2) < 0)
 29         {
 30          ERR_MSG("sem_post");
 31          return NULL;
 32         }
 33 
 34 
 35     }
 36     return NULL;
 37 }
 38 void* zxc2()
 39 {
 40     int a = strlen(buf);
 41     while(1)
 42     {
 43         sleep(1);
 44 
 45         if(sem_wait(&sem2) < 0)
 46         {
 47          ERR_MSG("sem_wait");
 48          return NULL;
 49         }
 50 
 51         if(sem_wait(&sem1) < 0)
 52         {
 53          ERR_MSG("sem_wait");                                                                                                                                                                                                                                                                                 
 54          return NULL;
 55         }
 56 
 57         for(int i=0,j=a-1;i<j;i++,j--)
 58         {
 59             char c=buf[i];buf[i]=buf[j];buf[j]=c;
 60         }
 61 
 62         if(sem_post(&sem1) < 0)
 63         {
 64          ERR_MSG("sem_post");
 65          return NULL;
 66         }
 67     }
 68 }
 69 int main(int argc, const char *argv[])
 70 {
 71     if(sem_init(&sem1,0,1) < 0)
 72     {
 73      ERR_MSG("sem_init");
 74      return -1;
 75     }
 76 
 77     if(sem_init(&sem2,0,0) < 0)
 78     {
 79      ERR_MSG("sem_init");
 80      return -1;
 81     }
 82 
 83     pthread_t tid1;
 84     pthread_t tid2;
 85 
 86     if(pthread_create(&tid1,NULL,zxc1,NULL) != 0)
 87     {
 88         printf("子线程1出错\n");
 89         return -1;
 90     }
 91 
 92     if(pthread_create(&tid2,NULL,zxc2,NULL) != 0)
 93     {
 94         printf("子线程2出错\n");
 95         return -1;
 96     }
 97 
 98     pthread_join(tid2,NULL);
 99 
100     if(sem_destroy(&sem1) < 0)
101     {
102      ERR_MSG("sem_destroy");
103      return -1;
104     }
105 
106     if(sem_destroy(&sem2) < 0)
107     {
108      ERR_MSG("sem_destroy");
109      return -1;
110     }
111 
112     return 0;
113 }
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                            

2.

  1 #include <stdio.h>                                                                                                                                                                                                                                                                                            
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 pthread_mutex_t mutex;
  6 pthread_cond_t cond;
  7 int flag = 0;
  8 char buf = 0;
  9 struct aaa
 10 {
 11     int fd;
 12     off_t a;
 13 };
 14 void* zxc1(void* p)
 15 {
 16     int fd = ((struct aaa*)p)->fd;
 17     off_t a = ((struct aaa*)p)->a;
 18     lseek(fd,0,SEEK_SET);
 19     for(int i=0;i<a;i++)
 20     {
 21         pthread_mutex_lock(&mutex);
 22         if(flag != 0)
 23         {
 24             pthread_cond_wait(&cond,&mutex);
 25         }
 26         int res = read(fd,&buf,1);
 27         if(res < 0)
 28         {
 29          ERR_MSG("read");
 30          return NULL;
 31         }
 32         flag = 1;
 33         pthread_cond_signal(&cond);
 34         pthread_mutex_unlock(&mutex);
 35     }
 36     pthread_exit(NULL);
 37 }
 38 
 39 void* zxc2(void* p)
 40 {
 41     int fd = ((struct aaa*)p)->fd;
 42         off_t a = ((struct aaa*)p)->a;
 43 
 44     for(int i=0;i<a;i++)
 45     {
 46         pthread_mutex_lock(&mutex);
 47         if(flag != 1)
 48         {
 49             pthread_cond_wait(&cond,&mutex);
 50         }
 51         printf("%c",buf);
 52         flag = 0;
 53         pthread_cond_signal(&cond);
 54         pthread_mutex_unlock(&mutex);
 55     }
 56     pthread_exit(NULL);
 57 }
 58 
 59 int main(int argc, const char *argv[])
 60 {
 61     int fd = open("a.c",O_RDONLY);
 62     if(fd < 0)
 63     {
 64      ERR_MSG("open");
 65      return -1;
 66     }
 67     off_t a = lseek(fd,0,SEEK_END);
 68     struct aaa p;
 69     p.fd = fd;
 70     p.a = a;
 71 
 72     pthread_mutex_init(&mutex,NULL);
 73     pthread_cond_init(&cond,NULL);
 74 
 75     pthread_t tid1;
 76     pthread_t tid2;
 77 
 78     if(pthread_create(&tid1,NULL,zxc1,(void*)&p) != 0)
 79     {
 80      printf("子线程1创建失败\n");
 81      return -1;
 82     }
 83     pthread_detach(tid1);
 84     if(pthread_create(&tid2,NULL,zxc2,(void*)&p) != 0)
 85     {
 86      printf("子线程2创建失败\n");
 87      return -1;
 88     }
 89 
 90     pthread_join(tid2,NULL);
 91 
 92     if(close(fd) < 0)
 93     {
 94      ERR_MSG("close");
 95      return -1;
 96     }
 97 
 98     pthread_mutex_destroy(&mutex);
 99     pthread_cond_destroy(&cond);
100     return 0;
101 }
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                                                                                                                                                                                                                                                 
~                                                                                            

3.xmind

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值