当前位置: 技术问答>linux和unix
进程调度,共享内存
来源: 互联网 发布时间:2016-07-04
本文导语: 最好哪位是能帮我运行一下 问题所在: 我在全局定义了 typedef char string[50]; struct databuf { int nbuf; //number of string buf string buf[10]; }; 但是最后提示的错误是:reader和writer函数中nbuf未定义 我不是定...
最好哪位是能帮我运行一下
问题所在:
我在全局定义了
typedef char string[50];
struct databuf
{
int nbuf; //number of string buf
string buf[10];
};
但是最后提示的错误是:reader和writer函数中nbuf未定义
我不是定义了吗?
哪位帮忙~ 谢了!
#include
#include
#include
#include
#include
#include
#include
#define SHMKEY (key_t)0x10
#define SEMKEY (key_t)0x20
#define IFLAGS (IPC_CREAT | IPC_EXCL)
#define ERR ((struct databuf*)-1)
typedef char string[50];
struct databuf
{
int nbuf; //number of string buf
string buf[10];
};
static semid,shmid;
void fatal(char *mes)
{
perror(mes);
exit(1);
}
void getseg(struct databuf **p)
{
if(shmid=shmget(SHMKEY,sizeof(struct databuf),0666|IFLAGS)==-1)
fatal("shmget");
//link to data pointer
if((*p=(struct databuf *)(shmat(shmid,0,0)))==ERR)
fatal("shmat");
}
int getsem()
{
//create semaphore
if((semid=semget(SEMKEY,1,IFLAGS | 0666))==-1)
fatal("semget");
if(semctl(semid,0,SETVAL,0)==-1)
fatal("semctl");
return semid;
}
void myremove()
{
if(semctl(semid,0,IPC_RMID,NULL)==-1)
fatal("semclt");
if(shmctl(shmid,IPC_RMID,NULL)==-1)
fatal("shmctl");
}
void reader(int semid,struct databuf *dbuf)
{
printf("This is reader process(son process)n");
struct sembuf p={0,-1,0};
struct sembuf v={0,1,0};
char judge;
while(1)
{
if(semop(semid,&p,1)==-1)
fatal("semop");
if(dbuf->nbuf==0)
{
fprintf(stderr,"There is no data in the shared memory!n");
semop(semid,&v,1);
continue;
}
else
{
printf("%s",dbuf->buf[--nbuf]);
}
if(semop(semid,&v,1)==-1)
fatal("semop");
/*
printf("Continue to read?(y/n)n");
scanf("%c",&judge);
if(judge=='Y' | judge=='y')
continue;
else
berak;*/
}
}
void writer(int semid,struct databuf *dbuf)
{
printf("This is writer process(father process)n");
struct sembuf p={0,-1,0};
struct sembuf v={0,1,0};
char judge;
while(1)
{
//wait
if(semop(semid,&p,1)==-1)
fatal("semop");
//whether the buf full?
if(dbuf->nbuf==10)
{
fprintf(stderr,"The buf is full!n");
semop(semid,&v,1);
continue;
}
else
{
printf("Please put data to the buf:n");
scanf("%s",dbuf->buf[nbuf++]);
}
if(semop(semid,&v,1)==-1)
{
fatal("semop");
}
/*
//continue to write
printf("Continue to write?(y/n)n");
scanf("%c",&judge);
if(judge=='Y' | judge=='y')
continue;
else
exit(0); */
}
}
int main()
{
int pid;
struct databuf *buf;
//myremove();
//initial semaphore set
semid=getsem();
//create and link to shared memory
getseg(&buf); //take care
switch(pid=fork())
{
case -1:
fatal("fork()");
case 0:
writer(semid,buf);
myremove();
break;
default:
reader(semid,buf);
break;
}
// remove();
exit(0);
}
问题所在:
我在全局定义了
typedef char string[50];
struct databuf
{
int nbuf; //number of string buf
string buf[10];
};
但是最后提示的错误是:reader和writer函数中nbuf未定义
我不是定义了吗?
哪位帮忙~ 谢了!
#include
#include
#include
#include
#include
#include
#include
#define SHMKEY (key_t)0x10
#define SEMKEY (key_t)0x20
#define IFLAGS (IPC_CREAT | IPC_EXCL)
#define ERR ((struct databuf*)-1)
typedef char string[50];
struct databuf
{
int nbuf; //number of string buf
string buf[10];
};
static semid,shmid;
void fatal(char *mes)
{
perror(mes);
exit(1);
}
void getseg(struct databuf **p)
{
if(shmid=shmget(SHMKEY,sizeof(struct databuf),0666|IFLAGS)==-1)
fatal("shmget");
//link to data pointer
if((*p=(struct databuf *)(shmat(shmid,0,0)))==ERR)
fatal("shmat");
}
int getsem()
{
//create semaphore
if((semid=semget(SEMKEY,1,IFLAGS | 0666))==-1)
fatal("semget");
if(semctl(semid,0,SETVAL,0)==-1)
fatal("semctl");
return semid;
}
void myremove()
{
if(semctl(semid,0,IPC_RMID,NULL)==-1)
fatal("semclt");
if(shmctl(shmid,IPC_RMID,NULL)==-1)
fatal("shmctl");
}
void reader(int semid,struct databuf *dbuf)
{
printf("This is reader process(son process)n");
struct sembuf p={0,-1,0};
struct sembuf v={0,1,0};
char judge;
while(1)
{
if(semop(semid,&p,1)==-1)
fatal("semop");
if(dbuf->nbuf==0)
{
fprintf(stderr,"There is no data in the shared memory!n");
semop(semid,&v,1);
continue;
}
else
{
printf("%s",dbuf->buf[--nbuf]);
}
if(semop(semid,&v,1)==-1)
fatal("semop");
/*
printf("Continue to read?(y/n)n");
scanf("%c",&judge);
if(judge=='Y' | judge=='y')
continue;
else
berak;*/
}
}
void writer(int semid,struct databuf *dbuf)
{
printf("This is writer process(father process)n");
struct sembuf p={0,-1,0};
struct sembuf v={0,1,0};
char judge;
while(1)
{
//wait
if(semop(semid,&p,1)==-1)
fatal("semop");
//whether the buf full?
if(dbuf->nbuf==10)
{
fprintf(stderr,"The buf is full!n");
semop(semid,&v,1);
continue;
}
else
{
printf("Please put data to the buf:n");
scanf("%s",dbuf->buf[nbuf++]);
}
if(semop(semid,&v,1)==-1)
{
fatal("semop");
}
/*
//continue to write
printf("Continue to write?(y/n)n");
scanf("%c",&judge);
if(judge=='Y' | judge=='y')
continue;
else
exit(0); */
}
}
int main()
{
int pid;
struct databuf *buf;
//myremove();
//initial semaphore set
semid=getsem();
//create and link to shared memory
getseg(&buf); //take care
switch(pid=fork())
{
case -1:
fatal("fork()");
case 0:
writer(semid,buf);
myremove();
break;
default:
reader(semid,buf);
break;
}
// remove();
exit(0);
}
|
static semid,shmid; //这是什么意思?
printf("%s",dbuf->buf[--nbuf]); [--nbuf]结构体的成员可以直接这样用吗?
printf("%s",dbuf->buf[--nbuf]); [--nbuf]结构体的成员可以直接这样用吗?
|
reader函数里面这一句:
printf("%s",dbuf->buf[--nbuf]);
--nbuf有问题,nbuf不能单独用的,要用必须是dbuf->nbuf
printf("%s",dbuf->buf[--nbuf]);
--nbuf有问题,nbuf不能单独用的,要用必须是dbuf->nbuf