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

高唐做网站建设的公司网络舆情

高唐做网站建设的公司,网络舆情,美业o2o平台有哪些,凡科快图一键抠图1.进程创建 1.1 fork函数 fork()通过复制调用进程来创建一个新进程。新进程称为子进程,是调用进程的精确副本 进程,但以下几点除外: 子进程有自己的PID,此PID与任何现有进程组的ID不匹配子进程的父进程ID…

1.进程创建

1.1 fork函数

        fork()通过复制调用进程来创建一个新进程。新进程称为子进程,是调用进程的精确副本
进程,但以下几点除外:

  • 子进程有自己的PID,此PID与任何现有进程组的ID不匹配
  • 子进程的父进程ID与父进程的进程ID相同。
  • 子进程没有继承父进程的内存锁
  • 进程资源利用率(getrusage(2))和CPU时间计数器(times(2))在子进程中重置为零
  • 子进程的挂起信号集最初为空
  • 子进程不能继承父进程的信号调整
  • 子进程不从父进程继承记录锁
  • 子进程不从父进程继承计时器
  • 子进程不继承父进程未完成的异步I/O操作,也不继承任何异步操作从它的父进程中获取同步I/O上下文

#include <unistd.h>
pid_t fork();
返回值:fork成功则子进程PID被返回给父进程,0被返回给子进程。失败,-1被返回给父进程,没有子进程创建。-- 给父进程返回子进程的pid,子进程返回0,是因为一个父进程可以有多个子进程,儿子进程只能有一个父进程。

 当一个进程调用fork之后,就有两个二进制代码相同的进程。而且它们都运行到相同的地方。但每个进程都将可执行自己的代码,看如下程序:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>int g_val = 0;
int main()
{pid_t id = fork();if(id < 0){perror("fork");return 0;}else if(id == 0){ //childg_val = 100;printf("child[%d]: %d : %p\n", getpid(), g_val, &g_val);}else{ //parentprintf("parent[%d]: %d : %p\n", getpid(), g_val, &g_val);}sleep(1);return 0;
}

执行结果如下: 

 

由结果可以得出,fork之后,父子进程各自执行自己的代码块

fork调用失败的原因: 系统中有太多的进程 实际用户的进程数超过了限制

1.2 写时拷贝

        通常,父子代码共享,父子在不写入时,数据也是共享的,当任意一方试图写入,便以写时拷贝的方式各自一份副 本。具体见下图:

2.进程终止

进程退出场景:

  • 代码运行结束,结果正确
  • 代码运行结束,结果错误
  • 代码异常终止

 进程退出方式:

  • 从main返回 return n; 
    执行 return n 等同于执行 exit(n), 因为调用 main 的运行时函数会将 main 的返
    回值当做 exit 的参数。
  • 在任意地方调用exit(errno) -- 库函数,终止进程,主动刷新缓冲区
  • _exit() -- 系统调用,终止进程不会刷新缓冲区
  • ctrl + c -- 信号终止

 echo $?  :看记录最后一个进程在命令行执行完毕时对应的退出码

#include <unistd.h>
void _exit(int status);

参数:status定义了进程的终止状态,父进程通过wait来获取该值 虽然statusint,但是仅有低8位可以被父进程所用

#include <unistd.h>
void exit(int status);

exit最后也会调用exit, 但在调用exit之前,还做了其他工作:

1. 执行用户通过 atexit on_exit 定义的清理函数。
2. 关闭所有打开的流,所有的缓存数据均被写入
3. 调用 _exit

3.进程等待 

        检测子进程推出信息,将子进程的退出信息通过status拿回来。

3.1 进程等待必要性

  • 之前讲过,子进程退出,父进程如果不管不顾,就可能造成 僵尸进程 ,进而造成内存泄漏。
  • 另外,进程一旦变成僵尸状态,那就刀枪不入, kill -9 也无能为力
  • 最后,父进程派给子进程的任务完成的如何,我们需要知道。
  • 父进程通过进程等待的方式,回收子进程资源,获取子进程退出信息

3.2 进程等待的方法

wait方法

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int* status);
参数:获取子进程的退出状态

返回值:成功,返回终止子进程的pid,失败返回-1

 waitpid方法

#include <sys/types.h>

#include <sys/wait.h>

