C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-10-2009, 05:48 AM   #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
strider1974 is offline   Reply With Quote
Old 08-10-2009, 10:31 AM   #2
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
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.
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP
Zlatko is offline   Reply With Quote
Old 08-14-2009, 05:34 AM   #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
strider1974 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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