免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10917 | 回复: 11
打印 上一主题 下一主题

gcc 为何union semun无法确定大小 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-09 16:54 |只看该作者 |倒序浏览
信号量编程中,如果使用

  1. #include <sys/types.h>;
  2. #include <sys/ipc.h>;
  3. #include <sys/sem.h>;

  4. union semun sem_val;
  5. sem_val.val = 1;
复制代码

定义,刚编译不通过,提示:unknown union semun size

but why?
教材都是这样写的

论坛徽章:
0
2 [报告]
发表于 2005-10-09 17:21 |只看该作者

gcc 为何union semun无法确定大小

#ifndef _APP_SEM_MUTEX_H__
#define _APP_SEM_MUTEX_H__
#include <pthread.h>;
#include <semaphore.h>;
#ifdef WIN32
#pragma comment(lib,"pthreadvc2.lib"
#endif
class CSem
{
public:
        CSem(int init_val=1)
        {
                sem_init(&m_sem,0,init_val);
        }
        ~CSem()
        {
                sem_destroy(&m_sem);
        }

        int wait()
        {
                return sem_wait(&m_sem);
        }
        //ret =0 ok else fail
        int trywait()
        {
                return sem_trywait(&m_sem);
        }
        int post()
        {
                return sem_post(&m_sem);
        }
private:
        sem_t m_sem;
};

class CMutex
{
public:
        CMutex()
        {
                pthread_mutexattr_init(&mta);
                pthread_mutexattr_settype(&mta,PTHREAD_MUTEX_RECURSIVE_NP);
                pthread_mutex_init(&mtx, &mta);
        }
        ~CMutex()
        {
                pthread_mutexattr_destroy(&mta);
                pthread_mutex_destroy(&mtx);
        }
        void lock()
        {
                pthread_mutex_lock(&mtx);
        }
        //ret =0 ok else fail
        int trylock()
        {
                return pthread_mutex_trylock(&mtx);
        }
        void unlock()
        {
                pthread_mutex_unlock(&mtx);
        }
private:
        pthread_mutex_t mtx;
        pthread_mutexattr_t mta;
};
#endif

论坛徽章:
0
3 [报告]
发表于 2005-10-09 17:31 |只看该作者

gcc 为何union semun无法确定大小

上面的是线程的同步吧,进程的有没例子?用到semun的?tks

论坛徽章:
0
4 [报告]
发表于 2005-10-09 17:34 |只看该作者

gcc 为何union semun无法确定大小

查一下有名信号量.代码差不多

论坛徽章:
0
5 [报告]
发表于 2005-10-09 17:37 |只看该作者

gcc 为何union semun无法确定大小

man 里写着
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

论坛徽章:
0
6 [报告]
发表于 2005-10-10 09:28 |只看该作者

gcc 为何union semun无法确定大小

哦.我看到他有个#if判断,已为已经定义了呢,看来还是理解能力跟E差些,回头试试,谢谢各位.
xth 该用户已被删除
7 [报告]
发表于 2005-11-25 18:54 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2005-11-25 19:17 |只看该作者
/* The user should define a union like the following to use it for arguments
   for `semctl'.

   union semun
   {
     int val;               <= value for SETVAL
     struct semid_ds *buf;      <= buffer for IPC_STAT & IPC_SET
     unsigned short int *array;     <= array for GETALL & SETALL
     struct seminfo *__buf;     <= buffer for IPC_INFO
   };

   Previous versions of this file used to define this union but this is
   incorrect.  One can test the macro _SEM_SEMUN_UNDEFINED to see whether
   one must define the union or not.  */
#define _SEM_SEMUN_UNDEFINED    1

现在要自己定义了吧...

论坛徽章:
0
9 [报告]
发表于 2007-08-07 18:11 |只看该作者
谢谢

论坛徽章:
0
10 [报告]
发表于 2007-08-07 18:59 |只看该作者
对 要自己定义
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP