Help with shared memory and semaphores

This is a discussion on Help with shared memory and semaphores within the C Programming forums, part of the General Programming Boards category; I have to write a program that solves the producer/consumer problem using shared memory, cicular buffer, mutex's and semaphores. My ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    Help with shared memory and semaphores

    I have to write a program that solves the producer/consumer problem using shared memory, cicular buffer, mutex's and semaphores.

    My problem is that while the course teaches us the theory behind these structures I have been given nothing on how to implement these in C.

    I have been looking at a number of different places on the web trying to wrap my head around how to use these in C but I am still having major issues.

    I am asking if anyone could point me to some reference materials that clearly and simply outline how to create shared memory, semaphores, mutex's, circular buffers etc. preferably using pthreads

    thank you

    Michael

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Hello strider.

    I assume you're using linux and that the system V interprocess communication (IPC) mechanism are OK to use. You could also use posix shared memory and posix semaphores.

    You don't really need shared memory or semaphores if the producer and consumer are threads in the same process (as you imply), but I understand that it may be a requirement for your assignment.

    The functions you're looking for are
    shmget, shmat, and shmdt for shared memory. You can read the man pages for them.
    Here is a simple example of creating shared memory.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    typedef struct 
    {
        int x;
    } WorkUnit;
    
    WorkUnit* pShm;
    
    int main(void)
    {
        int shmid;
        pid_t child;
    
        key_t key = 0x1234;
        // Or use ftok function
        
        if ((shmid = shmget(key, sizeof(WorkUnit)*10, IPC_CREAT | 0666)) < 0)
        {
    	perror("shmget");
    	return 1;
        }
        
    
        if ((pShm = (WorkUnit*)shmat(shmid, NULL, 0)) == (void*)-1)
        {
    	perror("shmat");
    	return -1;
        }
    
        /* Do some work then detach from shared memory */
    
        shmdt((void*)pShm);
    
        return 0;
    For semaphores, you need the semget, and semop functions. I guess you'll be using a binary semaphore to get exclusive access to your shared memory. If you're using producer and consumer threads, then you should use pthread mutex objects instead of semaphores but you could use semaphores as an exercise.

    I hope that starts you off. You really need to specify more information about your problem.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Zlato

    cheers, this got headed in the right direction even though my post was lacking in details

    Sorry for the late reply it has been a hell of week

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  2. problem with sending array of struct over shared memory
    By jet-plane in forum C Programming
    Replies: 26
    Last Post: 05-10-2008, 04:10 AM
  3. shared memory & mutex
    By lollobrigido in forum Linux Programming
    Replies: 3
    Last Post: 02-22-2008, 03:34 PM
  4. reader/writer locks on shared mem using semaphores
    By gibsosmat in forum C Programming
    Replies: 15
    Last Post: 11-21-2007, 10:47 PM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

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