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

非公党委网站的建设网络营销方案ppt

非公党委网站的建设,网络营销方案ppt,深圳百度网站推广,网站建设项目计划进程系统调用 文章目录进程系统调用fork()进程创建:fock()fork函数fork用法僵尸进程孤儿进程vfork函数vfork与fork区别exec函数族exec函数族-何时使用?exec函数族语法exec函数族使用区别exit和_exit_exit和exit的区别wait和waitpidfork() 进程创建&…

进程系统调用


文章目录

  • 进程系统调用
  • fork()
    • 进程创建:fock()
    • fork函数
    • fork用法
      • 僵尸进程
      • 孤儿进程
    • vfork函数
    • vfork与fork区别
  • exec函数族
    • exec函数族-何时使用?
    • exec函数族语法
    • exec函数族使用区别
  • exit和_exit
    • _exit和exit的区别
  • wait和waitpid


fork()

进程创建:fock()

#include <sys/types.h> // 提供类型pid_t的定义
#include <unistd.h>pid_t fork(void);

函数返回值
0: 子进程
子进程PID(大于0的整数):父进程
-1: 出错

#include <unistd.h>
#include <stdio.h>int main(){pid_t pid;if((pid=fork())==-1){perror("fork");return -1;}else if(pid == 0){printf("child 子进程\n");printf("child process %d,pid = %d,ppid = %d\n",pid,getpid(),getppid());}else{printf("parent 父进程\n");printf("parent process %d,pid = %d,ppid = %d\n",pid,getpid(),getppid());}return 0;
}

在这里插入图片描述

fork()执行后当前进程会被克隆一遍,克隆之后子进程会有一个与原来相同的代码,父进程与子进程之间是独立调度的,即操作系统独立调度父进程和子进程,先调度谁,后调度谁有一定随机性,此处先调度了父进程,父进程看到的是子进程的pid,3518,然后父进程打印自己的pid和ppid,父进程运行完之后进程结束了,操作系统接着调度子进程,调度子进程的时候,也从fork()返回,但子进程看到的pid是0,所以子进程打印else内容,然后打印自己的pid和ppid

#include <unistd.h>
#include <stdio.h>int main(){pid_t pid;if((pid=fork())==-1){perror("fork");return -1;}else if(pid == 0){printf("child 子进程\n");printf("child process %d,pid = %d,ppid = %d\n",pid,getpid(),getppid());子进程逻辑}else{printf("parent 父进程\n");printf("parent process %d,pid = %d,ppid = %d\n",pid,getpid(),getppid());父进程逻辑}return 0;
}

fork函数

使用fork函数得到的子进程从父进程的继承了整个进程的地址空间,包括:
进程上下文、进程堆栈、内存信息、打开的文件描述符、信号控制设置、进程优先级、进程组号、当前工作目录、根目录、资源限制、控制终端等。
子进程与父进程的区别在于:
1、父进程设置的锁,子进程不继承(因为如果是排它锁,被继承的话,矛盾了)
2、各自的进程ID和父进程ID不同
3、子进程的未决告警被清除;
4、子进程的未决信号集设置为空集

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int glob = 6;
int main()
{int local;int pid;local = 88; printf("parent[pid = %d]: before fork, glob(&glob) = %d(%p), local(&local) = %d(%p)\n",getpid(), glob, &glob, local, &local );if((pid = fork()) < 0) {perror("fail to fork");return -1; }   if(pid == 0) { /* child process */printf("child[pid = %d]: after fork, glob(&glob) = %d(%p), local(&local) = %d(%p)\n",getpid(), glob, &glob, local, &local );glob++;local++;    printf("child[pid = %d]: changed data after fork, glob(&glob) = %d(%p), local(&local) = %d(%p)\n",getpid(), glob, &glob, local, &local );}else { /* parent process */sleep(2);printf("parent[pid = %d]: after fork, glob(&glob) = %d(%p), local(&local) = %d(%p)\n",getpid(), glob, &glob, local, &local );}   /* return euqal to exit(0), but exit may cause a compile warning* due to main() is declared to return with an integter */return 0;  
}

