Hi everyone!
So my task is this: Make a program in C which runs three processes (A,B,C), which use a common memory (buffer). Process A generates 50 random numbers and writes then into the buffer. Process B writes down every even number into file F1. Process C writes down every odd number into file F2.
So far I have this code:
#include<fcntl.h>
#include<wait.h>
#include<stdio.h>
#include<unistd.h>
#include<time.h>
using namespace std;
extern char* getmem(int);
int main(){
int ID=0, nID, status=0;
int i;
short *a=(short*)getmem(3271);
FILE *f;
srand((unsigned)time(NULL));
// Defining the processes
cout<<"Starting process A...\n";
ID++;
nID=fork();
if(nID<0){
cout<<"Error!\n";
return 0;
}
else if(nID!=0){
wait(&status);
cout<<"Starting process B...\n";
ID++;
nID=fork();
if(nID<0){
cout<<"Error!\n";
return 0;
}
else if(nID!=0){
cout<<"Starting process C...\n";
ID++;
nID=fork();
if(nID<0){
cout<<"Error!\n";
return 0;
}
else if(nID!=0)
ID=0;
}
}
// The processes
if(ID==1)
for(i=0; i<50; i++){
*(a+i)=rand();
}
if(ID==2){
f=fopen("f1.txt", "w");
if(f==NULL)
cout<<"Error creating file!\n";
for(i=0; i<50; i++){
if((*(a+i)%2)==0)
fprintf(f, "%i\n", *(a+i));
}
fclose(f);
}
if(ID==3){
f=fopen("f2.txt", "w");
if(f==NULL)
cout<<"Error creating file!\n";
for(i=0; i<50; i++){
if((*(a+i)%2)!=0)
fprintf(f, "%i\n", *(a+i));
}
fclose(f);
}
wait(&status);
return 0;
}
But it gets me errors about declaration of srand(), cout, and rand(). In which libraries are they declared?