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.