注意

 sleep(2)

目的防止父进程先执行,实现两个进程间的同步

在这里插入图片描述
在这里插入图片描述

虚拟地址相同,但实际物理地址不相同,所以修改后,父进程中变量的值没有被修改

该代码演示了fork函数的使用,fork函数可以复制一个新进程,并在新进程和原进程中运行不同的代码。下面是代码的详细分析:

  1. 包含需要的头文件:stdio.h,stdlib.h和unistd.h。
  2. 声明全局变量glob,并初始化为6。
  3. 定义main函数。
  4. 声明一个名为local的整型变量。
  5. 将变量local初始化为88。
  6. 打印出父进程的进程ID、全局变量glob的值和地址,以及变量local的值和地址。
  7. 如果fork函数返回值小于0,则表示fork失败,打印出错误信息并返回-1。
  8. 如果fork函数返回值等于0,则表示当前代码在子进程中运行。打印出子进程的进程ID、全局变量glob的值和地址,以及变量local的值和地址。
  9. 在子进程中,全局变量glob的值加1,变量local的值加1。
  10. 在子进程中,打印出子进程修改后的全局变量glob的值和地址,以及变量local的值和地址。
  11. 如果fork函数返回值大于0,则表示当前代码在父进程中运行。
  12. 在父进程中,调用sleep函数暂停2秒,以等待子进程修改变量。
  13. 在父进程中,打印出父进程修改前的全局变量glob的值和地址,以及变量local的值和地址。
  14. 最后,在主函数中返回0。

根据代码运行结果,可以看出:

  • 父进程在调用fork函数之前,输出了全局变量glob和变量local的值和地址。

  • 子进程在调用fork函数之后,复制了父进程的地址空间,并打印出全局变量glob和变量local的值和地址。在子进程中,这两个变量的值和地址与父进程相同。

  • 子进程修改了全局变量glob和变量local的值,并打印出它们修改后的值和地址。在子进程中,这两个变量的值和地址已经发生了变化。

  • 父进程在等待2秒钟后继续执行,并打印出全局变量glob和变量local的值和地址。在父进程中,这两个变量的值和地址没有发生变化。

这个结果的原因是,子进程复制了父进程的地址空间,但是子进程和父进程有不同的地址空间。因此,它们可以独立地访问和修改自己的变量。在子进程中修改全局变量和局部变量的值并不会影响父进程的值。

fork用法

僵尸进程

  1. 父进程 Process A 创建子进程 Process B,当子进程退出时会给父进程发送信号 SIGCHLD; 2. 如果父进程没有调用 wait 等待子进程结束,退出状态丢失,转换成僵死状态,子 进程会变成一个僵尸进程
