Pergunta

I have a C application in which I have a variable whose value continually changes. I need to read this value in my Java application as it changes.

I have created a shared memory segment using "shmget()" in my C program, so I have the key to access the shared memory.

How can I access this shared memory using that key in Java code?

The C code is as follows

char myc;
int myshmid;
key_t mykey;
char *myshm,*mys;

int main(int argc, char *argv[], char *envp[])
{
  mykey=5678;
  if((myshmid=shmget(mykey,SHMSZ,IPC_CREAT | 0666))<0)
  {
    perror("shmget");
    exit(1);
  }

  if((myshm=shmat(myshmid,NULL,0))==(char *) -1)
  {
    perror("shmat");
    exit(1);
  }

  mys=myshm;
Foi útil?

Solução

You can't. You will need to communicate with your Java code in a different way.

You could use JNI if your C and Java code can run in the same process. Otherwise, use sockets or something similar.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top