Thread: shmget problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    shmget problem

    Good evening,

    I'm trying to allocated share memory and i get the error

    sh.c: In function `main':
    sh.c:14: error: parse error before ';' token

    My program is like that :

    Code:
    #define SHMSZ 40;
    
    int main() {
    
    key_t key;
    int shmid;
    
    if((key = ftok("path of user", 0)) < 0){
    printf("\n%d Error in key\n",key);
    return 0;
    }
    
    printf("%d\n",key);
    
    
    shmid = shmget(key,SHMSZ,(IPC_CREAT | 0666));
    
    /* the error appears here inside shmget*/
    
    }
    What do i do wrong?
    Thanks, in advance!

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Where is shmget() declared?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Well, i have included the libraries :

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdio.h>
    #include<time.h>
    #include <unistd.h>

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well putting a ; on the end of your #define is usually the wrong thing to do.

    Your use expands to
    shmid = shmget(key,40;,(IPC_CREAT | 0666));
    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.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Unbelievable!
    This was the error and I have spent 1 and a half hour to see what was going wrong and it was only a ';'!
    Thanks very much!

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Divide a shared memory segment

    Goodd evening,

    I would like to ask you sthg as far as dividing a shared memory segment into
    8 buffers is concerned.

    e.g. in the following code , we have allocated the shared memory segment and we want now to divide it into 8 stacks which every stack is going to consist of 4 integers.(4 * 8 =32 integers)
    How do we do that?

    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdio.h>
    #include<time.h>
    #include <unistd.h>
    
    #define SHMSZ 8
    
    int main() {
    
    key_t key;
    int shmid;
    int *buf;
    
    if((key = ftok("path of user", 0)) < 0){
    printf("\n%d Error in key\n",key);
    return 0;
    }
    
    printf("%d\n",key);
    
    
    shmid = shmget(key,SHMSZ * sizeof(int),(IPC_CREAT | 0666));
    
    
    buf = shmat(shmid,NULL,0);
    
    
    }
    Thanks, in advance!

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Isn't that just taking a 32*sizeof(int) block of memory and dividing it into 8 ? Aka having one pointer point to the beginning of the block, another 4 integers further, and then another 4 integers further, and so on ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Quote Originally Posted by Happy_Reaper
    Isn't that just taking a 32*sizeof(int) block of memory and dividing it into 8 ? Aka having one pointer point to the beginning of the block, another 4 integers further, and then another 4 integers further, and so on ?
    Well,i need to have for every buffer 3 semaphores which are going to check if the buffer is full,empty or no other process tries to have access to this buffer,this means that i must have 8 * 3 = 24 semaphores.

    Do you have any idea how i can utilise that?

    Thanks, in advance!

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Right, you can do that in similar fashion, either in a seperate block or by making your original block bigger by 24 * sizeof(semaphore).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM