首页 技术 正文
技术 2022年11月7日
0 收藏 570 点赞 981 浏览 605 个字
#include "sys/types.h"#include "stdio.h"#include "stdlib.h"#include "unistd.h"int main(){pid_t pid;/*需要引入sys/types.h 和 unistd.h两个头文件创建进程 返回子进程的id 创建失败,返回-1;并提示错误信息,EAGAIN:表示fork()函数没有足够的内存用于复制父进程的分页表和进程结构数据ENOMEM:表示fork()函数分配必要的内核数据结构时,内存不足fork()函数会复制进程的所有资源,包括进程环境、内存资源,不与父进程共享*/int var=1;//测试是否共享堆栈段pid = fork();if (pid<0){printf("fork error!\n");}else if (pid==0){var=var+1;printf("in the child process! pid:%d,var:%d\n",pid,var);}else{var=var+3;printf("in the parent process pid:%d,var:%d\n",pid,var);}exit(0); }

打印结果:

in the child process! pid:0,var:2in the parent process pid:4316,var:4[Finished in 0.5s]

由此看出 fork()没有共享内存资源,仅仅是复制了父进程的所有资源

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297