6月14日

1 #include <stdio.h>
2 #include <pthread.h>
3 
4 pthread_mutex_t mutex;
5 
6 
7 pthread_cond_t cond_a;
8 pthread_cond_t cond_b;
9 pthread_cond_t cond_c;
0 volatile int flag = 0;
1 
2 void* Breath_A(void* arg)
3 {
4     while(1)
5     {
6         //功能打印自己的编号
7         pthread_mutex_lock(&mutex);
8 
9         if(flag != 0)
0             pthread_cond_wait(&cond_a, &mutex);
1         printf("A\n");
2         flag = (flag+1) % 3;
3 
4         pthread_cond_signal(&cond_b);
5         pthread_mutex_unlock(&mutex);
6 
7 
8     }
9     return NULL;
0 }
1 
2 void* Breath_B(void* arg)
3 {
4     while(1)
5     {
6         //功能打印自己的编号
7         pthread_mutex_lock(&mutex);
8 
9         if(flag != 1)
0             pthread_cond_wait(&cond_b, &mutex);
1 
2         printf("B\n");
3         flag = (flag+1) % 3;
4 
5         pthread_cond_signal(&cond_c);
6         pthread_mutex_unlock(&mutex);
7 
8 
9     }
0     return NULL;
1 }
2 
3 void* Breath_C(void* arg)
4 {                                                                                                                                                                                                                                               
5     while(1)
6     {
7         //功能打印自己的编号
8         pthread_mutex_lock(&mutex);
9         if(flag != 2)
0             pthread_cond_wait(&cond_c, &mutex);
1 
2         printf("C\n");
3         static int m = 0;
4         m++;
5         printf("********%d*******\n",m);
6         flag = (flag + 1) % 3;
7 
8         pthread_cond_signal(&cond_a);
9         pthread_mutex_unlock(&mutex);
0 
1 
2     }
3     return NULL;
4 
5 }
6 
7 
8 
9 
0 int main(int argc, const char *argv[])
1 {
2 
3     //创建互斥锁
4     int pmi;
5     pmi = pthread_mutex_init(&mutex, NULL);
6     if(pmi != 0)
7     {
8         perror("pthread_mutex_init");
9         return -1;
0     }
1 
2     //申请条件变量
3     int pci_a = pthread_cond_init(&cond_a, NULL);
4     if(pci_a != 0)
5     {
6         perror("pthread_cond_init");
7         return -1;
8     }
9 
0     int pci_b = pthread_cond_init(&cond_b, NULL);
1     if(pci_a != 0)
2     {
3         perror("pthread_cond_init");
4         return -1;
5     }
6 
7     int pci_c = pthread_cond_init(&cond_c, NULL);
8     if(pci_a != 0)
9     {
0         perror("pthread_cond_init");
1         return -1;
2     }
3 
4 
5     //申请三个线程
6     pthread_t A, B, C;
7     int pa, pb, pc;
8     pa = pthread_create(&A, NULL, Breath_A, NULL);
9     if(pa != 0)
0     {
1         perror("pthread_create");
2         return -1;
3     }
4 
5     pb = pthread_create(&B, NULL, Breath_B, NULL);
6     if(pb != 0)
7     {
8         perror("pthread_create");
9         return -1;
0     }
1 
2     pc = pthread_create(&C, NULL, Breath_C, NULL);
3     if(pc != 0)
4     {
5         perror("pthread_create");
6         return -1;
7     }
8 
9 
0 
1     pthread_join(A, NULL);
2     pthread_mutex_destroy(&mutex);
3     pthread_cond_destroy(&cond_a);
4     pthread_cond_destroy(&cond_b);
5     pthread_cond_destroy(&cond_c);
6     return 0;
7 }
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                  
  #include <stdio.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <string.h>
  #include <sys/wait.h>
 
  int main(int argc, const char *argv[])
  {
      int pipefd[2];
      int pi = pipe(pipefd);
      if(pi != 0)
      {
          perror("pipe");
          return -1;
      }
 
      char str[256] = "";
 
      pid_t pid = fork();
      if(pid > 0)
      {
         //sleep(3);
          while(1)
          {
 
              ssize_t rd = read(pipefd[0], str, sizeof(str));
             puts("this is parent");
              if(rd < 0)
              {
                  perror("read");
                  return -1;
              }
 
              printf("len_str_parent = %ld\n",strlen(str));
              printf("str_parent = %s\n",str);
          }
          //sleep(1);
 
      }                                                                                             
      else if(0 == pid)
      {
          while(1)
          {
              printf("this is child\n");
              printf("str>>>>");
              fgets(str, sizeof(str), stdin);
              str[strlen(str)-1] = '\0';
 
              ssize_t wr = write(pipefd[1], str, sizeof(str));
              if(wr <= 0)
              {
                  perror("write");
                  return -1;
              }
              sleep(0.5);
 
          }
      }
      else
      {
          perror("fork");
          return -1;
      }
 
 
      pid_t wt = wait(NULL);
      if(wt < 0)
      {
          perror("wait");
          return -1;
      }
      printf("wt = %d\n",wt);
      return 0;
  }
 
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值