首页 > 代码库 > c语言坑之printf缓冲区
c语言坑之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 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 }
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 }
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 }
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行缓冲区的分析总结
c语言坑之printf缓冲区
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。