Hello everyone, my program compiles and works well with everything my professor is asking for when using the necessary testing values but when i run my code in the autograder, every IO Test passes but the structural code fails and this is the error i get -- Exit code: 1.
stdout:
stderr: , here is my code :
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <errno.h>
#include <math.h>
long createSamples(unsigned wID,long nbSamples,double d)
{
long ttl = 0; // Number of events detected by a process
unsigned seed = wID; //seed for random number generation
for(long i = 0; i < nbSamples; i++) {
double x = (double)rand_r(&seed) / RAND_MAX;
double y = (double)rand_r(&seed) / RAND_MAX;
if(fabs(x - y) < d) {
ttl++;
}
}
return ttl;
}
int main(int argc,char* argv[])
{
if (argc < 4) {
printf("usage: rpair <d> <samples> <workers>\n");
exit(1);
}
float d = atof(argv[1]); // value of d
long s = atol(argv[2]); // total number of trials to be executed
int nbW = atoi(argv[3]); // number of worker processes
long ttl = 0; //total number of events
double p = 0; //Probability of the occurence of events
long *shared_memory = mmap(NULL, sizeof(long), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*shared_memory = 0;
long samples_per_worker = s / nbW;
for(int i = 0; i < nbW; i++) {
if(fork() == 0) {
*shared_memory += createSamples(i, samples_per_worker, d);
exit(0);
}
}
for(int i = 0; i < nbW; i++) {
wait(NULL);
}
ttl = *shared_memory;
p = (double)ttl / s;
printf("Total trials = %ld \t Total events = %ld \t Probability = %lf\n",s,ttl,p);
munmap(shared_memory, sizeof(long));
return 0;
}```