实践题目
事先编辑好数据文件1.dat和2.dat,假设它们的内容分别为1 2 3 4 5 6 7 8 9 10和 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 , 设计一个程序,在这个程序中一共有3个线程,其中两个线程负责从文件读取数据到公共的缓冲区,另外一个线程从缓冲区读取数据作不同的处理(加和乘运算)。
具体要求:
• 线程1从1.dat将数据读文件读到buf1中;
• 线程2从2.dat 将数据读到buf2中;
• 当buf1,buf2有数据时,线程3将buf1和buf2的结果相加和乘法处理,并将结果显示出来;
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
int buf1[10];
int buf2[10];
//read through two threads
// process and print through one thread
sem_t s1; //control write in buf1
sem_t s2;
sem_t s3; //control read from buf1
sem_t s4;
//read from 1.dat,and put in buf1
void ReadDat1()
{
FILE *fp=fopen("1.dat","r"); // 读方式打开文件1.dat
int i=0;
sem_wait(&s1); //s1=0控制写入buf1
while( !feof(fp)