The program I wrote today will use a shared memory, which is created and initialized by another process. I attach the memory to my process space, but an error was reported when CALLING shmget

  1. Int shmid = shmget((key_t)key,size,0) int shmid = shmget((key_t)key,size,0); if(shmid == -1 ) { perror("shmget error:"); return NULL; } else {return shmat (shmid, 0, 0); }Copy the code

    \

Errno = 22 Invalid argument Then I find the problem, spend a lot of time to find the problem, finally restart the machine, after my thinking and search, I think there are two reasons for the cause. 1. When the program is compiled, the open shared memory must be closed first, that is, before compiling the program with shared memory, the terminal must use the command ipcrm to remove the used share memery, because the program that created the shared memory is already running, my program must attch the shared memory, so it is compiled. Errors may result. The size of the program that created the shared memory is smaller than the size of the program that used the shared memory. Although shmid is the same, it is larger than the size of the program that created the shared memory. This must be a problem. Since my program structure has 2 more structures than the one used to create the program, this will result in invalid arguments for the program. \

I wrote these two points in order to remember the cost of looking up the code for half a day and post them on the blog for everyone to share, so as to avoid repeating my old path and wasting a lot of time on unnecessary mistakes.

The specific usage is summarized as follows:

1. Display all IPC facilities

# ipcs -a

2, Display all Message queues

# ipcs -q

Display all semaphores

# ipcs -s

4. Display all shared memory

# ipcs -m

5. Display the details of IPC facilities

# ipcs -q -i id

Id corresponds to shmid, semID, and msgid. -q Indicates the type of the corresponding facility (queue). Use -s to view semaphore details. Use -m to view shared memory usage.

6, display IPC facilities limit size

# ipcs -m -l

-m Indicates the type of the facility. The options are -q, -m, and -s.

7. Display the permission relationship of IPC facilities

# ipcs -c

# ipcs -m -c

# ipcs -q -c

# ipcs -s -c

8. Display the ID of the process that accessed the IPC facility recently.

# ipcs -p

# ipcs -m -p

# ipcs -q -p

9. Display the last operation time of IPC facilities

# ipcs -t

# ipcs -q -t

# ipcs -m -t

# ipcs -s -t

10. Display the current status of IPC facilities

# ipcs -u

The ipcs command on Linux does not support the -b and -o commands on UNIX, and UNIX does not support the -l and -u commands. Therefore, you need to pay attention to this problem when writing cross-platform scripts.

[root@localhost ~]# ipcs -a


------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    


------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 196608     root       600        524288     2          dest         
0x00000000 229377     root       600        4194304    2          dest         
0x00000000 327682     root       600        4194304    2          dest         
0x6f000af7 622595     root       666        204000     0                       
0x00000000 458756     root       600        393216     2          dest         
0x51006ce5 491525     root       600        1024       1                       
0x00000000 524294     root       600        5674396    2          dest         
0x00000000 557063     root       600        4194304    2          dest         


------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x510014fb 65536      root       600        1         
0x0b000af7 98305      root       666        1         


[root@localhost ~]# ipcrm -m 622595
Copy the code


\