LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mmap problem (https://www.linuxquestions.org/questions/programming-9/mmap-problem-195791/)

os2 06-20-2004 07:58 PM

mmap problem
 
hi

i created a programme who use a library.
the library talk to a driver.

the library need to write in shared memory between the application and kernel.

i try to use mmap.

in my application, i do:

Code:

static const int _MAXMSGSIZE = 256;
static const int _MAXMSGNB = 256;
int fd;

static char *mapdev (const char *, int, int);

int main (int argc, char **argv)
{
        unsigned int offset, size, i;
        char *addr, *addr1, *maddr;

        if (argc != 4)
        {
                fprintf (stderr, "Usage: mapcmp dev1 offset pages\n");
                exit (1);
        }
        size = _MAXMSGSIZE * _MAXMSGNB;
       
        maddr = malloc(size);
       
        offset = 0;
       
        addr = mapdev (argv[1], offset, size);
       
        addr1 = addr;

       
        for (i = 0; i < size; i++){
                *addr1 = "a";
                addr1++;
        }
       
        addr1 = addr;
       
       
        for (i = 0; i < size; i++){
                printf("%c",*addr1);
                addr1++;
        }

        munmap(addr,size);
        close(fd);
       
        exit (0);
}

static char *mapdev (const char *dev, int offset, int size)
{
        char *addr;
        fd = open (dev, O_RDWR);

        printf("Va utiliser mmap1\n");
       
        if (fd < 0)
        {
                perror (dev);
                return (1);
        }
        printf("Va utiliser mmap2\n");
       
        addr = mmap (0, size, PROT_READ, MAP_PRIVATE, fd, offset);
        if (addr == MAP_FAILED)
        {
                perror (dev);
                return (1);
        }
        printf ("Mapped %s (%d @ %d) at 0x%p\n", dev, size, offset, addr);
        return (addr);
}

they are a problem when i do:

Code:

*addr1 = "a";
the machine freeze

any idea?

Dark_Helmet 06-20-2004 08:12 PM

I don't know if this will solve your problem, but you're trying to assign a string to a character.

*addr1 is the location of a single character in memory.
"a" is a string (not a single character; use single quotes to specify a character)

Try:
Code:

*addr1 = 'a';

infamous41md 06-20-2004 09:14 PM

when u say 'the machine freezes', do u mean the entire machine hangs and u have to reboot?? if so, can u plz tell me what device it is you are trying to open?

os2 06-21-2004 03:39 PM

no not need to reboot... i can kill the application

i got the same problem, Dark_Helmet with your solution

infamous41md 06-21-2004 05:24 PM

you are only specifying PROT_READ, and u r trying to write to the memory.


All times are GMT -5. The time now is 08:48 AM.