1 #include <my_head.h>
2
3 char buf[128]="";
4 ssize_t res=0;
5 pthread_mutex_t mutex;
6
7 void* callBack1(void* arg)
8 {
9 pthread_mutex_lock(&mutex);
10 while(1)
11 {
12 bzero(buf,sizeof(buf));
13 res=read(*(int*)arg,buf,sizeof(buf));
14 if(0==res)
15 break;
16 }
17 pthread_mutex_unlock(&mutex);
18 pthread_exit(NULL);
19 }
20
21 void* callBack2(void* arg)
22 {
23 char temp=0;
24 pthread_mutex_lock(&mutex);
25 while(1)
26 {
27 if(0==res)
28 break;
29 write(1,buf,res);
30
31 }
32 pthread_mutex_unlock(&mutex);
33 pthread_exit(NULL);
34 }
35
36
37 int main(int argc, const char *argv[])
38 {
39 int fd=open("./stat.c",O_RDONLY);
40 if(fd<0)
41 {
42 perror("open");
43 return -1;
44 }
45
46 pthread_mutex_init(&mutex,NULL);
47
48 pthread_t tid1,tid2;
49 if(pthread_create(&tid1,NULL,callBack1,(void*)&fd)!=0)
50 {
51 fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
52 return -1;
53 }
54 if(pthread_create(&tid2,NULL,callBack2,NULL))
55 {
56 fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
57 return -1;
58 }
59 pthread_join(tid1,NULL);
60 pthread_join(tid2,NULL);
61
62 pthread_mutex_destroy(&mutex);
63 close(fd);
64 return 0;
65 }
1 #include <my_head.h>
2 //临界资源
3 char ID[3] = "ABC";
4
5 //互斥锁
6 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
7 //创建条件变量
8 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
9
10 int flag = 0; //0: 打印 1: 倒置
11
12 void* callBack1(void* arg)
13 {
14 while(1)
15 {
16 //上锁
17 pthread_mutex_lock(&mutex);
18
19 if(flag != 0) //不是当前线程要访问的时机
20 {
21 //解开互斥锁,并设置一个唤醒条件
22 //等待被唤醒
23 pthread_cond_wait(&cond, &mutex);
24 //当被唤醒后,会立即尝试上锁
25 //上锁成功,则完全被唤醒,从当前位置继续往后执行
26 //上锁失败,重新回到cond上继续休眠,等待下一次唤醒。
27 }
28
29 /临界区///
30 printf("%c", ID[0]);
31 /临界区///
32
33 flag = 1; //修改访问时机
34 //通过指定的条件变量唤醒对方线程
35 pthread_cond_signal(&cond);
36
37 //解锁
38 pthread_mutex_unlock(&mutex);
39 }
40 pthread_exit(NULL);
41 }
42
43
44 void* callBack2(void* arg)
45 {
46 while(1)
47 {
48 //上锁
49 pthread_mutex_lock(&mutex);
50
51 if(flag != 1) //不是当前线程要访问的时机
52 {
53 //解开互斥锁,并设置一个唤醒条件
54 //等待被唤醒
55 pthread_cond_wait(&cond, &mutex);
56 //当被唤醒后,会立即尝试上锁
57 //上锁成功,则完全被唤醒,从当前位置继续往后执行
58 //上锁失败,重新回到cond上继续休眠,等待下一次唤醒。
59 }
60
61 /临界区///
62 printf("%c",ID[2]);
63 /临界区///
64
65 flag = 2; //修改访问时机
66 //通过指定的条件变量唤醒对方线程
67 pthread_cond_signal(&cond);
68
69 //解锁
70 pthread_mutex_unlock(&mutex);
71 }
72 pthread_exit(NULL);
73 }
74
75 void* callBack3(void* arg)
76 {
77 while(1)
78 {
79 //上锁
80 pthread_mutex_lock(&mutex);
81
82 if(flag != 2) //不是当前线程要访问的时机
83 {
84 //解开互斥锁,并设置一个唤醒条件
85 //等待被唤醒
86 pthread_cond_wait(&cond, &mutex);
87 //当被唤醒后,会立即尝试上锁
88 //上锁成功,则完全被唤醒,从当前位置继续往后执行
89 //上锁失败,重新回到cond上继续休眠,等待下一次唤醒。
90 }
91
92 /临界区///
93 printf("%c",ID[1]);
94 /临界区///
95
96 flag = 0; //修改访问时机
97 //通过指定的条件变量唤醒对方线程
98 pthread_cond_signal(&cond);
99
100 //解锁
101 pthread_mutex_unlock(&mutex);
102 }
103 pthread_exit(NULL);
104 }
105
106
107 int main(int argc, const char *argv[])
108 {
109 //创建并初始化一个互斥锁
110 //pthread_mutex_init(&mutex, NULL);
111
112 //创建条件变量
113 //pthread_cond_init(&cond, NULL);
114
115 pthread_t tid1, tid2,tid3;
116 if(pthread_create(&tid1, NULL, callBack1, NULL) != 0){
117 fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
118 return -1;
119 }
120
121 if(pthread_create(&tid2, NULL, callBack2, NULL) != 0){
122 fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
123 return -1;
124 }
125
126 if(pthread_create(&tid3, NULL, callBack3, NULL) != 0){
127 fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
128 return -1;
129 }
130
131
132 pthread_join(tid1, NULL);
133 pthread_join(tid2, NULL);
134 pthread_join(tid3, NULL);
135
136 //销毁互斥锁
137 pthread_mutex_destroy(&mutex);
138 //销毁条件变量
139 pthread_cond_destroy(&cond);
140
141 return 0;
142 }
1 #include"my_head.h"
2 sem_t sem;
3 char buf[]="1234567";
4
5 pthread_mutex_t mutex;
6
7 void* callBack1(void* arg)
8 {
9 while(1)
10 {
11 sem_wait(&sem);
12 printf("%s\n",buf);
13 sem_post(&sem);
14 }
15 pthread_exit(NULL);
16 }
17
18 void* callBack2(void* arg)
19 {
20 char temp=0;
21 while(1)
22 {
23 sem_wait(&sem);
24 for(int i=0;i<strlen(buf)/2;i++)
25 {
26 temp=buf[i];
27 buf[i]=buf[strlen(buf)-1-i];
28 buf[strlen(buf)-1-i]=temp;
29 }
30 printf("%s\n",buf);
31 sem_post(&sem);
32 }
33 pthread_exit(NULL);
34 }
35
36 int main(int argc, const char *argv[])
37 {
38 if(sem_init(&sem,0,1)<0)
39 {
40 perror("sem_init");
41 return -1;
42 }
43 pthread_t tid1,tid2;
44 if(pthread_create(&tid1,NULL,callBack1,NULL))
45 {
46 fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
47 return -1;
48 }
49 if(pthread_create(&tid2,NULL,callBack2,NULL))
50 {
51 fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
52 return -1;
53 }
54 pthread_join(tid1,NULL);
55 pthread_join(tid2,NULL);
56
57 sem_destroy(&sem);
58 return 0;
59 }