Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

shmget function error

807578Jun 18 2002 — edited Jun 19 2002
I am experimenting with the shared memory programming to pass information between two processes. I have a small example program to demonstrate how it is done but unfortunately it does not work properly and I get an error message when I run it. The error message I get is
shmget: Invalid argument
The program compiles without any errors. I am using a sparcstation 20, running solaris 2.6 and using sun workshop c compiler version 4.2.
Does anyone have any suggestions as to why this is happening?
I enclose the program below for your review./*
* 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);
}


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 17 2002
Added on Jun 18 2002
5 comments
1,084 views