Welcome, guest | Sign In | My Account | Store | Cart

sharem mem

C, 14 lines
 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;
   }

1 comment

Robert Naiditch 12 years, 4 months ago  # | flag

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.

Created by J Y on Tue, 9 Jun 2009 (MIT)
C recipes (32)
J Y's recipes (21)

Required Modules

  • (none specified)

Other Information and Tasks