Linux c shared memory between processes

  linux, question

If you want to share the handle of sqlite between two processes, program 1 has been running, trying to get program 2 to get the globalhandle by sharing variables. the total error is as follows. I don’t know where the error is.

Procedure 1

key_t id=ftok(path,0);
 if (id==-1)
 {
 /* code */
 perror("ftok error!  \n");
 }
 
 if((shmid=shmget(id,4096,IPC_CREAT|0777))==-1){
 perror("create shared memory error!  \n");
 }
 
 ret=sqlite3_open("123.db",&globalHandlex);
 sqlite3* handle = (sqlite3 *)shmat(shmid,0,0);
 handle =  globalHandlex;

Procedure 2

key_t id=ftok(path,0);
 if (id==-1)
 {
 /* code */
 perror("ftok error!  \n");
 }
 
 if((shmid=shmget(id,4096,IPC_CREAT|0777))==-1){
 perror("create shared memory error!  \n");
 }
 
 sqlite3* globalHandle=(sqlite3 *)shmat(shmid,0,0);
 fprintf(stderr, "globalHandle 3:%x\n", globalHandle);

You only shared one pointer ……sqlite3′ s data structure should be quite complicated.

YouDon’t say your real needsSo I don’t know how you should solve the problems you actually encounter.