Hi friends,
This is a small program built on the concept of shared memory. The producer is a separate program and process, and the consumer is a seperate program and process. Both are executed under the same user account. The producer takes some string from the user and adds that string to the shared memory, and the consumer reads that string. You can see my code here.
$ cat producer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#define MEM_SZ 4096
struct shared_use_st
{
int written_by_you;
char some_text[MEM_SZ];
};
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[MEM_SZ];
int shmid;
shmid = shmget((key_t)1234, MEM_SZ, 0666 | IPC_CREAT);
if(shmid == -1)
{
printf("shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1)
{
printf("\nshmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %x\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
while(running)
{
while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf("\nwaiting for client...\n");
}
printf("Enter some text: ");
fgets(buffer, MEM_SZ, stdin);
strcpy(shared_stuff->some_text, buffer);
shared_stuff->written_by_you = 1;
if(strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
if(shmdt(shared_memory) == -1)
{
printf("shmdt failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
$ cat consumer.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define MEM_SZ 4096
struct shared_use_st
{
int written_by_you;
char some_text[MEM_SZ];
};
int main()
{
int running = 1;
void *shared_memory=(void *)0;
struct shared_use_st *shared_stuff;
int shmid;
srand((unsigned int)getpid());
shmid = shmget((key_t)1234, MEM_SZ, 0666 | IPC_CREAT);
if(shmid ==-1)
{
printf("shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1)
{
printf("\nshmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %x\n", (int)shared_memory);
shared_stuff=(struct shared_use_st *)shared_memory;
shared_stuff->written_by_you = 0;
while(running)
{
if(shared_stuff->written_by_you)
{
printf("You wrote: %s", shared_stuff->some_text);
sleep( rand()%4);
shared_stuff->written_by_you = 0;
if(strncmp(shared_stuff->some_text, "end", 3)==0)
{
running = 0;
}
}
}
if(shmdt(shared_memory)==-1)
{
printf("shmdt failed\n");
exit(EXIT_FAILURE);
}
if(shmctl(shmid, IPC_RMID, 0)==-1)
{
printf("shmctl(IPC_RMID)failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
Now my question and confusion is.
1) Both producer and consumer create structures and shared memory spaces
2) Both are separate programs and seperate processes.
3) How do they know that a common memory space is shared between them, I mean we don't tell the consumer which part of memory is created by the producer, nor the producer is aware of the consumer's created memory.
4) Then how come the producer produces some string and the consumer gets that string.
Could you please throw some light on it. And when I change the first argument of shmget to
IPC_PRIVATE
, the program doesn't work then.
Could you please help me on this.
Looking forward to your kind replies.
Thanks in advance!