SEND.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
key_t key = ftok("./",'A');
int ID = shmget(key,PAGE_SIZE,IPC_CREAT|0664);
char *buff = shmat(ID,NULL,0);
while(1)
{
char arr[1024];
printf("请输入数据:");
fgets(arr,sizeof(arr),stdin);
strcpy(buff,arr);
if(strcmp(buff,"quit\n")==0)
break;
}
if(shmdt(buff) == -1)
{
perror("shmdt");
return -1;
}
if(shmctl(ID,IPC_RMID,NULL)==-1)
{
perror("shmctl");
return -1;
}
return 0;
}
REC.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#define PAGE_SIZE 4096
int main(int argc, const char *argv[])
{
key_t key = ftok("./",'A');
int ID = shmget(key,PAGE_SIZE,IPC_CREAT|0664);
char *buff = shmat(ID,NULL,0);
while(1)
{
if(strcmp(buff,"quit\n")==0)
break;
printf("数据:%s",buff);
}
if(shmdt(buff) == -1)
{
perror("shmdt");
return -1;
}
if(shmctl(ID,IPC_RMID,NULL)==-1)
{
perror("shmctl");
return -1;
}
return 0;
}