共享内存
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main()
{
pid_t t;
int shmid;
shmid = shmget(IPC_PRIVATE, 1024, 0666);//创建共享内存
system("ipcs -m");
t = fork();
if(t == 0){//child
sleep(3);
char *b;
b = (char *)shmat(shmid, NULL, 0);//建立映射
system("ipcs -m");
printf("the child b: %s\n",b);//把里面的东西打印一下
shmdt(b);//解除映射
system("ipcs -m");
shmctl(shmid, IPC_RMID, NULL);//删除对象
system("ipcs -m");
exit(0);
}else{//prarent
char *buf;
buf = (char *)shmat(shmid, NULL, 0);//映射一下
system("ipcs -m");
fgets(buf, 5, stdin);//弄点东西进去
printf("the parent buf: %s\n",buf);
shmdt(buf);//解除映射
system("ipcs -m");
waitpid(t, NULL, 0);//等待子进程退出再退出
}
return 0;
}