Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Memory and Pointers to structs

Status
Not open for further replies.

burnettben

Programmer
Apr 24, 2005
7
0
0
AU
I have allocated a piece of shared memory in a parent process and then forked to spawn 1 .. n children processes. I have 2 structs, one is a linked list which consists of a pointer to next and a pointer to the other struct (a player) which consists of a char array (name), and some other details. The player information is gained after the fork, but is required by by other processes.

My question is if i add a linked list node to the shared memory and then fork create a player and then attach my self to the shared memory, create a new node and then assign the player pointer to that node, does the player object have to be in common area of memory (another piece of shared memory for example) or can it be in the processes own memory space (ie just create a pointer and assign that pointer)?
 
Anything you do after a fork(), which you want others to see has to be in the shared memory.


--
 
IPC == inter process communication.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <assert.h>
#include <sys/wait.h>

#define BSZ 256
#define SM_NUM 122
#define SM_SZ  1000
#define destrShm(id) {shmctl(id,IPC_RMID,NULL);}
#define createShm(id,sz,mode) {shmid = shmget(id,sz,(IPC_CREAT | IPC_EXCL | mode));} 
#define attachShm(id,add) {add = shmat(id,NULL,0);}

static int shmid = -1;

struct a_tree {
int count;
char *data;
struct a_tree *one;
struct a_tree *two;
};

struct a_tree *newNode(struct a_tree *,char *);
void printTree(struct a_tree *);

int main(int argc, char **argv) {
int v, x = 10;
pid_t p;
char buf[BSZ];
void *attach;
struct a_tree *foo;
FILE *ff;
               if (argc < 2) {printf("Need filename..\n"); return -1;}
               ff = fopen(argv[1],"r");
               while ( (fgets(buf,BSZ,ff)) != NULL) {
		       foo = newNode(foo,buf);
	       }  
               createShm(SM_NUM,SM_SZ,0666);
               if (shmid < 0) {return -1;}
               attachShm(shmid,attach);
               attach = foo;
               while (x > 0) {
                             p = fork();
                             if (p == 0) {
                                         printf("In child : %d, from %d\n",getpid(),getppid());
                                         sleep(1);
                                         printTree(attach); 
                                         exit(0);
                             }
                             wait(NULL);
                             --x;
                }                        
               fclose(ff);
               destrShm(shmid);
               return 0;
}       

struct a_tree *newNode(struct a_tree *f_node,char *wrd) {
int i;

                        if (f_node == NULL) {
			    f_node = malloc(sizeof(struct a_tree));
		            assert(f_node);
			    f_node->data = strdup(wrd);
			    f_node->count = 1;
			    f_node->one = f_node->two = NULL;
			} else if ( (i = strcmp(wrd,f_node->data)) == 0) {
			    f_node->count++;
			} else if (i < 0) {
			    f_node->one = newNode(f_node->one, wrd);
			} else {
			    f_node->two = newNode(f_node->two,wrd);
			}
			return f_node;
}

void printTree(struct a_tree *pt) {

                if (pt != NULL) {
		                printTree(pt->one);
				printf("%4d %s\n",pt->count,pt->data);
				printTree(pt->two);
		 }
return;
}
Mostly borrowed from K&R C.
 
The problem you will get with shared memory is that you cannot use pointers. A pointer is an address which is local to the process. Pointers cannot be shared.

If you want to use a singly linked list, you will have to do it the very old fashioned way by using arrays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top