ICP shared memory

Discussion in 'C' started by cavemandave, Nov 20, 2007.

Thread Status:
Not open for further replies.
  1. cavemandave

    cavemandave New Member

    Joined:
    Nov 20, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
    Last edited by a moderator: Nov 27, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice