作业:
作业一:
typedef struct shm
{
char text[256];
int flgs;
}shm_t;
int main(int argc, const char *argv[])
{
key_t key=ftok("./ipc",1);
int shmid=shmget(key,sizeof(shm_t),IPC_CREAT|0666);
shm_t *shm_p=shmat(shmid,0,0);
while(1)
{
shm_p->flgs=0;
printf("talking to girl:");
scanf("%255s",shm_p->text);
while(getchar()!=10);
shm_p->flgs=1;
}
return 0;
}
typedef struct shm
{
char text[256];
int flgs;
}shm_t;
int main(int argc, const char *argv[])
{
key_t key=ftok("./ipc",1);
int shmid=shmget(key,sizeof(shm_t),IPC_CREAT|0666);
shm_t *shm_p=shmat(shmid,0,0);
while(1)
{
while(shm_p->flgs!=0);
printf("the boy says:%s\n",shm_p->text);
shm_p->flgs=1;
}
return 0;
}
作业二:
typedef struct shm
{
char btext[64];
char gtext[64];
int bflgs;
int gflgs;
}shm_t;
int main(int argc, const char *argv[])
{
key_t key=ftok("./ipc",1);
int shmid=shmget(key,sizeof(shm_t),IPC_CREAT|0666);
shm_t *shm_p=shmat(shmid,0,0);
pid_t pid=fork();
if(pid>0)
{
while(1)
{
shm_p->bflgs=0;
printf("talking to girl:");
scanf("%63s",shm_p->btext);
while(getchar()!=10);
shm_p->bflgs=1;
}
}
else if(pid==0)
{
while(1)
{
while(shm_p->gflgs!=0);
printf("the girl says:%s\n",shm_p->gtext);
shm_p->gflgs=1;
}
}
return 0;
}
typedef struct shm
{
char btext[64];
char gtext[64];
int bflgs;
int gflgs;
}shm_t;
int main(int argc, const char *argv[])
{
key_t key=ftok("./ipc",1);
int shmid=shmget(key,sizeof(shm_t),IPC_CREAT|0666);
shm_t *shm_p=shmat(shmid,0,0);
pid_t pid=fork();
if(pid>0)
{
while(1)
{
while(shm_p->bflgs!=0);
printf("the boy says:%s\n",shm_p->btext);
shm_p->bflgs=1;
}
}
else if(pid==0)
{
while(1)
{
shm_p->gflgs=0;
printf("talking to boy:");
scanf("%63s",shm_p->gtext);
while(getchar()!=10);
shm_p->gflgs=1;
}
}
return 0;
}