How to use shared memory with Linux in C

前端 未结 5 1428
悲&欢浪女
悲&欢浪女 2020-11-22 08:13

I have a bit of an issue with one of my projects.

I have been trying to find a well documented example of using shared memory with fork() but to no suc

5条回答
  •  情话喂你
    2020-11-22 09:04

    These are includes for using shared memory

    #include
    #include
    
    int shmid;
    int shmkey = 12222;//u can choose it as your choice
    
    int main()
    {
      //now your main starting
      shmid = shmget(shmkey,1024,IPC_CREAT);
      // 1024 = your preferred size for share memory
      // IPC_CREAT  its a flag to create shared memory
    
      //now attach a memory to this share memory
      char *shmpointer = shmat(shmid,NULL);
    
      //do your work with the shared memory 
      //read -write will be done with the *shmppointer
      //after your work is done deattach the pointer
    
      shmdt(&shmpointer, NULL);
    

提交回复
热议问题