system calls in linux

This is a discussion on system calls in linux within the Linux Programming forums, part of the Platform Specific Boards category; I am working on a relational database project that involves tweaking around with the file storage.I need to load a ...

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    system calls in linux

    I am working on a relational database project that involves tweaking around with the file storage.I need to load a file representing a relation into memory. I thought using mmap() would help but I am not able to interpret the output. This is what I did :

    Code:
     
    int main() 
     {
       int fp;
       int sz=100;
       unsigned char *op;
       
       fp=open("file.c");
       
       op = mmap (NULL, sz, PROT_WRITE, MAP_FILE | MAP_SHARED, fp,0);
        
      printf ("mmap (out) returned %08lx\n", (long)op);
      
      return 0;
     }
    Now, the man pages say that the call to mmap() returns the pointer to the location where the mapping was placed. So, my questions are:
    1) is this the pointer to the location in main memory? and how do I use this pointer to perform any update operations.


    2)I got a seg fault when I tried to read the value stored in the pointer.
    3)Is mmap() the appropriate sys call to use here or is there any other call that does the job that I am looking for?
    In the middle of difficulty, lies opportunity

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Platform specific... Linux
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah, linux..
    should I post it in some linux forum ?
    Duh! Its still a C program
    In the middle of difficulty, lies opportunity

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Does your DBMS support "containers". You can map a DBMS relation to physical part of disk (in this case, the file)

  5. #5
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    no, we are trying to create a mini-relational database project from the scratch using C to support a set of operations.
    In the middle of difficulty, lies opportunity

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > 2)I got a seg fault when I tried to read the value stored in the pointer.
    Erm, maybe you need
    PROT_READ | PROT_WRITE

    > 3)Is mmap() the appropriate sys call to use here or is there any other call that does the job that I am looking for?
    Well that depends.
    I'd make sure everything worked properly just using fopen / fread / fwrite / fseek.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    >PROT_READ | PROT_WRITE
    yeah, I did that too. I still get the seg fault when I try to read the value stored.
    now that u have brought this up, fopen() also actually maps the file into memory right?though not in an explicit manner as mmap().
    and , a slightly different question, can I implement DMA through a C program? which system call will I be needing for that?
    In the middle of difficulty, lies opportunity

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    No, fopen doesn't map the file. stdio usually has a buffer that contains a given number of bytes, normally for Linux it is 4096. It isn't populated until your code calls one of the stdio read functions: fgets fread etc.

    You can malloc a buffer, using the filesize (+ 1) you got from a stat call, then call fread to read the whole file into a buffer yourself. fclose() the file. If you plan on writing, malloc more space than the filesize, and keep track of what you add to the buffer. Then fopen & fwrite back to the file when you're done. The disadvantage with this is that if the program bombs you lose changes up to the point where it is written back to disk, plus this is a single process only approach.

    mmap and mysync let multiple process play with the memory mapped file - BUT read the man pages because MAP_SHARED comes with a lot of "unspecified" behaviors you need to check out first.

  9. #9
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    I am afraid ,that still isnt working.
    I have specified PROT_WRITE . Also, with respect to difference in behaviour with different flaovurs, the man pages done elaborate much on that.
    And I get the seg-fault whether I specify the MAP_SHARED flag or not.

    Is there any restriction on the SIZE parameter? Should I make that equal to the block size?
    THis brings me to two more questions :

    1) How do I what the block size is for my machine?
    and more importantly ,
    2) How do I access a particluar block?
    In the middle of difficulty, lies opportunity

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > fp=open("file.c");
    Maybe because open itself also takes a bunch of other parameters as well.

    Meh, checking the return result would be good as well. You probably don't even open the file properly given the lack of correct parameters.

    Do you get warnings when you compile, or are you just ignoring them?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    HURRAY!!!! Its working!!!!!

    Code:
    #include	<fcntl.h>
    
    int main() 
     {
       int fp;
       
       unsigned char *op;
       
       fp=open("file.c",O_RDWR);
       
      op = mmap (NULL, sz, PROT_WRITE|PROT_READ, MAP_FILE | MAP_SHARED, fp,0);
        
      printf ("mmap (out) returned %08lx\n", (long)op);
      
      printf("%s \n",op);  
     return 0;
     
    }
    SALEM,Thanks for identifying the flaw. I have become a bit lazy and have stopped error checking in my programs. I needed that..


    Its printing out the entire file now..
    I still have a few issues regarding detecting the end of the file, but I think I can handle it from here

    Thanks a lot!!!
    In the middle of difficulty, lies opportunity

  12. #12
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    I realised that I still havent found the answer to these questions :
    what
    1) How do I what the block size is for my machine?
    2) How do I access a particluar block?

    Any idea what I should be doing?
    In the middle of difficulty, lies opportunity

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Well having mapped the file, accessing op[pos] is just like seek(pos),getch()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    no, I am not talking about memory here.. I am referring to blocks on my disk. I need a pointer to each block containing my data file for indexing purposes.
    In the middle of difficulty, lies opportunity

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    op is a pointer to memory

    If you want to do
    result = memcmp( &op[pos], reference, 100 );
    Then just do it.

    The disk is irrelevant at this point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiling linux file system
    By Hoshangi in forum Linux Programming
    Replies: 1
    Last Post: 08-13-2008, 10:35 PM
  2. system calls
    By kiai_viper in forum Networking/Device Communication
    Replies: 7
    Last Post: 06-13-2007, 11:34 AM
  3. System calls stopping my function ...
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 06-20-2006, 09:01 AM
  4. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21