LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2011, 04:07 PM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
how to make link list and add and delete list node in shared memory


hello
I wrote an example for practicing shared memory and adding an array node to an existing node in memory as follow:
Code:
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>


#define DEFAULT_PROTOCOL 0
typedef struct struarr
{
    int a;
    int b;
    struct struarr *next;

}struarr;
typedef struct stru1
{
    int index;
    struarr *arrstruct;
}stru1;

int main()
{
    stru1 *structptr;
    int i=0;
    int j=0;
    key_t key;
    key_t key2;
    int shmid;


    key=5454;
    key2=5555;

    shmid=shmget(key,sizeof(stru1),0666|IPC_CREAT);
    if (shmid<0)
    {
        perror("err");

    }
    structptr=(stru1*)shmat(shmid,(void*)0,0);
    if (structptr == (stru1*)(-1))
        perror("shmat1");

    if (fork()==0)
    {
        int b=0;

        structptr->index=4;
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");

        printf("passed\n");
        while (i<structptr->index)
        {

            structptr->arrstruct[i].a=b;
            b++;
            structptr->arrstruct[i].b=b;
            b++;
            i++;
        }
        j=0;
        while (j<structptr->index)
        {
            printf("a:%d\tb:%d\n",structptr->arrstruct[j].a,structptr->arrstruct[j].b);
            j++;
        }
        printf("passed 2\n");
        i=0;
        while (i<5)
        {
            i++;
            sleep(1);
            scanf("%d",&b);
            j=0;
            while (j<structptr->index)
            {
            sleep(1);
                structptr->arrstruct[j].a=b;
                b++;
                structptr->arrstruct[j].b=b;
                b++;
                j++;
            }

        }
        shmdt(structptr);
        shmctl(shmid,IPC_RMID,0);
    }


    sleep(2);
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");

    i=0;
    while(i<30)
    {
        i++;
        sleep(1);

        j=0;
        while (j<structptr->index)
        {
            printf("a:%d\tb:%d\n",structptr->arrstruct[j].a,structptr->arrstruct[j].b);
            j++;
        }
    }
    shmdt(structptr);
    shmctl(shmid,IPC_RMID,0);
    return 0;

}
it is working fine but as you see I add structptr->arrstruct node to first node of link list by adding this part of code to both parent and child:
Code:
        shmid=shmget(key2,(structptr->index*sizeof(struarr)),0666|IPC_CREAT);
        if (shmid<0)
        {
            perror("err2");

        }
        structptr->arrstruct=(struarr*)shmat(shmid,(void*)0,0);
        if (structptr->arrstruct == (struarr*)(-1))
            perror("shmat11");
but as you see in this example I know when to add structptr->arrstruct node in both processes but what about if one process want to add another node to link list on some situation that the other process does not know. I mean how can one process add share memory node to an existing link and the other processes use it just with the next pointer that they have;how can be one process as maker and the other as just reader but they have the first pointer in shared memory to next shared memory that maker process make them.
I tried to discuss it in best way.
thanks for any help.
 
Old 12-18-2011, 07:52 AM   #2
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
First thing that comes to mind:

You need to make "master process" that'll manipulate the list in shared memory. Child processes should issue list manipulation commands to master using whatever interprocess communication mechanism is available (pipes, sockets, signals, etc). Master should notify children when list changes using IPC. Children should not manipulate list directly. Only one process should manipulate the list (otherwise, there will be a lot of multithreaded synchronization problems).

Other solutions may exist.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'E:Malformed line 54 in source list /etc/apt/sources.list (dist parse), E:The list of vsssuccess@gmail.com Linux - Desktop 1 11-17-2010 09:17 AM
How to share a list of structure using in shared memory in linux IPC Guptarulz Programming 11 10-10-2009 12:31 AM
Not delete items in list twantrd Programming 14 05-08-2006 03:15 PM
Seg Fault in GCC when deleting Node from Linked List gren838 Programming 2 12-11-2005 05:21 PM
c++ list class, type of node? exodist Programming 2 05-20-2004 07:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration