Segmentation fault mit der Posix-C-Programm mit mmap und mapfile

Nun habe ich dieses Programm und ich bekomme einen segmentation fault: 11 (core dumped). Nach vielen Prüfungen, die ich bekommen Sie diese, wenn die for-Schleife wird i=1024 und es versucht zu mapfile[i]=0. Das Programm wird über ein server-und einem client-Programm kommuniziert durch Lesen/schreiben in einer gemeinsamen Datei in das server-Programm. Dies ist das server-Programm und es druckt den Wert im inneren vor und nach der änderung. Ich möchte sehen, was passiert, wenn es ein problem mit dem mapping oder einfach nur problem mit Speicher der *mapfile. Danke!

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

int main()
{
    int ret, i;           
    int *mapfile;           

    system("dd if=/dev/zero of=/tmp/c4 bs=4 count=500");

    ret = open("/tmp/c4", O_RDWR | (mode_t)0600);
    if (ret == -1)
    {
        perror("File");
        return 0;
    }

    mapfile = mmap(NULL, 2000, PROT_READ | PROT_WRITE, MAP_SHARED, ret, 0);

    for (i=1; i<=2000; i++)
    {
        mapfile[i] = 0;
    }

    while(mapfile[0] != 555)
    {
        mapfile = mmap(NULL, 2000, PROT_READ | PROT_WRITE, MAP_SHARED, ret, 0);
        if (mapfile[0] != 0)
        {
            printf("Readed from file /tmp/c4 (before): %d\n", mapfile[0]);
            mapfile[0]=mapfile[0]+5;
            printf("Readed from file /tmp/c4 (after)  : %d\n", mapfile[0]);
            mapfile[0] = 0;
        }
        sleep(1);
    }

    ret = munmap(mapfile, 2000);
    if (ret == -1)
    {
        perror("munmap");
        return 0;
    }

    close(ret);

    return 0;
}
InformationsquelleAutor STRATOSpeed | 2014-05-09
Schreibe einen Kommentar