当前位置: 首页 > news >正文

大良o2o网站建设百度手机卫士下载安装

大良o2o网站建设,百度手机卫士下载安装,网页游戏制作工具,wordpress 新建数据库linux系统调用fork()创建一个和当前进程完全相同的拷贝进程#xff0c;其中父进程和子进程的代码段#xff0c;堆栈段#xff0c;数据段均独立 进程必须的4要点#xff1a; a.要有一段程序供该进程运行 b.进程专用的系统堆栈空间。 c.进程控制块#xff0c;在linux中具体实… linux系统调用fork()创建一个和当前进程完全相同的拷贝进程其中父进程和子进程的代码段堆栈段数据段均独立 进程必须的4要点 a.要有一段程序供该进程运行 b.进程专用的系统堆栈空间。 c.进程控制块在linux中具体实现是task_struct d.有独立的存储空间。 当一个进程缺少其中一个条件时候我们称其为线程。 1.先看fork()的原型 #includesys/types.h /* 提供类型pid_t的定义 */ #includeunistd.h /* 提供函数的定义 */ pid_t fork(void); 返回 父进程中执行则返回子进程PID子进程中执行返回0 现在进行一个例子创建一个子进程然后根据返回的 PID进行识别 [cpp] view plaincopy /*test.c*/   #include sys/types.h   #include sys/stat.h   #include unistd.h      main()   {           int count  0;       pid_t pid;      /*此时仅有一个进程*/           pidfork(); /*此时已经有两个进程在同时运行*/         if(pid0)    /*返回错误*/           printf(error in fork!);          else        {       if(pid0)      /*代码在子进程中执行*/           printf(I am the child process, my count is %d,my process ID is %d/n,count,getpid());       else        /*代码在父进程中执行*/           printf(I am the parent process,my count is %d, my process ID is %d/n,count,getpid());       }   }   弄好后在linux中键入 $ gcc test.c -o test $ ./test 在本次试验中 I am the parent process,my count is 1,my process ID is 3196 I am the child process, my count is 0,my process ID is 3776 从代码里面可以看出2者的pid不同内存资源count是值得复制父进程改变了count的值而子进程中的count没有被改变。有人认为这样大批量的复制会导致执行效率过低。其实在复制过程中子进程复制了父进程的task_struct系统堆栈空间和页面表这意味着上面的程序我们没有执行count前其实子进程和父进程的count指向的是同一块内存。而当子进程改变了父进程的变量时候会通过copy_on_write的手段为所涉及的页面建立一个新的副本。所以当我们执行count后这时候子进程才新建了一个页面复制原来页面的内容基本资源的复制是必须的而且是高效的。整体看上去就像是父进程的独立存储空间也复制了一遍。 在fork中父子进程是独立开来的 并没有影响 2.vfork函数 vfork创建出来的不是真正意义上的进程而是一个线程因为它缺少了上面提到的进程的四要素的第4项独立的内存资源我们编一个程序练习 [cpp] view plaincopy #include sys/types.h   #include sys/stat.h   #include unistd.h      main()    {           int count  1;           int child;              printf(Before create son, the fathers count is:%d/n, count);//打印没创建进程前           if(!(child  vfork())) //创建子进程       {                   printf(This is son, his pid is: %d and the count is: %d/n, getpid(), count);                   exit(1);           } else        {                   printf(After son, This is father, his pid is: %d and the count is: %d, and the child is: %d/n,              getpid(), count, child);           }   }   然后编译执行得到下列结果 Before create son, the fathers count is:1 This is son, his pid is: 4048 and the count is: 2 After son, This is father, his pid is: 4048 and the count is: 2, and the child is: 2748 从这里我们看到子进程和父进程是共享count的也就是说内存区是共享的 另外由vfork创造出来的子进程还会导致父进程挂起除非子进程exit或者execve才会唤起父进程看下面程序 [cpp] view plaincopy #include sys/types.h   #include sys/stat.h   #include unistd.h      main()    {           int count  1;           int child;              printf(Before create son, the fathers count is:%d/n, count);           if(!(child  vfork()))        {//这里是子进程执行区                   int i;                   for(i  0; i  100; i)            {                           printf(This is son, The i is: %d/n, i);                           if(i  70)                                   exit(1);                   }                   printf(This is son, his pid is: %d and the count is: %d/n, getpid(), count);                   exit(1);//子进程退出           }        else        {//父进程                   printf(After son, This is father, his pid is: %d and the count is: %d, and the child is: %d/n,              getpid(), count, child);           }   }   好编译通过执行。。。 Before create son, the fathers count is:1 This is son, The i is: 0 ... ... This is son, The i is: 68 This is son, The i is: 69 This is son, The i is: 70 After son, This is father, his pid is: 2564 and the count is: 1, and the child is: 2736 可以看到父进程总是等子进程执行完毕后才开始继续执行
http://www.zqtcl.cn/news/522294/

相关文章:

  • 青岛崂山区网站建设广东企业网站建设多少钱
  • 男女做那个的小视频网站韩国儿童才艺网站建设模板
  • 餐饮品牌网站建设淮北论坛最新招聘
  • 给客户做网站网站自动适应屏幕
  • 人力资源培训与开发什么是网站优化
  • 制作 网站 盈利农村自建房设计图一层平房
  • 佛山住房和城乡建设厅网站wordpress图片外链转内链
  • 海东高端网站建设价格wordpress侧边栏淘宝客
  • 网站功能建设中页面wordpress让投稿
  • 学校网站 asp网站结构方面主要做哪些优化
  • 深圳做网站(信科网络)做网站需要多少资金
  • 做网站实例教程网站图片的作用
  • 网站建设展板营销渠道的三个类型
  • 用php做视频网站有哪些十大免费logo设计
  • 网站建设对于网络营销的意义微信购物商城
  • 基于个性化推荐的电商网站设计与实现网站 用户体验的重要性
  • 怎么用ajax做电商网站企业网查询是什么
  • 海淀企业网站建设张店学校网站建设公司
  • 专业微网站开发做购物网站怎么赚钱
  • 怎样做酒店网站ppt什么是企业网络营销平台
  • 科技部网站改版方案济南众筹网站建设
  • 中国城乡与住房建设部网站电子商务公司名字推荐
  • 设计参考网站有哪些wordpress 支付宝免签
  • 网站关键词排名优化应该怎么做外包加工网缝纫机外放加工活
  • 电影网站建设模板从传播的角度
  • 北京建网站的公司广州冼村和猎德村哪个最有钱
  • 成都网站建设有限公司济南j建设网
  • 一家网站建设公司需要什么资质互联网网站模块
  • 网站开发php支付接口网站平台建设缴纳什么税
  • 百度文库推广网站庆云网站seo