#include<apue.h>
#include<sys/shm.h>
#include<TELL_WAIT.h>
#define NLOOPS 1000
#define SIZE sizeof(long)
using namespace std;
static int update(long *ptr){
return ((*ptr)++);
}
int main(){
int i,counter;
pid_t pid;
int shmid;
key_t key;
void *area;
struct ipc_perm perm;
perm.mode=0400|0200;
struct shmid_ds shm_ds;
shm_ds.shm_perm=perm;
if((shmid=shmget(key,SIZE,IPC_CREAT))<0)
err_sys("shmget error");
if(shmctl(shmid,IPC_SET,&shm_ds)<0)
err_sys("shmctl error");
if((area=shmat(shmid,0,0))<0)
err_sys;
TELL_WAIT();
if((pid=fork())<0)
err_sys("fork error");
else if(pid>0){
for(i=0;i<NLOOPS;i++){
if(counter=update((long *)area)!=i)
err_quit("parent:expected %d,got %d",i,counter);
}
TELL_CHILD(pid);
WAIT_CHILD();
}else{
for(i=1;i<NLOOPS+1;i+=2){
WAIT_PARENT();
if((counter=update((long *)area))!=i)
err_quit("child:expected %d,got %d",i,counter);
TELL_PARENT(getppid());
}
}
if(shmctl(shmid,IPC_RMID,NULL)<0)
err_sys("shmctl error");
exit(0);
}