急,关于union semun的编译错误,大家帮忙看看,在线等待
初学LINUX编程不久,最近涉及到信号量,
但是关于union semun 的声明一直编译不过去,
抄了一段示例代码也不行
编译报错“aggregate `union semun options' has incomplete type and cannot be initialized”
怎么回事呢?多谢大家了,在线等待
我的系统是Red Hat Linux release 6.1 (Cartman)
Kernel 2.2.12-20 on an i686
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
void main() {
key_t unique_key; /* 定义一个IPC关键字*/
int id;
struct sembuf lock_it;
/*大家请看,就是这一行 编译不过去*/
union semun options;
int i;
unique_key = ftok(".", 'a'); /* 生成关键字,字符'a'是一个随机种子*/
/* 创建一个新的信号量集合*/
id = semget(unique_key, 1, IPC_CREAT | IPC_EXCL | 0666);
printf("semaphore id=%d\n", id);
options.val = 1; /*设置变量值*/
semctl(id, 0, SETVAL, options); /*设置索引0的信号量*/
/*打印出信号量的值*/
i = semctl(id, 0, GETVAL, 0);
printf("value of semaphore at index 0 is %d\n", i);
/*下面重新设置信号量*/
lock_it.sem_num = 0; /*设置哪个信号量*/
lock_it.sem_op = -1; /*定义操作*/
lock_it.sem_flg = IPC_NOWAIT; /*操作方式*/
if (semop(id, &lock_it, 1) == -1) {
printf("can not lock semaphore.\n");
exit(1);
}
i = semctl(id, 0, GETVAL, 0);
printf("value of semaphore at index 0 is %d\n", i);
/*清除信号量*/
semctl(id, 0, IPC_RMID, 0);
}