#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 16
int main()
{
char *data;
data=(char *)malloc(SIZE);
if(data==NULL)
{
printf("mallco failed!\n");
exit(1);
}
char *str="test data";
strcpy(data,str);
printf("before fork,the data is: %s\n",data);
printf("before fork the addr of mem is: %p\n",data);
int pid;
if((pid=fork())==0)//父进程查看分配的内存信息
{
strcpy(data,"I am parent");
printf("In parent,the data is: %s\n",data);
printf("In parent,the addr of mem is: %p\n",data);
sleep(10);
printf("In parent,the data is: %s\n",data);
printf("In parent,the addr of mem is: %p\n",data);
}
else
{
sleep(5);
strcpy(data,"I am child");
printf("In child,the data is: %s\n",data);
printf("In child,the addr of mem is: %p\n",data);
}
free(data);
return 0;
}
<
Linux中父子进程Fork与malloc关系示例
最新推荐文章于 2024-05-28 23:50:24 发布
#include #include #include #define SIZE 16int main(){ char *data; data=(char *)malloc(SIZE); if(data==NULL) { printf("mallco failed!\n"); exit(1); } char *str="test data"; strcpy
摘要由CSDN通过智能技术生成