c内存共享

来源:互联网 发布:模块数据出现异常400 编辑:程序博客网 时间:2024/06/05 00:27

c内存共享

int segment_id = shmget (shm_key, getpagesize (),IPC_CREAT | S_IRUSR | S_IWUSER);  shared_memory = (char*) shmat (segment_id, 0, 0);  shmctl (segment_id, IPC_STAT, &shmbuffer);  int main(){          int shm_id;          int *share;          int num;          srand(time(NULL));          shm_id = shmget (1234, getpagesize(), IPC_CREAT);          if(shm_id == -1){              perror("shmget()");          }          share = (int *)shmat(shm_id, 0, 0);          while(1){              num = random() % 1000;              *share = num;              printf("write a random number %d\n", num);              sleep(1);          }          return 0;  }int main(){          int shm_id;          int *share;          shm_id = shmget (1234, getpagesize(), IPC_CREAT);          if(shm_id == -1){              perror("shmget()");          }          share = (int *)shmat(shm_id, 0, 0);          while(1){              sleep(1);              printf("%d\n", *share);          }          return 0;  }  
原创粉丝点击