条件变量相关函数

 1 #include <head.h>
 2 //互斥锁
 3 pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
 4 //创建三个条件变量                                     
 5 pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;
 6 pthread_cond_t cond2=PTHREAD_COND_INITIALIZER;
 7 pthread_cond_t cond3=PTHREAD_COND_INITIALIZER;
 8 int flag=0;
 9 void* callback1(void* arg)
10 {
11     while(1)
12     {
13         pthread_mutex_lock(&mutex);
14         if(0!=flag)
15         {
16             pthread_cond_wait(&cond1,&mutex);
17         }
18 
19         printf("A");
20         flag=1;
21 
22         pthread_cond_signal(&cond2);
23 
24         pthread_mutex_unlock(&mutex);
25     }
26     pthread_exit(NULL);
27 }
28 void* callback2(void* arg)
29 {
30     while(1)                                     
31     {
32 
33         pthread_mutex_lock(&mutex);
34         if(1!=flag)
35         {
36             pthread_cond_wait(&cond2,&mutex);
37         }
38         printf("B");
39         flag=2;
40 
41         pthread_cond_signal(&cond3);
42 
43         pthread_mutex_unlock(&mutex);
44         }
45 
46         pthread_exit(NULL);
47 }
48 void* callback3(void* arg)
49 {
50     while(1)
51     {
52 
53         pthread_mutex_lock(&mutex);
54         if(2!=flag)                                 
55         {
56             pthread_cond_wait(&cond3,&mutex);
57         }
58 
59 
60        printf("C\n");
61         flag=0;
62         pthread_cond_signal(&cond1);
63 
64         pthread_mutex_unlock(&mutex);
65     }
66 
67         pthread_exit(NULL);
68 }
69 
70 
71 int main(int argc, const char *argv[])
72 {
73     pthread_t tid1,tid2,tid3;
74     if(pthread_create(&tid1,NULL,callback1,(void*)&mutex)!=0)
75     {
76         printf("fail %d\n",__LINE__);
77         return -1;
78     }
79     if(pthread_create(&tid2,NULL,callback2,(void*)&mutex)!=0)
80     {
81         printf("fail %d\n",__LINE__);
82         return -1;
83     }
84     if(pthread_create(&tid3,NULL,callback3,(void*)&mutex)!=0)
85     {
86         printf("fail %d\n",__LINE__);
87         return -1;                                              
88     }
89 
90     pthread_join(tid1,NULL);
91     pthread_join(tid2,NULL);
92     pthread_join(tid3,NULL);
93     pthread_mutex_destroy(&mutex);
94     pthread_cond_destroy(&cond1);
95     pthread_cond_destroy(&cond2);
96     pthread_cond_destroy(&cond3);
97     return 0;
98 }
 

 

 

 1 #include <head.h>
 2 int main(int argc, const char *argv[])
 3 {
 4     //创建两个无名管道
 5     int pfd1[2];
 6     if(pipe(pfd1)<0)
 7     {
 8         perror("pipe");
 9         return -1;
10     }
11     printf("this is sucess pfd1[0]=%d pfd1[1]=%d\n",pfd1[0],pfd1[1]);
12     int pfd2[2];
13     if(pipe(pfd2)<0)
14     {
15         perror("pipe");
16         return -1;
17     }
18     printf("this is sucess pfd2[0]=%d pfd2[1]=%d\n",pfd2[0],pfd2[1]);
19 
20     pid_t pid=fork();
21 
22     char buf[128]="";
23     ssize_t res = 0;
24     if(pid>0)
25     {
26         while(1)
27         {
28             //从终端获取数据
29             fgets(buf,sizeof(buf),stdin);
30             buf[strlen(buf)-1] = '\0';                                 
31 
32             if(strcmp(buf,"quit")==0)
33             {
34                 break;
35             }
36                                                               
37             printf("父进程说>>>\n");
38             if((res=write(pfd1[1],buf,sizeof(buf)))<0)
39             {
40                 perror("write");
41                 return -1;
42             }
43 
44 
45             //接受子进程的
46             bzero(buf,sizeof(buf));
47             res = read(pfd2[0],buf,sizeof(buf));
48 
49             if(res<0)
50             {
51                 perror("read");
52                 return -1;
53             }
54             if(strcmp(buf,"quit")==0)
55             {
56                 break;
57             }
58 
59             printf("父进程听到的>>>%s\n",buf);
60 
61         }
62     }
 63     else if(pid == 0)
 64     {                                                            
 65         while(1)
 66         {   
 67             bzero(buf,sizeof(buf));
 68             res = read(pfd1[0],buf,sizeof(buf));
 69  
 70             if(res<0)
 71             {
 72                 perror("read");
 73                 return -1;
 74             }
 75 
 76             if(strcmp(buf,"quit")==0)
 77             {
 78                 break;
 79             }
 80 
 81             printf("子进程听到的>>>%s\n",buf);
 82 
 83             //从终端获取数据
 84             fgets(buf,sizeof(buf),stdin);
 85             buf[strlen(buf)-1] = '\0';
 86 
 87             //发送给父进程
 88             printf("子进程说\n");
 89             if((res=write(pfd2[1],buf,sizeof(buf)))<0)
 90             {
 91                 perror("write");
 92                 return -1;
 93             }
 94             if(strcmp(buf,"quit")==0)
 95             {
 96                 break;
 97             }
 98 
 99         }
100     }
101     else
102     {                                                    
103         perror("fork");
104         return -1;
105     }
106     close(pfd1[0]);
107     close(pfd1[1]);
108     close(pfd2[0]);
109     close(pfd2[1]);
110     return 0;
111 }
                                                        

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值