turingcat 发表于 2004-12-23 19:21:18

编程问题

最近在学操作系统,
写了一个p,v操作的程序,在学校机房老版本的red hat下可以编译通过

在magic linux下就是不能通过
提示storage size of ‘semopts’ isn't known
大家帮我看看是怎么回事

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <semaphore.h>
#include <sys/sem.h>
#include <sys/ipc.h>

void P(int semid,int index)
{
struct sembuf sem;
sem.sem_num = index;
sem.sem_op = -1;
sem.sem_flg = 0; //:操作标记:0或IPC_NOWAIT等
semop(semid,&sem,1); //1:表示执行命令的个数
return;
}

void V(int semid,int index)
{
struct sembuf sem;
sem.sem_num = index;
sem.sem_op = 1;
sem.sem_flg = 0;
semop(semid,&sem,1);
return;
}
int semid;
int a=0;
#define LOOPS 10
pthread_t p1,p2;
void *subp1();
void *subp2();
main()
{
union semun semopts;
int res;
semid = semget(300,2,IPC_CREAT|0666);
if (semid<0) {
printf("error");
return;
}
semopts.val = 1;
res=semctl(semid,0,SETVAL,semopts);
semopts.val = 0;
res=semctl(semid,1,SETVAL,semopts);
if (res < 0) return;
pthread_create(&p1,NULL,subp1,NULL);
pthread_create(&p2,NULL,subp2,NULL);
pthread_join(p1,NULL);
pthread_join(p2,NULL);
semctl(semid,0,IPC_RMID,0);
}
void *subp1()
{
int i,j;
for (i=0; i< LOOPS;i++) {
P(semid,0);
printf("subp1:a=%d\n",a);
V(semid,1);
}
return;
}

void *subp2()
{
int i,j;
for (i=0;i<LOOPS;i++) {
P(semid,1);
a+=10;
V(semid,0);
}
return;
}
页: [1]
查看完整版本: 编程问题