嵌入式 Linux 进程间通信之信号灯的几点注意事项

来源:互联网 发布:windows日志怎么看sql 编辑:程序博客网 时间:2024/06/05 05:06

1. 关于union semun结构体的定义和头文件的包含

在用信号灯进行通信时,包含头文件:

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/sem.h>

但是,在编译程序的时候提示错误,提示union semun对应的类型不存在

看到有加头文件:#include <linux/sem.h> (不便于程序移植,如果包含该头文件,则不能移到UNIX系统下)

但在加上该头文件之后,又会提示,说有些数据结构被重复的定义了

搞得是加也不是,不加也不是

因为在semctl()方法中用到了该数据结构,所以man semctl,发现有如下说明:

The calling  program must define this union as follows:
           union semun {
               int              val;                        /* Value for SETVAL */
               struct semid_ds *buf;            /* Buffer for IPC_STAT, IPC_SET */
               unsigned short  *array;        /* Array for GETALL, SETALL */
               struct seminfo  *__buf;         /* Buffer for IPC_INFO
                                           (Linux-specific) */
           };

即,调用程序必须定义如下结构体...

原来需要自己定义union semun联合体才可以...

2. 关于ftok()函数中参数pathname的取值

key_t ftok(const char *pathname, int proj_id);

原来以为,随便给pathname取个值就好了,但是执行的时候总是提示错误:“ENOENT: A component of path does not exist, or path is an empty string.”

后来才发现,必须给该参数取一个有效的路径才可以...

在man ftok中也详细的文档说明:

pathname  (which  must  refer  to an existing, accessible file)  即,pathname必须指向一个已存在,并可访问的路径;

least significant 8 bits of proj_id 即,proj_id为8bit的有效的非0值(1-127)

3. 程序的执行

由于信号集为系统内核中存储,所以程序的执行必须有Root权限才可以

0 0