I am having problems with the shmget function. The code compiles correctly, but I get the error "shmget: Invalid argument" when I execute it. I am compiling on a Sparcstation 20 running solaris 2.6 and using the sun workshop c compiler v4.2.
Does anyone have any idea why this is failing?
I include the code for my small program below:
/*
* shm_server.c - Program to test passing information between
* processes using shared memory
*
* Creates the string and shared memory portion.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
/* #define SHMSZ 27 */
main()
{
char c;
int shmid;
int msgflg = IPC_CREAT | 0666;
key_t key;
size_t size;
char *shm;
char *s;
/*
* We will name our shared memory segment
* "5678".
*/
key = 5678;
size = 27;
/*
* Create the segment.
*/
printf("shmget( %i, %i, %o)\n", key, size, msgflg);
/* shmid = shmget(key, SHMSZ, msgflg); */
shmid = shmget(key , size, IPC_CREAT | 0666);
if ( shmid < 0 )
{
perror("shmget");
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ( (shm = shmat(shmid, NULL, 0)) == (char *) -1 )
{
perror("shmat");
exit(1);
}
/*
* Now we put some things into the memory for the
* other process to read.
*/
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
/*
* Finally, we wait until the other process
* changes the first character of our memory
* to '*', indicating that it has read what
* we put there.
*/
while (*shm != '*')
sleep(1);
exit(0);
}