#include<unistd.h>
#include<string.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<iostream>
#include<cstdio>
#include<sys/wait.h>
#include<cstdlib>
#include<signal.h>
using namespace std;
int k;
void int_func(int sig){
k=0;
}
int main(){
int pid;
signal(SIGUSR1,int_func);
k=1;
int shmid ;
shmid = shmget(1234,1024,0666|IPC_CREAT);
char* shm_addr=(char*)shmat(shmid,0,0);
// cout<<shm_addr<<endl;
while((pid=fork())==-1);
if(pid==0){
//子进程
while(k==1);
cout<<"recieve message from parent :\n"<<shm_addr<<endl;
shmdt(shm_addr);
shmctl(shmid,IPC_RMID,0);
exit(0);
}else{
cout<<"please inputs your message ,'end' is end"<<endl;
while(1){
char buffer[1024];
fgets(buffer,1024,stdin);
strcat(shm_addr,buffer);
if(strncmp(buffer,"end",3)==0)break;
sleep(1);
}
shmdt(shm_addr);
kill(pid,SIGUSR1);
exit(0);
}
}