Linux:IPC
在Linux中与IPC相关的命令有三个:
ipcs,查看;
ipcrm,移除;
ipcmk,创建;
IPCS。
ipcs的默认输出方式与“ipcs -a”的输出是一样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x2de6c51d 753664 oracle 644 4096 0 0x2164d04d 786433 oracle 644 4096 0 0x2b8a7132 524290 root 644 20480000 0 ------ Semaphore Arrays -------- key semid owner perms nsems 0x49aea4f6 262144 oracle 644 152 0x3583b26e 294913 oracle 644 152 0xf8e5e05c 327682 oracle 644 152 [root@oel71 ~]# |
上面结果集中的三个部分也是可以单独输出的,具体如下:
ipcs -q,输出共享内存中的消息队列的情况。
ipcs -m,输出共享内存中的进程间通讯的情况。
ipcs -s,输出共享内存中使用信号进行进程间通讯的情况。
Shell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@oel71 ~]# ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages [root@oel71 ~]# ipcs -m ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x2de6c51d 753664 oracle 644 4096 0 0x2164d04d 786433 oracle 644 4096 0 0x2b8a7132 524290 root 644 20480000 0 [root@oel71 ~]# ipcs -s ------ Semaphore Arrays -------- key semid owner perms nsems 0x49aea4f6 262144 oracle 644 152 0x3583b26e 294913 oracle 644 152 0xf8e5e05c 327682 oracle 644 152 [root@oel71 ~]# |
在上面的结果集中,需要关注每一部分的前两列:Key和id。
ipcrm,用于删除的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
[root@oel71 ~]# man ipcrm IPCRM(1) User Commands IPCRM(1) NAME ipcrm - remove a message queue, semaphore set or shared memory id SYNOPSIS ipcrm [options] ipcrm {shm|msg|sem} id... DESCRIPTION ipcrm removes System V interprocess communication (IPC) objects and associated data structures from the system. In order to delete such objects, you must be superuser, or the creator or owner of the object. System V IPC objects are of three types: shared memory, message queues, and semaphores. Deletion of a message queue or semaphore object is immediate (regardless of whether any process still holds an IPC identifier for the object). A shared memory object is only removed after all currently attached processes have detached (shmdt(2)) the object from their virtual address space. Two syntax styles are supported. The old Linux historical syntax specifies a three letter keyword indicating which class of object is to be deleted, followed by one or more IPC identifiers for objects of this type. The SUS-compliant syntax allows the specification of zero or more objects of all three types in a single command line, with objects speci- fied either by key or by identifier. (See below.) Both keys and identifiers may be specified in decimal, hexadecimal (specified with an initial '0x' or '0X'), or octal (specified with an initial '0'). OPTIONS -M, --shmem-key shmkey removes the shared memorysegment created with shmkey after the last detach is performed. -m, --shmem-id shmid removes the shared memory segment identified by shmid after the last detach is performed. -Q, --queue-key msgkey removes the message queue created with msgkey. -q, --queue-id msgid removes the message queue identified by msgid. -S, --semaphore-key semkey removes the semaphore created with semkey. -s, --semaphore-id semid removes the semaphore identified by semid. -a, --all [shm msg sem] Remove all resources. When option argument is provided the removal is performed only to for the specified resource types. Warning! Do not use -a if you are unsure how the software using resources might react on missing objects. Some programs create these resources at start up and may not have any code to deal unexpected disappearance. The details of the removes are described in msgctl(2), shmctl(2), and semctl(2). The identifiers and keys may be found by using ipcs(1). NOTES In its first Linux implementation, ipcrm used the deprecated syntax shown in the SYNOPSIS. Functionality present in other *nix implementa- tions of ipcrm has since been added, namely the ability to delete resources by key (not just identifier), and to respect the same command- line syntax. For backward compatibility the previous syntax is still supported. SEE ALSO ipcs(1), ipcmk(1), msgctl(2), msgget(2), semctl(2), semget(2), shmctl(2), shmdt(2), shmget(2), ftok(3) AVAILABILITY The ipcrm command is part of the util-linux package and is available from Linux Kernel Archive ?ftp://ftp.kernel.org/pub/linux/utils/util- linux/?. util-linux September 2011 IPCRM(1) [root@oel71 ~]# |
你可以选择全部删除:ipcrm -a。
或者指定不同类型(三种类型)的唯一:Key或者ID。
具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
[root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x2de6c51d 753664 oracle 644 4096 0 0x2164d04d 786433 oracle 644 4096 0 0x2b8a7132 524290 root 644 20480000 0 ------ Semaphore Arrays -------- key semid owner perms nsems 0x49aea4f6 262144 oracle 644 152 0x3583b26e 294913 oracle 644 152 0xf8e5e05c 327682 oracle 644 152 [root@oel71 ~]# [root@oel71 ~]# ipcrm -M 0x2de6c51d [root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x2164d04d 786433 oracle 644 4096 0 0x2b8a7132 524290 root 644 20480000 0 ------ Semaphore Arrays -------- key semid owner perms nsems 0x49aea4f6 262144 oracle 644 152 0x3583b26e 294913 oracle 644 152 0xf8e5e05c 327682 oracle 644 152 [root@oel71 ~]# [root@oel71 ~]# ipcrm -m 524290 [root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x2164d04d 786433 oracle 644 4096 0 ------ Semaphore Arrays -------- key semid owner perms nsems 0x49aea4f6 262144 oracle 644 152 0x3583b26e 294913 oracle 644 152 0xf8e5e05c 327682 oracle 644 152 [root@oel71 ~]# [root@oel71 ~]# ipcrm -a [root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status ------ Semaphore Arrays -------- key semid owner perms nsems [root@oel71 ~]# |
IPCMK,用于创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
[root@oel71 ~]# man ipcmk IPCMK(1) User Commands IPCMK(1) NAME ipcmk - create various ipc resources SYNOPSIS ipcmk <resource options> [additional options] DESCRIPTION ipcmk allows you to create shared memory segments, message queues or semaphore arrays. RESOURCE OPTIONS Resources may be specified as follows: -M, --shmem [size] Shared memory segment of size bytes. -S, --semaphore [number] Semaphore array with number of elements. -Q, --queue Message queue. ADDITIONAL OPTIONS -p, --mode [mode] Permission for the resource. Default is 0644. -h, --help Display a short help message and exit. -V, --version Output version information and exit. SEE ALSO ipcrm(1), ipcs(1) AUTHOR Hayden A. James ?hayden.james@gmail.com? AVAILABILITY The ipcmk command is part of the util-linux package and is available from Linux Kernel Archive ?ftp://ftp.kernel.org/pub/linux/utils/util- linux/?. util-linux September 2011 IPCMK(1) [root@oel71 ~]# |
和上面ipcrm的使用类似,具体如下:
创建消息队列:ipcmk -Q
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status ------ Semaphore Arrays -------- key semid owner perms nsems [root@oel71 ~]# [root@oel71 ~]# ipcmk -Q Message queue id: 0 [root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages 0xcc21bd2a 0 root 644 0 0 ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status ------ Semaphore Arrays -------- key semid owner perms nsems [root@oel71 ~]# [root@oel71 ~]# |
创建共享内存:ipcmk -M <容量大小,单位:字节>。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages 0xcc21bd2a 0 root 644 0 0 ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status ------ Semaphore Arrays -------- key semid owner perms nsems [root@oel71 ~]# ipcmk -M 2048000 Shared memory id: 1212416 [root@oel71 ~]# [root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages 0xcc21bd2a 0 root 644 0 0 ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x7c392f46 1212416 root 644 2048000 0 ------ Semaphore Arrays -------- key semid owner perms nsems [root@oel71 ~]# |
创建信号:ipcs -S
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages 0xcc21bd2a 0 root 644 0 0 ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x7c392f46 1212416 root 644 2048000 0 ------ Semaphore Arrays -------- key semid owner perms nsems [root@oel71 ~]# [root@oel71 ~]# ipcmk -S 152 Semaphore id: 360448 [root@oel71 ~]# [root@oel71 ~]# ipcs -a ------ Message Queues -------- key msqid owner perms used-bytes messages 0xcc21bd2a 0 root 644 0 0 ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x7c392f46 1212416 root 644 2048000 0 ------ Semaphore Arrays -------- key semid owner perms nsems 0x53b0e9c9 360448 root 644 152 [root@oel71 ~]# |
———————————
Done。