#include <sys/types.h> 
#include <unistd.h> /* create a ZOMBIE * ps -ax | grep a.out to show the zombie */ 
int main() 
{ if(fork()) { // 父进程while(1){ sleep(1); }}   // 子进程
} 

在这里插入图片描述

子进程啥事不干,退出,父进程循环不调用wai回收子进程

表示为僵尸进程
在这里插入图片描述

孤儿进程

如 果 父 进 程 退 出 , 并 且 没 有 调 用 wait 函数 , 它 的 子 进 程 就 变 成 孤 儿 进程 , 会 被 一个 特 殊 进 程 继 承 , 这 就 是 init 进程, init 进程 会 自 动 清理 所 有 它 继 承 的 僵 尸 进 程 。
16.04做了修改由upstart继承
ubuntu14 由init继承】

#include <sys/types.h> 
#include <unistd.h> int main() 
{ if(fork()) { // 父进程}else{ // 子进程while(1){ sleep(1); }}   
} 

在这里插入图片描述
在这里插入图片描述

vfork函数

由于fork完整地拷贝了父进程的整个地址空间,因此执行速度是比较慢的。
为了提高效率, Unix系统设计者创建了vfork。
vfork也创建新进程,但不产生父进程的副本。
它通过允许父子进程可访问相同物理内存从而伪装了对进程地址空间的真实拷贝,当子进程需要改变内存中数据时才拷贝父进程。
这就是著名的“写操作时拷贝” (copy-on-write)技术

vfork与fork区别

关键区别一: vfork 直接使用父进程存储空间,不用拷贝 关键区别二:vfork 保证子进程先运行,当子进程调用 exit 退出后,父进程才执行

我们要创建一个进程,谁来负责fork?
shell
我们在shell下执行的程序,进程又是如何创建装载的?
函数族
strace

exec函数族

exec函数族提供了一种在进程中启动另一个程序执行的方法。
它可以根据指定的文件名或目录名找到可执行文件, 并用它来取代原调用进程的数据段、 代码段和堆栈段。
在执行完之后, 原调用进程的内容除了进程号外, 其他全部都被替换了。
可执行文件既可以是二进制文件, 也可以是任何Linux下可执行的脚本文件。

exec函数族-何时使用?

当进程认为自己不能再为系统和用户做出任何贡献了时就可以调用exec函数,让自己执行新的程序
如果某个进程想同时执行另一个程序,它就可以调用fork函数创建子进程,然后在子进程中调用任何一个exec函数。这样看起来就好像通过执行应用程序而产生了一个新进程一样

exec函数族语法

#include <unistd.h>int execl(const char *path, const char *arg, ...);
int execv(const char *path, char *const argv[]);
int execle(const char *path, const char *arg, ..., char *const envp[]);
int execve(const char *path, char *const argv[], char *const envp[]);
int execlp(const char *file, const char *arg, ...);
int execvp(const char *file, char *const argv[]);

函数返回值 :-1:出错

exec函数族使用区别

可执行文件查找方式
表中的前四个函数的查找方式都是指定完整的文件目录路径,
而最后两个函数(以p结尾的函数)可以只给出文件名, 系统会自动从环境变量“$PATH” 所包含的路径中进行查找。
参数表传递方式
两种方式:
逐个列举或是将所有参数通过指针数组传递
以函数名的第五位字母来区分,
字母为“l”(list)的表示逐个列举的方式;• 字母为“v”(vertor)的表示将所有参数构造成指针数组传递,其语法为char *const argv[]
环境变量的使用
exec函数族可以默认使用系统的环境变量,也可以传入指定的环境变量。
这里,以“e”(Enviromen)结尾的两个函数execle、 execve就可以在envp[]中传递当前进程所使用的环境变量

eg
p
而最后两个函数(以p结尾的函数)可以只给出文件名, 系统会自动从环境变量“$PATH” 所包含的路径中进行查找。
l
字母为“l”(list) 表示参数逐个列举的方式;
v
字母为“v”(vertor)的表示将所有参数构造成指针数组传递,其语法为char *const argv[]

echo $PATH

在这里插入图片描述

#include <stdio.h>
#include <unistd.h>int main()
{if (execlp("ps", "ps", "-ef", NULL) < 0){    perror("execlp error!");}   return 0;
}

在这里插入图片描述

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>int main(int argc,char **argv)
{pid_t pid;printf("PID = %d\n",getpid());pid=fork();if(pid==0){   //子进程execvp("ls",argv);    // ./r -ef        
//      execv("/bin/ls",argv);  ./run -l 
//      execl("/bin/ls","ls","-l","/",NULL);//指令路径 指令 参数   目录   空
//      execlp("ls","ls","-al","/",NULL);sleep(10);}else if(pid!=-1){   //父进程        printf("\nParrent porcess,PID = %d\n",getpid());}else{   printf("error fork() child proess!");}   return 0 ; 
}

在这里插入图片描述
在这里插入图片描述

exit和_exit

所需头文件 exit: #include <stdlib.h>_exit: #include <unistd.h>函数原型 exit: void exit(int status);_exit: void _exit(int status);函数传入值 status是一个整型的参数,可以利用这个参数传递进程结束时的状态。通常0表示正常结束;其他的数值表示出现了错误,进程非正常结束。在实际编程时,可以用wait系统调用接收子进程的返回值,进行相应的处

在这里插入图片描述

_exit和exit的区别

_exit() :
直接使进程终止运行,清除其使用的内存空间,并销毁其在内核中的各种数据结构;
exit()
在这些基础上作了一些包装,在执行退出之前加了若干道工序。 exit()函数在调用exit系统调用之前要检查文件的打开情况,把文件缓冲区中的内容写回文件,就是图中的"清理I/O缓冲"一项。

int main(){printf("Using exit...\n");printf("This is the end");exit(0);
}

在这里插入图片描述

#include <stdio.h>
#include <inistd.h>int main(){printf("Using _exit...\n");printf("This is the end");_exit(0);
}

在这里插入图片描述

wait和waitpid

wait和waitpid
wait函数
调用该函数使进程阻塞,直到任一个子进程结束或者是该进程接收到了一个信号为止。如果该进程没有子进程或者其子进程已经结束, wait函数会立即返回。
waitpid函数
功能和wait函数类似。可以指定等待某个子进程结束以及等待的方式(阻塞或非阻塞)

waitpid.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>/* wait method: *//* waitpid*/
int main(int argc, char **argv)
{pid_t pid;printf("parent[pid=%d] is born\n", getpid());if (-1 == (pid = fork())) {perror("fork error");return -1;}if (pid == 0){//子进程printf("child[pid=%d] is born\n", getpid());sleep(5);printf("child is over\n");}else{ //parent  父进程pid_t pid_w;while((pid_w = waitpid(pid, NULL, WNOHANG)) == 0) {printf("parent wait w/o HAND and returns with 0\n");sleep(1);}printf("waitpid returns with pid = %d.\n", pid_w);printf("father is over\n");}return 0;
}

在这里插入图片描述

wait_z.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>/* no wait and let zombie be */int main(int argc, char **argv)
{pid_t pid;printf("parent[pid=%d] is born\n", getpid());if (-1 == (pid = fork())) {perror("fork error");return -1;}if (pid == 0){printf("child[pid=%d] is born\n", getpid());sleep(10);printf("child is over\n");}else{while(1){};}return 0;
}

在这里插入图片描述

wait.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>/* wait */int main(int argc, char **argv)
{pid_t pid;int status;printf("parent[pid=%d] is born\n", getpid());if (-1 == (pid = fork())) {perror("fork error");return -1;}if (pid == 0){printf("child[pid=%d] is born\n", getpid());sleep(10);printf("child is over\n");exit(7);return 123;}else{pid_t pid_w;pid_w = wait(&status);//等待子进程退出 0x7b00if (pid_w < 0) {perror("wait error");return 1;}printf("status=%x \n",status);if (WIFEXITED(status)) {//正常退出status = WEXITSTATUS(status);//提取返回信息printf("wait returns with pid = %d. return status is %d\n", pid_w, status);} else {//非正常退出printf("wait returns with pid = %d. the child is terminated abnormally\n", pid_w);}        printf("father is over\n");        return 0;}
}

在这里插入图片描述


文章转载自:
http://neigh.c7500.cn
http://turki.c7500.cn
http://phocine.c7500.cn
http://hooded.c7500.cn
http://patriotic.c7500.cn
http://convivial.c7500.cn
http://continuity.c7500.cn
http://themselves.c7500.cn
http://excrescency.c7500.cn
http://exactitude.c7500.cn
http://end.c7500.cn
http://mannar.c7500.cn
http://pirozhki.c7500.cn
http://bulkhead.c7500.cn
http://unlighted.c7500.cn
http://posho.c7500.cn
http://payoff.c7500.cn
http://preceding.c7500.cn
http://palawan.c7500.cn
http://solidungulate.c7500.cn
http://swank.c7500.cn
http://unnational.c7500.cn
http://econometrical.c7500.cn
http://suboceanic.c7500.cn
http://balloon.c7500.cn
http://observer.c7500.cn
http://socialistic.c7500.cn
http://dialecticism.c7500.cn
http://vacuolate.c7500.cn
http://chart.c7500.cn
http://apsidal.c7500.cn
http://untraceable.c7500.cn
http://intraday.c7500.cn
http://dipsomania.c7500.cn
http://neutralist.c7500.cn
http://fawn.c7500.cn
http://bebeeru.c7500.cn
http://overabundance.c7500.cn
http://sidecar.c7500.cn
http://ley.c7500.cn
http://reapportionment.c7500.cn
http://lognitudinal.c7500.cn
http://unzealous.c7500.cn
http://anglomaniacal.c7500.cn
http://araucaria.c7500.cn
http://goldilocks.c7500.cn
http://devastator.c7500.cn
http://remake.c7500.cn
http://fluidic.c7500.cn
http://bassoon.c7500.cn
http://rimbaldian.c7500.cn
http://continentality.c7500.cn
http://litharge.c7500.cn
http://succour.c7500.cn
http://tutiorism.c7500.cn
http://andron.c7500.cn
http://outrigger.c7500.cn
http://funiform.c7500.cn
http://eschatology.c7500.cn
http://cathedra.c7500.cn
http://epidemic.c7500.cn
http://strangle.c7500.cn
http://convertaplane.c7500.cn
http://polymeric.c7500.cn
http://sapa.c7500.cn
http://piccolo.c7500.cn
http://tripe.c7500.cn
http://trinary.c7500.cn
http://chapatty.c7500.cn
http://nastic.c7500.cn
http://thoth.c7500.cn
http://companionable.c7500.cn
http://sitsang.c7500.cn
http://tagmemicist.c7500.cn
http://harmoniser.c7500.cn
http://reticule.c7500.cn
http://pyrolatry.c7500.cn
http://pedigreed.c7500.cn
http://bracket.c7500.cn
http://fertilization.c7500.cn
http://acrocentric.c7500.cn
http://malaria.c7500.cn
http://monopolizer.c7500.cn
http://anzac.c7500.cn
http://monaul.c7500.cn
http://molasse.c7500.cn
http://docetae.c7500.cn
http://soave.c7500.cn
http://wafd.c7500.cn
http://unenvied.c7500.cn
http://upsweep.c7500.cn
http://unwise.c7500.cn
http://spiniform.c7500.cn
http://diagnostician.c7500.cn
http://tuck.c7500.cn
http://photoisomerization.c7500.cn
http://polyomino.c7500.cn
http://discharge.c7500.cn
http://counterpane.c7500.cn
http://dilatancy.c7500.cn
http://www.zhongyajixie.com/news/68308.html

相关文章:

  • 建立团购网站交换友情链接推广法
  • wordpress设置标题字体大小seo免费自学的网站
  • 自己写代码做网站纹身网站设计
  • 网站建设能带来流量么最近一周新闻大事件
  • 263企业邮箱报价天津seo优化公司哪家好
  • 网站设计定制多少钱竞价网站
  • 通用wap网站生成系统企业类网站有哪些例子
  • 全国b2c网站建设b站视频推广网站动漫
  • 在哪找做调查赚钱的网站好google seo优化
  • 免费建立微信网站今日头条郑州头条新闻
  • wordpress虚拟币接口优化什么
  • 哪些企业网站做的不错郑州seo技术顾问
  • 做一个这样的网站应该报价多少营销策划方案
  • 网页设计的培训机构运营seo是什么意思
  • 网站开发中如何设计验证码百度域名收录提交入口
  • 食品网站建设的照片市场seo是什么
  • 网站必须做API接口吗网站推广软件
  • 开封企业网络推广方案襄阳网站推广优化技巧
  • 尚层别墅装饰seo友情链接
  • 做赌博网站代理网站seo视频
  • 武汉专业做网站公司西安百度推广怎么做
  • 石家庄做公司网站普通话手抄报文字内容
  • php网站建设英文文献青岛seo排名扣费
  • 用asp做的网站怎么做电商卖东西
  • 高端网站建设专业网站推广平台搭建
  • 济南网站制作*推搜点seo是什么意思中文
  • 湖南做网站问磐石网络专业友情链接代码
  • 郑州免费网站建设哪家好大型网站建设
  • 自己做网站练手seo网络贸易网站推广
  • 网站开发软硬件seo优化器