Thread: Shared memory in Linux: B-TREE of structures

  1. #1
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Smile Shared memory in Linux: B-TREE of structures

    Hello,
    Anyone worked with shared memory in linux? I want to share a B-TREE of structures.

    Does anyone have any idea?
    Root or one user can give access to his/her data in memory for others.

    Can you provide me a sample of few lines? Just two node of a simple structure.
    What will be the client program, and what change need in server program.

    Server:
    #include<stdio.h>
    int main()
    {
    struct NAME {
    char name[30];
    int id;
    struct NAME *top;
    struct NAME *down;
    } *tm, *hd;

    tm=(struct NAME *)malloc(sizeof(struct NAME));
    tm->top=NULL;
    strcpy(tm->name,"Zahid Hossain");
    tm->id=1999;
    hd=tm;

    tm=(struct NAME *)malloc(sizeof(struct NAME));
    tm->top=hd;
    tm->down=NULL;

    strcpy(tm->name,"Glynn Clements");
    tm->id=2001;
    hd->down=tm;

    while(1)
    {
    printf("%s %d\n",hd->name, hd->id);
    printf("[email protected] %s %d\n",hd->down->name, hd->down->id);
    sleep(12);
    }
    }
    Note: Once I have tried for string but did not work for structure or BTREE. Or I don't know how to implement it. Last time xmdvp helped me on the problem.
    Last edited by zahid; 01-07-2003 at 11:25 PM.

  2. #2
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Alternative of shared memory.

    Is there any alternative of shared memory?
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    3
    I've never worked with a B-tree before, but I have worked with shared memory. On linux, pretty much you only option is SYSV shared memory (any one know of a distro supporting posix shared memory?) Any way here is code for a shared memory server and client. The server prints out a shared memory id to the screen, which you have to use as the first argument to the client. The server then asks for a message in a loop, and writes your message to shared memory. You can than use the client to access what message was written to shared memory.

    I did not remove the shared memory when the server exits, so you have to use ipcrm -m <shmid> to remove the shared memory.

    see the man pages for shmop, and shmget for more info.

    shmserver.c
    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <errno.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main (int argc, char ** argv) {
      int shmid;
      void * mem;
      char buffer [1024];
    
      /* get the shared memory (handle) */
      if ((shmid = shmget(IPC_PRIVATE, sizeof(buffer), IPC_CREAT|0666)) == -1) {
        fprintf(stderr, "Error [%d]: shmget failed - %s.\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
      }
      printf("shmid: [%d]\n", shmid);
    
      /* attach the memory to an address */
      if ((mem = shmat(shmid, NULL, SHM_W)) == (void*) -1) {
        fprintf(stderr, "Error [%d]: shmat failed - %s.\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      while (1) {
        printf("Enter Message: ");
        if (!fgets(buffer, sizeof(buffer), stdin) || !strncmp(buffer, "exit", 4)) {
          break;
        }
        strncpy(mem, buffer, sizeof(buffer));
      }
    
      if (shmdt(mem) == -1) {
        fprintf(stderr, "Error [%d]: shmdt failed - %s.\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      return EXIT_SUCCESS;
    }
    shmclient.c
    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <errno.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main (int argc, char ** argv) {
      int shmid;
      void * mem;
    
      if (argc != 2) {
        fprintf(stderr, "Usage: %s <shmid>\n", argv[0]);
        exit(EXIT_FAILURE);
      }
    
      shmid = atoi(argv[1]);
    
      /* attach the memory to an address */
      if ((mem = shmat(shmid, NULL, SHM_R)) == (void*) -1) {
        fprintf(stderr, "Error [%d]: shmat failed - %s.\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      printf("Read from shared memory:\n[%s]\n", mem);
    
      if (shmdt(mem) == -1) {
        fprintf(stderr, "Error [%d]: shmdt failed - %s.\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
      }
    
      return EXIT_SUCCESS;
    }
    Running ther server

    $ shmserver
    shmid: [792]
    Enter Message: hello shared memory!
    Enter Message: maybey i should finish fixing the olstores table and send a report to my boss
    Enter Message: or work on my robocode tank
    Enter Message: exit


    Running the client

    $ shmclient 792
    Read from shared memory:
    [hello shared memory!
    ]
    [jabba@/users/dp/bryanc/shm]
    $ shmclient 792
    Read from shared memory:
    [maybey i should finish fixing the olstores table and send a report to my boss
    ]
    [jabba@/users/dp/bryanc/shm]
    $ shmclient 792
    Read from shared memory:
    [or work on my robocode tank
    ]


    Listing and removing the shared memory (by the way im running on Solaris 8 sparc, should be similar though.

    [jabba@/users/dp/bryanc/shm]
    $ ipcs -m | grep bryanc
    m 792 0 --rw-rw-rw- bryanc dp
    [jabba@/users/dp/bryanc/shm]
    $ ipcrm -m 792

  4. #4
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Your code is working..

    I can do this .. it's okay. But I have an application which uses B-Tree And I want to make abailable the data on the memory for all other of read or if necessary for write.

    Now, for the implementation of the B-Tree where the required memory space is not predefined seems little complex to me.

    I was asking for the help or any idea of implementing.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partly shared memory
    By DrSnuggles in forum C++ Programming
    Replies: 13
    Last Post: 01-21-2009, 03:35 AM
  2. How do I make shared libs on Linux?
    By cpjust in forum C Programming
    Replies: 13
    Last Post: 11-05-2007, 04:54 PM
  3. malloc for tree structures
    By Tumbuler in forum C Programming
    Replies: 1
    Last Post: 10-31-2007, 06:43 PM
  4. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 02:29 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM