-
Hi all.
I am trying to create a program that will use a linked list as a queuing system where the linked list is stored in shared memory so that a second process can access it and delete elements in the list as neccessary.
The problem I am having is creating the linked list in the allocated shared memory.
below is my code so far if anyone could give me some pointers it would be much appreciated :)
Ben
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
# define SHMSZ 27
/************************************************************************/
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
void DisplayBits(unsigned value);
int MaskMain(unsigned int to_shift, unsigned int mask);
int main(void)
{
int shmid;
key_t key;
unsigned int *shm, *s;
/**************************************************************************/
/* Use an identifier for the shared memory segment */
key = 5678;
/* Create the segment */
if((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
_Exit(1);
}
printf("Segment created\n");
/* Now attach the segment to the data space */
if((shm = shmat(shmid,NULL,0)) == (unsigned int *)-1)
{
perror("shmat");
_Exit(1);
}
printf("Segment attached\n");
/**************************************************************************/
int i;
for(i=0;i<3;i++)
{
unsigned int shift = getpid();
printf("PID = %d\n", shift);
unsigned int mask = 7, send_to_list;
send_to_list = MaskMain(shift, mask);
insert(send_to_list);
display();
/*exit(1);*/
/* Now place some data into the memory block */
s = shm;
*s++ = send_to_list;
}
/**s =(unsigned int) NULL;*/
/* Now wait for other process to signal back that the
first character within the block has been changed
thus indicating that the block has been read */
while(*shm != (unsigned int)'*')
sleep(1);
/*Print out what is now in memory */
printf("%p\n",shm);
return 0;
}
int MaskMain(unsigned int to_shift, unsigned int mask)
{
unsigned int priority, after_shift, with_priority, view_priority;
printf("PID to add priority to is: ");
DisplayBits(to_shift);
priority = (rand() % 5 + 1);
printf("Priority is: ");
DisplayBits(priority);
after_shift = to_shift << 3;
printf("After left shift: ");
DisplayBits(after_shift);
with_priority = after_shift | priority;
printf("With priority inserted: ");
DisplayBits(with_priority);
view_priority = with_priority & mask;
printf("Read priority from result: ");
DisplayBits(view_priority);
return with_priority;
}
void DisplayBits(unsigned value)
{
unsigned c;
unsigned DisplayMask = 1 << 31;
printf("%7u = ", value);
for(c = 1; c <= 32; c++)
{
putchar(value & DisplayMask ? '1':'0');
value <<= 1;
if(c % 8 == 0)
{
putchar(' ');
}
}
putchar('\n');
}
int insert(unsigned int x)
{
struct node *tmp;
int added_item;
tmp = (struct node *)malloc(sizeof(struct node));
/* printf("Input the element for adding in queue : "); */
added_item = x;
tmp->info = added_item;
tmp->link=NULL;
if(front==NULL) /*If Queue is empty*/
front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/
int del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp=front;
printf("Deleted element is %d\n",tmp->info);
front=front->link;
free(tmp);
}
}/*End of del()*/
int display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("\n\nQueue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;
}
printf("\n");
}/*End of else*/
}/*End of display()*/
Also, Ignore the functions here, as I built up to this point I have been adding debugging functions to be able to see the data printed as I have gone along. del() and display() no longer used.
-
I haven't used shared memory functions myself, but I believe your problem is going to be this:
Code:
tmp = (struct node *)malloc(sizeof(struct node));
I believe you can't use standard allocation to allocate shared memory chunks. Basically, what you are allocating here with malloc isn't shared. You need like shmalloc or something. I'm not sure what it's called, and I'm too lazy to google it for you.
Quzah.
-
The code I have correctly allocated the shared memory and I can add data to it as a memory location which my other process can access and read from fine.
The problem I have is that I want the linked list in the code above to be allocated to the shared memory area itself. extensive googling and beej's guide to ipc hasn't helped me so far :(
-
IPC:Shared Memory Read.
You aren't going to be able to just malloc up some nodes and slap them together in a linked list. I think. I think you need to specifically attach (shmat) a head to the segment you allocate, treating it as if that was your malloc. Then, what, cast it as a node, and shmat on another segment, and set the first spot's appropriate pointer to that new spot. Repeat until done? Sounds good to me.
Quzah.
-
I've been using Dave's guide for a lot of the work so far. I'll give what you suggested a go, but is a bit over my head. Will see how it goes :)
Cheers for the help so far!
++ As you can see I was using Dave's server/client example as the base of my communication with shared memory as it is. Replaced his little for loop to send 'a' to 'z' between process'