#shared virtual memory

14 messages · Page 1 of 1 (latest)

green tinsel
#

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;
}```
bold spearBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

timber vessel
green tinsel
#

which from my understanding it means

#

the structure test is looking for keywords in your source code to make sure you're implementing your solution as specified in the instructions
so if it fails, it's not finding the right word in your source code

timber vessel
#

I would be surprised if they did that, however auto graders do all sorts of nonsense

green tinsel
#

im not sure if it is something to do with what I do after I am done with the shared memory?

timber vessel
#

You'll have to consult the guidelines you were given to understand what you need to do better if that is the case

green tinsel
#

nvm i ended up figuring it out from some lecture sludes

#

slides*

#

!solved

bold spearBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

timber vessel