Hello, I'm working on some wacky shared memory using application for Unix.
Here is the thing, I use shmget and shmat to create and attach a shared memory block, but making it an array of structures.
The structure:
SHM declaration:Code:struct object{ char nick[16]; pid_t pid; key_t queue; } *mem, *p;
And now the problematic part. I'm using this loop to browse through this thing.Code:if( (mem = (struct object*)shmat((key_t)argv[1], (void*)0, 0)) == (void*)-1 ) { shmget((key_t)argv[1], SHMSIZE*sizeof(struct object), IPC_CREAT | 0666); mem = (struct object*)shmat( (key_t)argv[1], (void*)0 , 0); }
But this one just doesn't work. Any suggestions please?Code:for(p = mem; p< &mem[SHMSIZE]); p++) { if(!p->nick) { key = ftok(".", randomize()); //randomize just provides a random integer from dev/random msgid = msgget(key, IPC_CREAT); p->nick = getenv("USERNAME"); printf("%s", p->nick); p->pid = getpid(); p->queue = msgid; } printf("lalala\n"); }



LinkBack URL
About LinkBacks



