Fairy
Fairy

Reputation: 111

shared memory not updating

So I've been trying to learn a little more about shared memory. I am creating a shared memory structure in my master and forking off children. Each child is then creating a new process using execv. When I try to change the value of shared memory from the new process the parent does not see that change. Also I was expecting the parent to interleave execution with child. For Example

Child 1 exec 
parent 
child 2 exec 
parent 
child 3 exec
parent 

But Instead I get

child 1 exec 
child 2 exec 
child 3 exec 
parent
parent
parent

this is my code

simulate.c

# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include <stdbool.h>
# include <ctype.h>
# include <sys/wait.h>
# include <signal.h>
# include <sys/mman.h>
# include <sys/time.h>
# include <stdint.h>
# include <fcntl.h>
# include <sys/shm.h>
struct sharedregion{
    volatile int seconds;
    volatile int sharedmsg;
};
struct sharedregion *mystruct;
void spawnchild()
{
    int fd = shm_open("sharedmemory", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRWXG | S_IRWXU);
    if (fd == -1)
        perror("Error in opening");
    if (ftruncate(fd, sizeof(struct sharedregion)) == -1)
        perror("Unable to truncate");
    mystruct = mmap(NULL, sizeof(struct sharedregion),PROT_READ | PROT_WRITE, MAP_SHARED| MAP_ANONYMOUS, fd, 0);
    if (mystruct == MAP_FAILED)
        perror("mapping failed");
    mystruct->seconds=0;
    mystruct->sharedmsg=0;
    int i;
    for(i=0;i<4;i++)
    {
        pid_t pID = fork();

        if (pID == 0)
        {
            static char *args[]={"./child",NULL};

            execv(args[0], args);
            perror("failed to execv");
            exit(EXIT_FAILURE);

        }
        else if (pID < 0)            // failed to fork
        {
            perror(" Failed to fork:");
            exit(EXIT_FAILURE);
        }
        else if(pID>0)
        {

            printf("\nfrom parent= %d",mystruct->sharedmsg);
            mystruct->seconds=mystruct->seconds+1;
            if(mystruct->seconds==1000000000)
            {
                mystruct->seconds=1;
            }
        }
    }
}
int main(int argc, char **argv)
{
    spawnchild();
}

child.c

# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include <stdbool.h>
# include <ctype.h>
# include <sys/wait.h>
# include <signal.h>
# include <sys/mman.h>
# include <sys/time.h>
# include <stdint.h>
# include <fcntl.h>
# include <sys/shm.h>
struct sharedregion{
    volatile int seconds;
        volatile int sharedmsg;
};
struct sharedregion *mystruct;

int main(int argc, char **argv)
{
    int memFd = shm_open("sharedmemory", O_RDWR, S_IRUSR | S_IWUSR | S_IRWXG | S_IRWXU);
    if (memFd == -1)
    {
        perror("Can't open file");
        return 1;
    }

    mystruct = mmap(NULL, sizeof (struct sharedregion), PROT_READ | PROT_WRITE, MAP_SHARED| MAP_ANONYMOUS, memFd, 0);

    printf("\nhello from exec");
    mystruct->sharedmsg=8;
    printf("buffer %d",mystruct->sharedmsg);

    return 0;
}

My output

hello from execbuffer 8
hello from execbuffer 8
hello from execbuffer 8
hello from execbuffer 8
hello from execbuffer 8
from parent= 0
from parent= 0
from parent= 0
from parent= 0
from parent= 0

Upvotes: 1

Views: 1262

Answers (1)

OznOg
OznOg

Reputation: 4732

Remove the flag MAP_ANONYMOUS

according to the manual:

MAP_ANONYMOUS [...] The fd argument is ignored;

thus it ignores your shared memory

Upvotes: 2

Related Questions