-1

The goal of this code is to create a shared memory space and write n's value to it in the child then print all the numbers generated in from the parent process. But this presently just prints out memory addresses like 16481443B4 which change every time I run the program. I am not sure if I am writing to the shared memory incorrectly or reading from the shared memory incorrectly. Possibly both.

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <sys/mman.h>

int main(int argc, char** argv){

    //shared memory file descriptor
    int shm_fd;

    //pointer to shared memory obj
    void *ptr;
    //name of shared obj space
    const char* name = "COLLATZ";

    //size of the space
    const int SIZE = 4096;

    //create a shared memory obj
    shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);

    //config size
    ftruncate(shm_fd, SIZE);

    //memory map the shared memory obj
    ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);

    int n = atoi(argv[1]);
    pid_t id = fork();

    if(id == 0) {
        while(n>1) {
            sprintf(ptr, "%d",n);
            ptr += sizeof(n);
            if (n % 2 == 0) {
                n = n/2;
            } else {
                n = 3 * n + 1;
            }
        }
        sprintf(ptr,"%d",n);
        ptr += sizeof(n);
    } else {
        wait(NULL);
        printf("%d\n",(int*)ptr);
    }

    //Umap the obj
    munmap(ptr, SIZE);

    //close shared memory space
    close(shm_fd);

    return 0;
}
Tim
  • 4,790
  • 4
  • 33
  • 41
  • At what point do you check for errors? – spectras Sep 11 '17 at 22:49
  • I don't have any error checking code I can assume the input is always correct – Tori Swartz Sep 11 '17 at 23:09
  • 1
    Says who? Every single call you use can fail. `shm_open` may return `-1`. So may `ftruncate`. `mmap` may return `MAP_FAILED`. `fork` can return `-1` as well. You **must** check for error conditions and act upon them (for instance print the error code from `errno`, eg with [`perror`](https://linux.die.net/man/3/perror) and quit). – spectras Sep 11 '17 at 23:11

1 Answers1

1

Listen to your compiler!

$ gcc main.c -lrt
main.c: In function 'main':
main.c:51:9: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
  printf("%d\n",(int*)ptr);
         ^

Assuming you want to print the integer pointed to by ptr, it should be:

printf("%d\n",*((int*)ptr));
Tim
  • 4,790
  • 4
  • 33
  • 41
  • That's a beginning, but there are so many things wrong in the posted sample. void pointer arithmetic? incorrect system call arguments? lack of error checking? misunderstanding of string representation? Just from a quick look without even attempting to run the code. – spectras Sep 11 '17 at 23:28
  • @spectras Yes, there are lots of things wrong with the code, but this will fix the immediate problem of printing out the raw pointer value. – Tim Sep 11 '17 at 23:34