急,关于union semun的编译错误,大家帮忙看看,在线等待

flyingdiablo 2002-09-02 04:47:18
初学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);
}

...全文
1008 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcconquer 2010-06-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 charity 的回复:]

找一本C的书好好看看union
[/Quote]

妈的,我最讨厌这种装B的了
blh 2002-09-03
  • 打赏
  • 举报
回复
他就是书上的例子
把下面的一段代码贴到你的程序里,缺少内省定义,union semun 在linux的定义被取消了,需要自己定义
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* union semun is defined by including <sys/sem.h> */
#else
/* according to X/OPEN we have to define it ourselves */
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* array for GETALL, SETALL */
/* Linux specific part: */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
charity 2002-09-02
  • 打赏
  • 举报
回复
找一本C的书好好看看union
flyingdiablo 2002-09-02
  • 打赏
  • 举报
回复
已经贴出来了,这里就是完整的代码
麻烦大家看看了
#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);
}
blh 2002-09-02
  • 打赏
  • 举报
回复
把你的代码铁出来

19,614

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