IO day9

1.

  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 pthread_t tid1;
  6 pthread_t tid2;
  7 struct msgbuf
  8 {
  9     long mtype;
 10     char mtext[128];
 11 };
 12 void *zxc1(void *p)
 13 {
 14     int msqid = *(int*)p;
 15     struct msgbuf msg1;
 16     msg1.mtype = 1;
 17     while(1)
 18     {
 19         bzero(msg1.mtext,sizeof(msg1.mtext));
 20         printf("请输入>>>\n");
 21         scanf("%s",msg1.mtext);
 22         if(msgsnd(msqid,&msg1,sizeof(msg1.mtext),IPC_NOWAIT) < 0)
 23         {
 24             ERR_MSG("msgsnd");
 25             return NULL;
 26         }
 27         if(strcmp(msg1.mtext,"quit") == 0)
 28         {
 29             return NULL;
 30         }
 31     }
 32 }
 33 
 34 void *zxc2(void* p)
 35 {
 36     int msqid = *(int*)p;
 37     struct msgbuf msg2;
 38     msg2.mtype = 2;
 39     while(1)
 40     {
 41         bzero(msg2.mtext,sizeof(msg2.mtext));
 42         if(msgrcv(msqid,&msg2,sizeof(msg2.mtext),2,0) < 0)
 43         {
 44             ERR_MSG("msgrcv");
 45             return NULL;
 46         }
 47         if(strcmp(msg2.mtext,"quit") == 0)
 48         {
 49             if(msgctl(msqid,IPC_RMID,NULL) < 0)
 50             {
 51              ERR_MSG("msgctl");
 52              return NULL;
 53             }
 54             exit(0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 55         }
 56         printf("%s\n",msg2.mtext);
 57     }
 58 }
 59 int main(int argc, const char *argv[])
 60 {
 61     key_t key = ftok("./",1);
 62     if(key < 0)
 63     {
 64      ERR_MSG("ftok");
 65      return -1;
 66     }
 67 
 68     int msqid = msgget(key,IPC_CREAT|0664);
 69     if(msqid < 0)
 70     {
 71      ERR_MSG("msgget");
 72      return -1;
 73     }
 74 
 75     if(pthread_create(&tid1,NULL,zxc1,(void*)&msqid) != 0)
 76     {
 77      printf("子线程1创建失败\n");
 78      return -1;
 79     }
 80 
 81     if(pthread_create(&tid2,NULL,zxc2,(void*)&msqid) != 0)
 82     {
 83      printf("子线程2创建失败\n");
 84      return -1;
 85     }
 86 
 87     pthread_join(tid1,NULL);
 88 
 89     return 0;
 90 }
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 pthread_t tid1;
  6 pthread_t tid2;
  7 struct msgbuf
  8 {
  9     long mtype;
 10     char mtext[128];
 11 };
 12 void *zxc1(void *p)
 13 {
 14     int msqid = *(int*)p;
 15     struct msgbuf msg2;
 16     msg2.mtype = 2;
 17     while(1)
 18     {
 19         bzero(msg2.mtext,sizeof(msg2.mtext));
 20         printf("请输入>>>\n");
 21         scanf("%s",msg2.mtext);
 22         if(msgsnd(msqid,&msg2,sizeof(msg2.mtext),IPC_NOWAIT) < 0)
 23         {
 24             ERR_MSG("msgsnd");
 25             return NULL;
 26         }
 27         if(strcmp(msg2.mtext,"quit") == 0)
 28         {
 29             return NULL;
 30         }
 31     }
 32 }
 33 
 34 void *zxc2(void* p)
 35 {
 36     int msqid = *(int*)p;
 37     struct msgbuf msg1;
 38     msg1.mtype = 1;
 39     while(1)
 40     {
 41         bzero(msg1.mtext,sizeof(msg1.mtext));
 42         if(msgrcv(msqid,&msg1,sizeof(msg1.mtext),1,0) < 0)
 43         {
 44             ERR_MSG("msgrcv");
 45             return NULL;
 46         }
 47         if(strcmp(msg1.mtext,"quit") == 0)
 48         {
 49             if(msgctl(msqid,IPC_RMID,NULL) < 0)
 50             {
 51              ERR_MSG("msgctl");
 52              return NULL;
 53             }
 54             exit(0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 55         }
 56         printf("%s\n",msg1.mtext);
 57     }
 58 }
 59 int main(int argc, const char *argv[])
 60 {
 61     key_t key = ftok("./",1);
 62     if(key < 0)
 63     {
 64      ERR_MSG("ftok");
 65      return -1;
 66     }
 67 
 68     int msqid = msgget(key,IPC_CREAT|0664);
 69     if(msqid < 0)
 70     {
 71      ERR_MSG("msgget");
 72      return -1;
 73     }
 74 
 75     if(pthread_create(&tid1,NULL,zxc1,(void*)&msqid) != 0)
 76     {
 77      printf("子线程1创建失败\n");
 78      return -1;
 79     }
 80 
 81     if(pthread_create(&tid2,NULL,zxc2,(void*)&msqid) != 0)
 82     {
 83      printf("子线程2创建失败\n");
 84      return -1;
 85     }
 86 
 87     pthread_join(tid1,NULL);
 88 
 89     return 0;
 90 }
~                                                                    

2.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 int main(int argc, const char *argv[])
  6 {
  7     key_t key = ftok("./",1);
  8     if(key < 0)
  9     {
 10      ERR_MSG("ftok");
 11      return -1;
 12     }
 13 
 14     int shmid = shmget(key,32,IPC_CREAT|0664);
 15     if(shmid < 0)
 16     {
 17      ERR_MSG("shmget");
 18      return -1;
 19     }
 20 
 21     void* addr = shmat(shmid,NULL,0);
 22     int *flag = (int*)addr;
 23     *flag = 0;
 24     char *p = (char*)addr+4;
 25     strcpy(p,"1234567");
 26 
 27     while(1)
 28     {                                                                                                                                                                                                     
 29         if(*flag == 0)
 30         {
 31             printf("%s\n",p);
 32             sleep(1);
 33             *flag = 1;
 34         }
 35     }
 36 
 37     return 0;
 38 }
~                                    
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <head.h>
  5 int main(int argc, const char *argv[])
  6 {
  7     key_t key = ftok("./",1);
  8     if(key < 0)
  9     {
 10         ERR_MSG("ftok");
 11         return -1;
 12     }
 13 
 14     int shmid = shmget(key,32,IPC_CREAT|0664);
 15     if(shmid < 0)
 16     {
 17         ERR_MSG("shmget");
 18         return -1;
 19     }
 20 
 21     void* addr = shmat(shmid,NULL,0);
 22     int *flag = (int*)addr;
 23     char *p = (char*)addr+4;
 24     int len = strlen(p);                                                                                                                                                                                  
 25     while(1)
 26     {
 27         if(*flag == 1)
 28         {
 29             for(int i=0,j=len-1;i<j;i++,j--)
 30             {
 31                 char a = *(p+i);
 32                 *(p+i) = *(p+j);
 33                 *(p+j) = a;
 34             }
 35             printf("逆置完成 %s\n",p);
 36             *flag = 0;
 37         }
 38     }
 39 
 40     return 0;
 41 }

3.xmind

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值