共享内存实现父子进程间通讯
来源:互联网 发布:nodejs和java 编辑:程序博客网 时间:2024/06/12 00:20
//共享内存实现父子进程间通讯
//2013-10-30
//yang
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(void)
{
int shmid,pid=0,stop=3;
char *shm_p=NULL;
shmid = shmget(123456,2028,0600|IPC_CREAT);//创建共享内存
if(shmid == -1)
{
perror("shmget");
return 1;
}
shm_p = shmat(0600|IPC_CREAT,NULL,0);//对共享内存的映射实现父子进程可以调用内存
if(shm_p == NULL)
{
perror("shmat");
return 1;
}
pid = fork(); //创建进程
if(pid < 0)
{
printf("fork error");
}
if(pid == 0) //子进程
{
shm_p =(char *)malloc(strlen("hello...属于子进程")+1);
while(shm_p[0] != 'h')
{
memset(shm_p,'\0',strlen("hello...属于子进程")+1);
memcpy(shm_p,"hello...属于子进程",strlen("hello...属于子进程")+1);
}
puts(shm_p);
exit(stop);
}
else //父进程
{
wait(&stop);
//sleep(2);
shm_p =(char *)malloc(strlen("world...属于父进程")+1);
while(shm_p[0] != 'w')
{
memset(shm_p,'\0',strlen("world...属于父进程")+1);
strcat(shm_p,"world...属于父进程");
}
}
puts(shm_p);
free(shm_p);
shm_p = NULL;
}
//2013-10-30
//yang
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(void)
{
int shmid,pid=0,stop=3;
char *shm_p=NULL;
shmid = shmget(123456,2028,0600|IPC_CREAT);//创建共享内存
if(shmid == -1)
{
perror("shmget");
return 1;
}
shm_p = shmat(0600|IPC_CREAT,NULL,0);//对共享内存的映射实现父子进程可以调用内存
if(shm_p == NULL)
{
perror("shmat");
return 1;
}
pid = fork(); //创建进程
if(pid < 0)
{
printf("fork error");
}
if(pid == 0) //子进程
{
shm_p =(char *)malloc(strlen("hello...属于子进程")+1);
while(shm_p[0] != 'h')
{
memset(shm_p,'\0',strlen("hello...属于子进程")+1);
memcpy(shm_p,"hello...属于子进程",strlen("hello...属于子进程")+1);
}
puts(shm_p);
exit(stop);
}
else //父进程
{
wait(&stop);
//sleep(2);
shm_p =(char *)malloc(strlen("world...属于父进程")+1);
while(shm_p[0] != 'w')
{
memset(shm_p,'\0',strlen("world...属于父进程")+1);
strcat(shm_p,"world...属于父进程");
}
}
puts(shm_p);
free(shm_p);
shm_p = NULL;
}
0 0
- 共享内存实现父子进程间通讯
- 共享内存实现进程间通讯
- 共享内存实现进程间通讯 收藏
- 共享内存实现进程间通讯
- linux_c语言实现父子进程共享内存
- 进程间通讯-共享内存
- 进程间通讯----共享内存
- 进程间通讯---共享内存
- 进程间通讯-共享内存
- 进程间通讯:共享内存
- 父子进程间的共享内存通信
- 进程间通讯概述管道通讯信号通讯共享内存
- 进程间通讯-共享内存2(代码)
- 进程间通讯共享内存的具体步骤
- 使用内存共享机制进程间通讯
- Linux进程间通讯之共享内存
- Linux进程间通讯之共享内存
- 进程间通讯 —— 共享内存
- javascript
- Unity异常 OutOfMemoryException: Out of memory
- 云计算IAAS、PAAS、SAAS三者之间的联系
- 解决新建短信时,输入“+86”,然后输入联系人名字“1”,按删除键之后,联系人变为“1”,删除操作为达到预期结果
- 哪些原因会导致创业的失败呢?
- 共享内存实现父子进程间通讯
- Plus One
- doppler channle characteristic
- 使用jqMobi开发app基础:Badge的使用
- 配置数据发送邮件
- Reserved space protection
- 亲身经历:中型企业ERP选型全纪录
- ---
- 什么是非自然链接