5、数,又创建一个进程forktree,称之为第二孙进程,并获得处理机执行,此进程很快执行完,将处理机还给子进程,子进程也很快执行完,将处理机还给父进程,父进程P>0执行if语句,运行fork()函数,又创建一个进程forktree,称之为第二子进程,此进程获得处理机执行很快运行完,将处理机还给父进程,父进程运行sleep(15)语句,休眠15秒,用pstree命令查询进程树。3、运行程序,分析运行结果。#include main(){ int p,x,ppid,pid; x=0; p=fork(); if(p>0)
6、 { printf("parent output x=%d",++x); ppid=getpid(); printf("Thi id number of parent is:ppid=%d",ppid); } else { printf("child output x=%d",++x); pid=getpid(); printf("Thi id number of child is:pid=%d",pid); }} 运行结果:Parent output x=
7、1This id number of parent is:ppid=3110Child output x =1This is number of child is:pid=3111分析:fork创建进程的时候子进程与父进程共享代码区,子进程复制父进程的数据区,所以,两个进程中的数据互不影响都是1。4、loop.c#include main(){ while(1){ };{实验步骤:编译 gcc loop.c –o loop后台运行 ./loop &(可多次使用该命令)多次使用ps命令查看进程状态结果:F S UI
8、D PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD0 S 1000 2645 2643 0