Thread: ICP shared memory

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    ICP shared memory

    hi people i have been teaching myself C for the last few months and have recently moved onto ICP shared memory. I have with aid developed some simple code that prints out some chars i then wanted to do the same with int but cannont seem to get it to work.


    producer

    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdio.h>
    
    #define SHMSZ     27
    
    int main()
    {
        int c;
        int shmid;
        key_t key;
        int *shm, *s;
    
        key = 5678;
    
    
        if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
            perror("shmget");
            return(1);
        }
    
    
        if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
            perror("shmat");
            return(1);
        }
    
        s = shm;
    
        for (c = 10; c <= 20; c++)
            *s++ = c;
        *s = -1;
    
    
        while (*shm != '*')
            sleep(1);
    
        return(0);
    }
    consumer

    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdio.h>
    
    #define SHMSZ     27
    
    int main()
    {
        int shmid;
        key_t key;
        char *shm, *s;
    
    
        key = 5678;
    
    
        if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
            perror("shmget");
            return(1);
        }
    
    
        if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
            perror("shmat");
            return(1);
        }
    
    
        for (s = shm; *s != 0; s++)
            putchar(*s);
        putchar('\n');
    
    
        *shm = '*';
    
        return(0);
    }
    any ideas?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "Cannot make it work" in what sens?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  3. Shared memory woes
    By joshdick in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 08:59 AM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM