码迷,mamicode.com
首页 > 编程语言 > 详细

c语言坑之printf缓冲区

时间:2017-05-06 00:59:50      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:targe   opened   分享   sleep   取数据   tar   define   semaphore   行缓冲   

技术分享
 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <semaphore.h>
 4 #include <unistd.h>
 5 #define MAXSTACK 100
 6 int stack[MAXSTACK][2];
 7 int size = 0;
 8 sem_t sem;
 9 /* 从文件1.dat读取数据,每读一次,信号量加一*/
10 void* ReadData1(void*)
11 {
12     FILE *fp = fopen("1.dat", "r");
13     if (fp == NULL) 
14     {
15         printf("open 1.dat failed!");
16         return NULL;
17     }
18     while (!feof(fp))
19     {
20         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
21         sem_post(&sem);
22         ++size;
23     }
24     fclose(fp);
25 }
26 /*从文件2.dat读取数据*/
27 void* ReadData2(void*)
28 {
29     FILE *fp = fopen("2.dat", "r");
30     if (fp == NULL) 
31     {
32         printf("open 2.dat failed!");
33         return NULL;
34     }
35     while (!feof(fp))
36     {
37         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
38         sem_post(&sem);
39         ++size;
40     }
41     fclose(fp);
42 }
43 /*阻塞等待缓冲区有数据,读取数据后,释放空间,继续等待*/
44 void *HandleData1(void *)
45 {
46     while (1)
47     {
48         if (sem_trywait(&sem)<0) continue;
49         printf("Plus:%d+%d=%dn", stack[size][0], stack[size][1],stack[size][0] + stack[size][1]);
50         --size;
51     }
52 }
53 
54 void *HandleData2(void*)
55 {
56     while (1)
57     {
58         sem_wait(&sem);
59         printf("Multiply:%d*%d=%dn", stack[size][0], stack[size][1],stack[size][0] * stack[size][1]);
60         --size;
61     }
62 }
63 int main(void)
64 {
65     pthread_t t1, t2, t3, t4;
66     sem_init(&sem, 0, 0);
67     pthread_create(&t1, NULL, HandleData1, NULL);
68     pthread_create(&t2, NULL, HandleData2, NULL);
69     pthread_create(&t3, NULL, ReadData1, NULL);
70     pthread_create(&t4, NULL, ReadData2, NULL);
71     //usleep(1000);
72     //fflush(stdout);
73     /* 防止程序过早退出,让它在此无限期等待*/
74     pthread_join(t1, NULL);
75 }
printf没输出
技术分享
 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <semaphore.h>
 4 #include <unistd.h>
 5 #define MAXSTACK 100
 6 int stack[MAXSTACK][2];
 7 int size = 0;
 8 sem_t sem;
 9 /* 从文件1.dat读取数据,每读一次,信号量加一*/
10 void* ReadData1(void*)
11 {
12     FILE *fp = fopen("1.dat", "r");
13     if (fp == NULL) 
14     {
15         printf("open 1.dat failed!");
16         return NULL;
17     }
18     while (!feof(fp))
19     {
20         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
21         sem_post(&sem);
22         ++size;
23     }
24     fclose(fp);
25 }
26 /*从文件2.dat读取数据*/
27 void* ReadData2(void*)
28 {
29     FILE *fp = fopen("2.dat", "r");
30     if (fp == NULL) 
31     {
32         printf("open 2.dat failed!");
33         return NULL;
34     }
35     while (!feof(fp))
36     {
37         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
38         sem_post(&sem);
39         ++size;
40     }
41     fclose(fp);
42 }
43 /*阻塞等待缓冲区有数据,读取数据后,释放空间,继续等待*/
44 void *HandleData1(void *)
45 {
46     while (1)
47     {
48         //if (sem_trywait(&sem)<0) continue;
49         sem_wait(&sem);
50         printf("Plus:%d+%d=%d\n", stack[size][0], stack[size][1],stack[size][0] + stack[size][1]);
51         --size;
52     }
53 }
54 
55 void *HandleData2(void*)
56 {
57     while (1)
58     {
59         sem_wait(&sem);
60         printf("Multiply:%d*%d=%d\n", stack[size][0], stack[size][1],stack[size][0] * stack[size][1]);
61         --size;
62     }
63 }
64 int main(void)
65 {
66     pthread_t t1, t2, t3, t4;
67     sem_init(&sem, 0, 0);
68     pthread_create(&t1, NULL, HandleData1, NULL);
69     pthread_create(&t2, NULL, HandleData2, NULL);
70     pthread_create(&t3, NULL, ReadData1, NULL);
71     pthread_create(&t4, NULL, ReadData2, NULL);
72     //usleep(1000);
73     //fflush(stdout);
74     /* 防止程序过早退出,让它在此无限期等待*/
75     pthread_join(t1, NULL);
76 }
printf有输出(\n)
技术分享
 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <semaphore.h>
 4 #include <unistd.h>
 5 #define MAXSTACK 100
 6 int stack[MAXSTACK][2];
 7 int size = 0;
 8 sem_t sem;
 9 /* 从文件1.dat读取数据,每读一次,信号量加一*/
10 void* ReadData1(void*)
11 {
12     FILE *fp = fopen("1.dat", "r");
13     if (fp == NULL) 
14     {
15         printf("open 1.dat failed!");
16         return NULL;
17     }
18     while (!feof(fp))
19     {
20         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
21         sem_post(&sem);
22         ++size;
23     }
24     fclose(fp);
25 }
26 /*从文件2.dat读取数据*/
27 void* ReadData2(void*)
28 {
29     FILE *fp = fopen("2.dat", "r");
30     if (fp == NULL) 
31     {
32         printf("open 2.dat failed!");
33         return NULL;
34     }
35     while (!feof(fp))
36     {
37         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
38         sem_post(&sem);
39         ++size;
40     }
41     fclose(fp);
42 }
43 /*阻塞等待缓冲区有数据,读取数据后,释放空间,继续等待*/
44 void *HandleData1(void *)
45 {
46     while (1)
47     {
48         //if (sem_trywait(&sem)<0) continue;
49         sem_wait(&sem);
50         printf("Plus:%d+%d=%d\n", stack[size][0], stack[size][1],stack[size][0] + stack[size][1]);
51         --size;
52     }
53 }
54 
55 void *HandleData2(void*)
56 {
57     while (1)
58     {
59         sem_wait(&sem);
60         printf("Multiply:%d*%d=%d\n", stack[size][0], stack[size][1],stack[size][0] * stack[size][1]);
61         --size;
62     }
63 }
64 int main(void)
65 {
66     pthread_t t1, t2, t3, t4;
67     sem_init(&sem, 0, 0);
68     pthread_create(&t1, NULL, HandleData1, NULL);
69     pthread_create(&t2, NULL, HandleData2, NULL);
70     pthread_create(&t3, NULL, ReadData1, NULL);
71     pthread_create(&t4, NULL, ReadData2, NULL);
72     //usleep(1000);
73     //fflush(stdout);
74     /* 防止程序过早退出,让它在此无限期等待*/
75     pthread_join(t1, NULL);
76 }
printf有输出(main正常结束)
技术分享
 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <semaphore.h>
 4 #include <unistd.h>
 5 #define MAXSTACK 100
 6 int stack[MAXSTACK][2];
 7 int size = 0;
 8 sem_t sem;
 9 /* 从文件1.dat读取数据,每读一次,信号量加一*/
10 void* ReadData1(void*)
11 {
12     FILE *fp = fopen("1.dat", "r");
13     if (fp == NULL) 
14     {
15         printf("open 1.dat failed!");
16         return NULL;
17     }
18     while (!feof(fp))
19     {
20         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
21         sem_post(&sem);
22         ++size;
23     }
24     fclose(fp);
25 }
26 /*从文件2.dat读取数据*/
27 void* ReadData2(void*)
28 {
29     FILE *fp = fopen("2.dat", "r");
30     if (fp == NULL) 
31     {
32         printf("open 2.dat failed!");
33         return NULL;
34     }
35     while (!feof(fp))
36     {
37         fscanf(fp, "%d%d", &stack[size][0], &stack[size][1]);
38         sem_post(&sem);
39         ++size;
40     }
41     fclose(fp);
42 }
43 /*阻塞等待缓冲区有数据,读取数据后,释放空间,继续等待*/
44 void *HandleData1(void *)
45 {
46     while (1)
47     {
48         //if (sem_trywait(&sem)<0) continue;
49         sem_wait(&sem);
50         printf("Plus:%d+%d=%dn", stack[size][0], stack[size][1],stack[size][0] + stack[size][1]);
51         --size;
52     }
53 }
54 
55 void *HandleData2(void*)
56 {
57     while (1)
58     {
59         sem_wait(&sem);
60         printf("Multiply:%d*%d=%dn", stack[size][0], stack[size][1],stack[size][0] * stack[size][1]);
61         --size;
62     }
63 }
64 int main(void)
65 {
66     pthread_t t1, t2, t3, t4;
67     sem_init(&sem, 0, 0);
68     pthread_create(&t1, NULL, HandleData1, NULL);
69     pthread_create(&t2, NULL, HandleData2, NULL);
70     pthread_create(&t3, NULL, ReadData1, NULL);
71     pthread_create(&t4, NULL, ReadData2, NULL);
72     usleep(1000);
73     fflush(stdout);
74     /* 防止程序过早退出,让它在此无限期等待*/
75     pthread_join(t1, NULL);
76 }
printf有输出(fflush)

 printf行缓冲区的分析总结

 

c语言坑之printf缓冲区

标签:targe   opened   分享   sleep   取数据   tar   define   semaphore   行缓冲   

原文地址:http://www.cnblogs.com/cdyboke/p/6815424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!