ActiveState Code

Recipe 576799: shared memory


sharem mem

C
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  /* 共享内存记录pid列表, 组有读写执行的权利. 子进程只能读共享内存,确定其在列表中的位置  */
    pros_shm = shmget(IPC_PRIVATE, pros_num * sizeof(pid_t), IPC_CREAT | S_IRWXU | S_IRWXG);
    if( -1 == pros_shm){
        printf("unable to create shared memory %s\n", strerror(errno));
        return -1;
    }


   /* 挂载共享内存 */
   pros_mem = shmat(pros_shm, NULL, SHM_RND);
   if(-1 == (int)pros_mem ){
       printf("unable to attach shared memory at the parent process %s\n", strerror(errno));
       return -1;
   }

Comments

  1. 1. At 7:20 p.m. on 14 jul 2009, Robert Naiditch said:

    The style is kind of backward, the variable usually comes first in a comparison and the usage of -1 referred to as a "Magical Number" has not been acceptable in modern day ANSI C in over a decade. Other than that, it is straight forward though the usage of multiple exits (return -1) is harder to debug since -1 has no meaning and which if statement caused the exit from the module.

Sign in to comment