send.c
#include<myhead.h>
#define lage 4096
int main(int argc, const char *argv[])
{
key_t key = ftok("./",'w');
if(key == -1)
{
perror("ftok");
return -1;
}
int shmid = shmget(key,lage,IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmid");
return -1;
}
char *buff;
if((buff=shmat(shmid,NULL,0))==(void *)-1)
{
perror("shmat");
return -1;
}
char cpy[1024];
while(1)
{
printf("发送\n");
fgets(buff,sizeof(buff),stdin);//从文件输入流写入buff
strcpy(cpy,buff);
if(strcmp(cpy,"quit\n")==0)
{
break;
}
}
if(shmdt(buff)==-1)//断开进程和共享内存的联系
{
perror("shmdt");
return -1;
}
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl");
return -1 ;
}
return 0;
}
rev.c
#include<myhead.h>
#define lage 4096
int main(int argc, const char *argv[])
{
key_t key = ftok("./",'w');
if(key == -1)
{
perror("ftok");
return -1;
}
int shmid = shmget(key,lage,IPC_CREAT|0664);
if(shmid==-1)
{
perror("shmid");
return -1;
}
char *buff;
if((buff=shmat(shmid,NULL,0))==(void *)-1)
{
perror("shmat");
return -1;
}
while(1)
{
printf("%s\n",buff);
if(strcmp(buff,"quit\n")==0)
{
break;
}
}
if(shmdt(buff)==-1)//断开进程和共享内存的联系
{
perror("shmdt");
return -1;
}
if(shmctl(shmid,IPC_RMID,NULL)==-1)
{
perror("shmctl");
return -1 ;
}
return 0;
}