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.