1、概述

   System V IPC共有三种类型:System V消息队列、System V 信号量、System V 共享内存区。 System V IPC操作函数如下:

2、key_t键和ftok函数

  三种类型的IPC使用key_t值作为他们的名字,头文件<sys/types.h>把key_t定义为一个整数,通常是一个至少32位的整数,由ftok函数赋予的。函数ftok把一个已存的路径和一个整数标识符转换成一个key_t值,称为IPC键。函数原型如下:

#include <sys/types.h>#include <sys/ipc.h>
key_t ftok(const char *pathname, int proj_id);  //成功返回IPC键,出错返回-1

写个程序看看ftok是如何组合IPC键,程序如下:

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/ipc.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8
9 typedef unsigned long u_long;
10
11 int main(int argc,char *argv[])
12 {
13 struct stat st;
14 key_t key;
15 if(argc != 2)
16 {
17 printf("usage: ftok<pathname>");
18 exit(0);
19 }
20 //获取文件结构信息
21 if (stat(argv[1],&st) == -1)
22 {
23 perror("stat() error");
24 exit(EXIT_FAILURE);
25 }
26 printf("st_dev : %lx,st_ino: %lx ",(u_long)st.st_dev,(u_long)st.st_ino);
27 //产生IPC键
28 if((key = ftok(argv[1],0x57)) == -1)
29 {
30 perror("ftok() error");
31 exit(EXIT_FAILURE);
32 }
33 printf("key: %x\n",key);
34 exit(0);
35 }

在Ubuntu上程序测试结果如下:

可以看出在Linux上面IPC键使用的是id低8位,st_dev的低8位以及st_ino的低16位构成的。

3、ipc_perm结构

  内核给每个IPC对象维护一个信息结构,内容跟内核给文件维护的信息类似。Unix下结构信息如下:

struct ipc_perm
{
key_t       key;     /* Key supplied to semget(2) */
uid_t        uid;     /* Effective UID of owner */
gid_t        gid;     /* Effective GID of owner */
uid_t        cuid;    /* Effective UID of creator */
gid_t        cgid;    /* Effective GID of creator */
unsigned short   mode;   /* Permissions */
unsigned short     seq;    /* Sequence number */
};

在Linux该结构信息如下:

参考http://linux.die.net/man/5/ipc

4、创建与打开IPC通道

  对于key值,有两种选择:

(1)调用fotk函数,给它传递pathname和id。

(2)指定key为IPC_PRIVATE,保证会创建一个新的、唯一的IPC对象。

5、IPC权限

For semaphores (from sys/sem.h)
#define SEM_A 0200 /* alter permission */
#define SEM_R 0400 /* read permission */

For message queues (from sys/msg.h)
#define MSG_R 0400 /* read permission */
#define MSG_W 0200 /* write permission */

For shared memory (from sys/shm.h)
#define SHM_R 0400 /* read permission */
#define SHM_W 0200 /* write permission */

6、标识符重用

  System V IPC
标识符是系统范围的,不是特定于进程的。ipc_perm结构含有一个名为seq的变量,是内核为系统每个潜在的IPC对象维护的计数器,每当删除一个IPC对象时,内核就递增相应的槽位号,若溢出则循环到0。这样避免短时间内重用System
V IPC标识符,有助于确保过早终止的服务器重新启动后不会重用标识符。写个程序测试输出有megget返回的前10个标识符值,程序如下:

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/ipc.h>
6 #include <sys/msg.h>
7
8 #define MSG_R 0400 /* read permission */
9 #define MSG_W 0200 /* write permission */
10
11 #define SVMSG_MODE (MSG_R | MSG_W | MSG_R >>3 | MSG_R >>6)
12
13 int main()
14 {
15 int i,msqid;
16 for(i=0;i<10;++i)
17 {
18 msqid = msgget(IPC_PRIVATE,SVMSG_MODE|IPC_CREAT);
19 printf("msqid = %d\n",msqid);
20 msgctl(msqid,IPC_RMID,NULL);
21 }
22 exit(0);
23 }

在Ubuntu上面测试结果如下:

7、ipcs和ipcrm程序

  System V IPC的三种类型不是以文件系统中的路径名标识的,不能使用ls和rm程序查看和删除。而是同ipcs程序输出System V IPC特性的各种信息,ipcrm则删除一个System V 消息队列、信号量或共享内存区。

ipcs - 分析消息队列、共享内存和信号量
ipcs [-mqs] [-abcopt] [-C core] [-N namelist]
-m 输出有关共享内存(shared memory)的信息
-q 输出有关信息队列(message queue)的信息
-s 输出信号量(semaphore)的信息

输出本机所有System V IPC消息如下:

ipcrm - 删除ipc(清除共享内存信息)
ipcrm -m|-q|-s shm_id
-m 输出有关共享内存(shared memory)的信息
-q 输出有关信息队列(message queue)的信息
-s 输出信号量(semaphore)的信息

8、内核限制

  System V IPC的多数实现在有内在的内核限制,如消息队列的最大数目、每个信号集的最大信号量数等等。

System V IPC的更多相关文章

  1. 第3章 System V IPC

    3.1 概述 System V IPC 包含:System V消息队列.System V信号量.System V共享内存. 3.2 key_t 键和 ftok函数 这三种类型的System V IPC ...

  2. 《Unix网络编程》卷2 读书笔记 第3章- System V IPC

    1. 概述 三种类型的System V IPC:System V 消息队列.System V 信号量.System V 共享内存区 System V IPC在访问它们的函数和内核为它们维护的信息上共享 ...

  3. 从并发处理谈PHP进程间通信(二)System V IPC

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

  4. System V IPC 之共享内存

    IPC 是进程间通信(Interprocess Communication)的缩写,通常指允许用户态进程执行系列操作的一组机制: 通过信号量与其他进程进行同步 向其他进程发送消息或者从其他进程接收消息 ...

  5. System V IPC 之信号量

    本文继<System V IPC 之共享内存>之后接着介绍 System V IPC 的信号量编程.在开始正式的内容前让我们先概要的了解一下 Linux 中信号量的分类. 信号量的分类 在 ...

  6. System V IPC 之消息队列

    消息队列和共享内存.信号量一样,同属 System V IPC 通信机制.消息队列是一系列连续排列的消息,保存在内核中,通过消息队列的引用标识符来访问.使用消息队列的好处是对每个消息指定了特定消息类型 ...

  7. 四十九、进程间通信——System V IPC 之消息队列

    49.1 System V IPC 介绍 49.1.1 System V IPC 概述 UNIX 系统存在信号.管道和命名管道等基本进程间通讯机制 System V 引入了三种高级进程间通信机制 消息 ...

  8. system v ipc的标识符ID

    system v ipc对象是靠标识符ID来识别和操作的,具有系统唯一性.意思就是说,该ID是操作系统内的全局变量,只要具有权限,任何进程都可以通过标识符进行进程间的通信.获取标识符ID的函数为int ...

  9. UNIX 进程间通讯(IPC)概念(Posix,System V IPC)

     IPC(Inter-Process Communication,进程间通讯)可以有三种信息共享方式(随文件系统,随内核,随共享内存).(当然这里虽然说是进程间通讯,其实也是可以和线程相通的). 相对 ...

随机推荐

  1. Minimum Window Substring leetcode java

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  2. C++11中万能的可调用类型声明std::function<...>

    在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...

  3. LA 4728 Square ,旋转卡壳法求多边形的直径

    给出一些正方形.让你求这些正方形顶点之间的最大距离的平方. //返回点集直径的平方 int diameter2(vector<Point> & points) { vector&l ...

  4. 数字签名算法(C#)

    public static string GetSHA1Method(string strSource) { string strResult = ""; //Create Sys ...

  5. Bootstrap全局CSS样式之表格

    .table--基础表格样式. .table-striped--给<tbody>之内的每一行添加斑马条纹样式: .table-bordered--为表格添加边框: .table-hover ...

  6. 导出DLLRegisterServer接口遇到的问题

    I'm trying to add DLLRegisterServer and DLLUnregisterServer entry points to an existing DLL that is ...

  7. XXX全球 IP 地址库

    XXX全球 IP 地址库 Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93.123.23.4 93.123.23.5 93.123.23.6 93.123 ...

  8. 《linux 内核全然剖析》 sys.c 代码分析

    sys.c 代码分析 setregid /* * This is done BSD-style, with no consideration of the saved gid, except * th ...

  9. Linux-cpu分析-vmstat

    转载:https://blog.csdn.net/ty_hf/article/details/63394960 一. 前言 为了更方便的理解本篇内容含义,所以请最好看看如下繁琐的概念,更容易理解. 没 ...

  10. 整数划分问题--DFS

    单点时限:1000ms 内存限制:256MB 描写叙述 Given two positive integers N and M, please divide N into several intege ...