1 #include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <semaphore.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9
10
11 void * A(void *argc)
12 {
13
14 printf("this is A...\n");
15 int path = open("./1.jpeg", O_RDONLY);
16 int path1= open("./2.jpeg",O_WRONLY | O_CREAT | O_TRUNC, 0777);
17 if(path1 < 0 || path < 0)
18 {
19 perror("open");
20 return NULL;
21 }
22 off_t res = lseek(path,0,SEEK_END);
23
24
25 pthread_mutex_lock((pthread_mutex_t *)argc);//上锁
26
27 lseek(path,0,SEEK_SET);
28 lseek(path1,0,SEEK_SET);
29 char picture;
30 for(int i =0;i<res/2;i++)
31 {
32 read(path,&picture,1);
33 write(path1,&picture,1);
34 }
35
36 pthread_mutex_unlock((pthread_mutex_t *)argc);//解锁
37
38
39 pthread_exit(NULL);
40 }
41 void * B(void *argc)
42 {
43 printf("this is B...\n");
44 int path = open("./1.jpeg", O_RDONLY);
45 int path1= open("./2.jpeg",O_WRONLY , 0777);
46 if(path1 < 0 || path < 0)
47 {
48 perror("open");
49 return NULL;
50 }
51 off_t res = lseek(path,0,SEEK_END);
52
53
54 pthread_mutex_lock((pthread_mutex_t *)argc);//上锁
55 lseek(path,res / 2,SEEK_SET);
56 lseek(path1,res / 2,SEEK_SET);
57 char picture;
58 for(int i =res / 2;i < res ; i++)
59 {
60 read(path,&picture,1);
61 write(path1,&picture,1);
62 }
63 pthread_mutex_unlock((pthread_mutex_t *)argc);//解锁
64
65
66 pthread_exit(NULL);
67
68 }
69
70 int main(int argc, const char *argv[])
71
72 {
73
74 //创建互斥锁
75 pthread_mutex_t mutex;
76 pthread_mutex_init(&mutex,NULL);//互斥锁初始化
77
78
79 pthread_t tid1;
80 if(pthread_create(&tid1,NULL,A,(void*)&mutex) < 0)
81 {
82 fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
83 return -1;
84 }
85
86 pthread_join(tid1,NULL);
87 pthread_t tid2;
88 if(pthread_create(&tid2,NULL,B,(void*)&mutex) < 0)
89 {
90 fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
91 return -1;
92 }
93
94 //while(1);
95 //pthread_detach(tid2);//分离线程
96
97 pthread_join(tid1,NULL);
98 pthread_mutex_destroy(&mutex);
99
100
101
102
103 return 0;
104 }
运行结果:
2.创建编号为ABC三个线程,三个线程循环打印自己的编号,要求打印出来的结果必须是ABC;
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <semaphore.h>
7
8 //创建互斥锁,并初始化
9 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
10 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
11 pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
12
13 //创建条件变量
14 //pthread_cond_t cond= PTHREAD_COND_INITIALIZER;
15 int flag = 0; //0:打印A 1:打印B 2:打印C
16 void * A(void *arg)
17 {
18 //printf("this is A\n");
19 pthread_mutex_lock(&mutex1);//上锁
20 if(flag == 0 )
21 {
22 //pthread_cond_wait(&cond,&mutex1);
23 printf("A\n");//打印字符A
24
25 flag = 1;
26 }
27 // printf("A\n");//打印字符A
28 // flag = 1;
29 // pthread_cond_signal(&cond);
30
31 pthread_mutex_unlock(&mutex1);//解锁
32 pthread_exit(NULL);
33 }
34 void * B(void *arg)
35 {
36 //printf("this is B\n");
37 pthread_mutex_lock(&mutex2);//上锁
38 if(flag == 1 )
39 {
40 //pthread_cond_wait(&cond,&mutex2);
41 printf("B\n");//打印字符B
42
43 flag = 2;
44 }
45 //printf("B\n");//打印字符B
46 //flag = 2;
47
48 //pthread_cond_signal(&cond);
49 pthread_mutex_unlock(&mutex2);//解锁
50 pthread_exit(NULL);
51 }
52
53 void * C(void *arg)
54 {
55 //printf("this is C\n");
56
57 pthread_mutex_lock(&mutex3);//上锁
58 if(flag == 2)
59 {
60 //pthread_cond_wait(&cond,&mutex3);
61 printf("C\n");//打印字符C
62
63 flag = 0;
64 }
65 //printf("C\n");//打印字符C
66 //flag = 0;
67
68 //pthread_cond_signal(&cond);
69 pthread_mutex_unlock(&mutex3);//解锁
70 pthread_exit(NULL);
71 }
72
73 int main(int argc, const char *argv[])
74 {
75
76
77
78 pthread_t tid1;
79 if(pthread_create(&tid1,NULL,A,NULL) < 0)
80 {
81 fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
82 return -1;
83 }
84 //pthread_join(tid1,NULL);//阻塞,防止主线程退出
85
86 pthread_t tid2;
87 if(pthread_create(&tid2,NULL,B,NULL) < 0)
88 {
89 fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
90 return -1;
91 }
92 //pthread_join(tid2,NULL);//阻塞,防止主线程退出
93
94 pthread_t tid3;
95 if(pthread_create(&tid3,NULL,C,NULL) < 0)
96 {
97 fprintf(stderr,"pthread_create fail __%d__\n",__LINE__);
98 return -1;
99 }
00
01 pthread_detach(tid1);//分离线程
02
03 pthread_join(tid1,NULL);//阻塞,防止主线程退出
04 //pthread_join(tid2,NULL);//阻塞,防止主线程退出
05
06 //pthread_join(tid3,NULL);//阻塞,防止主线程退出
07
08
09 pthread_mutex_destroy(&mutex1);//销毁互斥锁
10 pthread_mutex_destroy(&mutex2);//销毁互斥锁
11 pthread_mutex_destroy(&mutex3);//销毁互斥锁
12 //pthread_cond_destroy(&cond);//销毁条件变量
13
14
15
16
17 return 0;
18 }
运行结果:
3.创建无名管道,并进行读写
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <string.h>
5 int main(int argc, const char *argv[])
6 {
7 int pfd[2];
8 if(pipe(pfd)<0)
9 {
10 perror("pipe");
11 return -1;
12 }
13 printf("创建管道成功 %d %d\n",pfd[0],pfd[1]);
14 char arr[32]="";
15 pid_t pid =fork();
16 if(pid >0)
17 {
18 while(1)
19 {
20 fgets(arr,sizeof(arr),stdin);
21 arr[strlen(arr)-1]='\0';
22 if(write(pfd[1],arr,sizeof(arr))<0)
23 {
24 perror("write");
25 return -1;
26 }
27 printf("写入成功 %s %d \n",arr,__LINE__);
28 }
29 }
30 else if(pid == 0)
31 {
32 ssize_t size;
33 while(1)
34 {
35 size= read(pfd[0],arr,sizeof(arr));
36 return -1;
37 }
38 printf("写入成功 %s %d \n",arr,__LINE__);
39 }
40 else
41 {
42 perror("read");
43 return -1;
44 }
45
46 return 0;
47 }
运行结果