pid_t wait(pid_t pid, int *status, int options);
参数:pid:pid=-1,等待任意子进程,与wait等效;pid>0,等待其进程id与pid相等的子进程

           status:输出型参数,拿到子进程的退出结果。-- 有自己的为图结构,只关心低16
                        个比特位()0-15,
                        次低八位(8-15):进程退出状态(结果是否正确) -- 设为:(status>>8) &0XFF
                        低七位(0-7):进程终止信号(是否正常结束) -- 设为:status&0X7F
           options:先设为0
返回值:成功,返回收集到的子进程的pid

              若无可以收集的一推出的子进程,return 0;

               若调用出错,返回-1,这时,errno会被设置为相应的值,以指示错误所在。

 WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)

WEXITSTATUS(status): WIFEXITED非零,提取子进程退出码。(查看进程的退出码)        

 下面看一段示例代码:

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>int main()
{pid_t id = fork();if(id == 0){int cnt = 5;while(cnt){printf("我是子进程pid:%d, 父进程:%d, cnt = %d\n", getpid(), getppid(), cnt--);sleep(1);}// int d = 10/0;exit(10);}sleep(7);int status = 0;pid_t ret = waitpid(id, &status, 0);if(id > 0){printf("wait success: %d, sig_number: %d, child_exit_code: %d\n", ret, (status & 0x7F), (status>>8)&0xFF);}sleep(5);return 0;
}

结果下图所示:

第一个图为我执行kill -9 22287得到的结果,进程收到9号信号,进程终止

第二个图为程序正常运行结束的结果。

阻塞等待和非阻塞等待:

        阻塞等待:子进程未推出时,父进程不调用一直等待直到子进程结束。

        非阻塞等待:在与逆行期间一直询问子进程是否结束 waitpid()函数第三个参数设置为WNOHANG。
非阻塞等待的好处:

  • 不会占用父进程的所有精力,可以在轮询期间做别的事情

下面为非阻塞式等待的代码:

#include <stdio.h> 
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{pid_t pid;pid = fork();if(pid < 0){printf("%s fork error\n",__FUNCTION__);return 1;}else if( pid == 0 ){ //childprintf("child is run, pid is : %d\n",getpid());sleep(5);exit(1);} else{int status = 0;pid_t ret = 0;do{ret = waitpid(-1, &status, WNOHANG);//非阻塞式等待if( ret == 0 ){printf("child is running\n");}sleep(1);}while(ret == 0);if( WIFEXITED(status) && ret == pid ){printf("wait child 5s success, child return code is :%d.\n",WEXITSTATUS(status));}else{printf("wait child failed, return.\n");return 1;}}return 0;
}

可以看到,父进程在等待子进程的同时还会执行打印语句。若为非阻塞等待,会一直卡在等待的环节。 

总结:

进程等待:

  • 是什么? -- 通过系统调用,让父进程等待子进程的方式
  • 为什么? -- 释放子进程的僵尸状态,获取子进程状态
  • 怎么等? -- wait/waitpid  阻塞等待/非阻塞等待

4.进程程序替换

        相当于用自己的程序把别人的程序跑起来,支持不同语言(任何后端语言)的替换

4.1 创建子进程的目的

  • 想让子进程执行父进程代码的一部分 -- 执行父进程对应的磁盘代码中的一部分
  • 想让子进程执行一个全新的程序 -- 让紫禁城想办法,家在磁盘上指定的程序,执行新程序的代码和数据

4.2 替换原理

        就是将指定程序的代码和数据加载带指定的位置,进程替换时并没有创建新的进程。在子进程中调用execl函数并不会影响父进程的执行(进程具有独立性)。OS感觉到替换后,则进行写时拷贝,重新分配内存--子进程通过页表重新映射到新的内存。

4.3 替换函数

#include <unistd.h>`
int execl(const char *path, const char *arg, ...);
参数:path:路径
            arg:在命令行怎么执行就怎么传参
...:可变参数列表
返回值:执行失败返回-1,并继续执行源代码,成功不会返回值。
int execlp(const char *file, const char *arg, ...);
p:代表的就是如何找程序的功能,带p字符的函数,不用告诉我替换程序的路径,只需要知道时谁,会自动在环境变量PATH中进行可执行程序的查找。
int execle(const char *path, const char *arg, ...,char *const envp[]);
int execv(const char *path, char *const argv[]);
v
:vector,可以将所有的可执行参数,放入到数组(必须以空作为结束)中,不用可变参数传参
int execvp(const char *file, char *const argv[]);
 
int execve(const char *path, char *const argv[], char *const envp[]); -- 允许自定义环境变量

所有的execl* 系列的接口都必须以NULL结尾 

下面为各个接口的演示:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main()
{// 用我们的程序将别人的程序执行起来// .c -> exe -> load -> process -> 运行 -> 执行我们现在所写的代码printf("process is running...\n");// 只要是个函数,掉用就有可能失败 就是没有替换成功 继续执行下面的原代码// 只有错误时会返回,返回值为-1execl("/usr/bin/ls"/*要执行哪个程序*/, "ls","-a", "-l", "--color=auto", NULL/*你想怎么执行*/);  // 所有的execl* 系列的接口都必须以NULL结尾printf("execl\n");// 为何下面的printf没有执行呢? printf是在execl之后的,execl执行完毕后,代码已经完全被替换 开始新的程序的代码了printf("process running done...\n");return 0;
}

 这个代码使用execl接口将我们的程序替换为了命令行命令,ls -a -l,结果如下:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main(int argc, char *argv[])
{printf("process is running\n!");pid_t id = fork();assert(id!=-1);if(id == 0){sleep(1);execlp("ls", "ls","-a", "-l", NULL);printf("原子进程!\n");exit(10);}// 下面为父进程的代码,子进程的替换不会影响父进程的代码int status = 0;int ret = waitpid(id, &status, 0);if(ret > 0) printf("wait success: exit code:%d, sig:%d\n", (status>>8) & 0xFF, status & 0x7F);return 0;
}

 有这段代码也可以看出,子进程在进行程序替换时,是不会影响父进程的

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main(int argc, char *argv[])
{printf("process is running\n!");pid_t id = fork();assert(id!=-1);if(id == 0){sleep(1);char *arv_[] = { "ls","-a", "-l", NULL };execv("/usr/bin/ls", arv_);exit(10);}// 下面为父进程的代码,子进程的替换不会影响父进程的代码int status = 0;int ret = waitpid(id, &status, 0);if(ret > 0) printf("wait success: exit code:%d, sig:%d\n", (status>>8) & 0xFF, status & 0x7F);return 0;
}

其他几个与上面的类似,这里就不一一列举了。除了可以替换系统命令,还可以将程序替换为自己的程序,下面用一个接口execvp来演示。

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/wait.h>
#include<sys/types.h>int main(int argc, char *argv[])
{printf("process is running\n!");pid_t id = fork();assert(id!=-1);if(id == 0){sleep(1);char *arv_[] = { "mycpp", NULL };execvp("./mybin", arv_);exit(10);}// 下面为父进程的代码,子进程的替换不会影响父进程的代码int status = 0;int ret = waitpid(id, &status, 0);if(ret > 0) printf("wait success: exit code:%d, sig:%d\n", (status>>8) & 0xFF, status & 0x7F);return 0;
}

 


文章转载自:
http://unfamed.crhd.cn
http://formalization.crhd.cn
http://pedestrianism.crhd.cn
http://hydrophobia.crhd.cn
http://photodramatist.crhd.cn
http://gorry.crhd.cn
http://audiogram.crhd.cn
http://dull.crhd.cn
http://amylase.crhd.cn
http://carrick.crhd.cn
http://hantu.crhd.cn
http://concordance.crhd.cn
http://zecchino.crhd.cn
http://whoosh.crhd.cn
http://pallasite.crhd.cn
http://analysissitus.crhd.cn
http://osmunda.crhd.cn
http://citral.crhd.cn
http://vacationland.crhd.cn
http://seroconvert.crhd.cn
http://revest.crhd.cn
http://sixpence.crhd.cn
http://pant.crhd.cn
http://precipitation.crhd.cn
http://tuvaluan.crhd.cn
http://mainboom.crhd.cn
http://destructional.crhd.cn
http://grammatical.crhd.cn
http://ultrafast.crhd.cn
http://moralization.crhd.cn
http://alonso.crhd.cn
http://unpeg.crhd.cn
http://ferdinand.crhd.cn
http://frankish.crhd.cn
http://glassworm.crhd.cn
http://judicial.crhd.cn
http://lockstitch.crhd.cn
http://materiel.crhd.cn
http://stickykey.crhd.cn
http://rexine.crhd.cn
http://eurocredit.crhd.cn
http://fricative.crhd.cn
http://peridiolum.crhd.cn
http://ghaut.crhd.cn
http://transparent.crhd.cn
http://indecisive.crhd.cn
http://inconsiderable.crhd.cn
http://tributary.crhd.cn
http://dismemberment.crhd.cn
http://collagenous.crhd.cn
http://castrate.crhd.cn
http://coelostat.crhd.cn
http://bimensal.crhd.cn
http://faddism.crhd.cn
http://dequeue.crhd.cn
http://impendent.crhd.cn
http://cowled.crhd.cn
http://gabardine.crhd.cn
http://significatory.crhd.cn
http://sokeman.crhd.cn
http://himation.crhd.cn
http://tariffless.crhd.cn
http://intensively.crhd.cn
http://mudder.crhd.cn
http://engorgement.crhd.cn
http://untainted.crhd.cn
http://lightningproof.crhd.cn
http://gradational.crhd.cn
http://dvandva.crhd.cn
http://waiter.crhd.cn
http://tetraethyl.crhd.cn
http://oleomargarine.crhd.cn
http://ineludible.crhd.cn
http://shear.crhd.cn
http://sedlitz.crhd.cn
http://sector.crhd.cn
http://liftboy.crhd.cn
http://internship.crhd.cn
http://amuse.crhd.cn
http://modenese.crhd.cn
http://unfitness.crhd.cn
http://revealing.crhd.cn
http://dentition.crhd.cn
http://rattled.crhd.cn
http://pomelo.crhd.cn
http://farness.crhd.cn
http://transection.crhd.cn
http://determination.crhd.cn
http://telemetric.crhd.cn
http://annamese.crhd.cn
http://please.crhd.cn
http://neighborhood.crhd.cn
http://chromatophile.crhd.cn
http://zingy.crhd.cn
http://hypomotility.crhd.cn
http://gelatose.crhd.cn
http://romanian.crhd.cn
http://crownwork.crhd.cn
http://mucrones.crhd.cn
http://lancang.crhd.cn
http://www.15wanjia.com/news/89702.html

相关文章:

  • 青岛网站建设哪个好深圳推广系统
  • 深圳关键词快速排名东莞做网站排名优化推广
  • 杭州培训网站建设某个网站seo分析实例
  • 网站设计建议腾讯云域名
  • 湖南省建筑设计院集团有限公司seo外包是什么意思
  • 如何分析一个网站做的怎么样怎么搭建自己的网站
  • 有合作做时时彩网站的吗网络营销和网络推广
  • net asp网站开发seo是什么意思广东话
  • 武汉手机微信网站建设网店推广的重要性
  • 徐州网站推广优化googleplay安卓版下载
  • 开发动态网站有哪些技术头条新闻
  • 公司黄页怎么查seo关键词怎么优化
  • 如何设计好酒店网站模板百度seo排名帝搜软件
  • 做网站开发要注册搜索引擎优化人员优化
  • 垂直网站怎么建设网络做推广公司
  • 有什么网站可以做设计兼职的seo 知乎
  • 建设网站怎么赚钱的临沂百度推广多少钱
  • php网站开发前景大丰seo排名
  • 广东建设教育协会网站如何让百度搜索排名靠前
  • 群晖ds1817做网站国内搜索引擎排名2022
  • 网站建设制作设计推广广告网站留电话不用验证码
  • 东凤网站宁波网站关键词优化代码
  • 深圳比较大的贸易进口公司长沙seo网站管理
  • 宝应县城乡建设局网站百度app客服人工在线咨询
  • 国外服装图案设计网站国内的搜索引擎排名
  • 织梦 旅游网站模板b站推广入口2023破解版
  • 营销型品牌网站建设手机百度2022年新版本下载
  • 国外有哪些网站做b2b的官方百度app下载安装
  • 周口市城乡建设局网站seo营销推广全程实例
  • 哪里有做ppt的网站优化方案官方网站