实验五 实验名称:进程管理实验报告:实验要求: cat /etc/group (查看组信息)1. 编写一个程序,打印进程的如下信息:进程标识符,父进程标识符,真实用户ID,有效用户ID,真实用户组ID,有效用户组ID并分析真实用户ID和有效用户ID的区别代码如下:#include#includeint main(){printf("***********\n");printf("This is the process\n");printf(" pid=%d\n",getpid());printf("ppid=%d\n",getppid());printf(" uid=%d\n",getuid());printf("euid=%d\n",geteuid());printf(" gid=%d\n",getgid());printf("egid=%d\n",getegid());}真实用户ID和有效用户ID的区别:真实用户ID:这个ID就是我们登陆unix系统时的身份ID 有效用户ID:定义了操作者的权限。
有效用户ID是进程的属性,决定了该进程对文件的访问权限.2. 阅读如下程序:/* process using time */#include#include#include#include#includevoid time_print(char *,clock_t);int main(void){ clock_t start,end; struct tms t_start,t_end; start = times(&t_start); system(“grep the /usr/doc/*/* > /dev/null 2> /dev/null”); // > 将信息放到该文件null中 end=times(&t_end); // 0 1 2 标准输入 标准输出 错误输出 time_ print(“elapsed”,end-start); puts(“parent times”); time _print(“\tuser CPU”,t_end.tms _utime); time_ print(“\tsys CPU”,t_end.tms_stime); //获得执行system()的子进程ID puts(“child times”); time_print(“\tuser CPU”,t_end.tms_cutime); time_print(“\tsys CPU”,t_end.tms_cstime); exit(EXIT_SUCCESS);}void time_print(char *str, clock_t time){ long tps = sysconf(_SC_CLK_TCK);/*函数sysconf()的作用为将时钟滴答数转化为秒数,_SC_CLK_TCK 为定义每秒钟有多少个滴答的宏*/ printf(“%s: %6.2f secs\n”,str,(float)time/tps);}编译并运行,分析进程执行过程的时间消耗(总共消耗的时间和CPU消耗的时间),并解释执行结果。
再编写一个计算密集型的程序替代grep,比较两次时间的花销注释程序主要语句 因为该程序计算量很小,故消耗的时间比较少,均为0.00secs 不奇怪而更改为计算密集型的之后就较容易观察出消耗时间的差异,如图所示 3. 阅读下列程序:/* fork usage */#include#include#includeint main(void){ pid_t child; if((child=fork())==-1{ perror(“fork”); exit(EXIT_FAILURE); }else if(child==0){ puts(“in child”); printf(“\tchild pid = %d\n”,getpid()); //取得目前进程的进程ID printf(“\tchild ppid = %d\n”,getppid());//取得目前进程的父进程ID exit(EXIT_SUCCESS); }else{ puts(“in parent”); printf(“\tparent pid = %d\n”,getpid()); printf(“\tparent ppid = %d\n”,getppid()); } exit(EXIT_SUCCESS);}编译并多次运行,观察执行输出次序,说明次序相同(或不同)的原因;观察进程ID,分析进程ID的分配规律。
总结fork()的使用方法注释程序主要语句创建进程ID开始时一般随机分配,但若多次运行,或创建子进程时,会顺序分配内存此外,当父进程结束时,子进程尚未结束,则子进程的父进程ID变为1,即initfork()的使用方法:fork()会产生一个新的子进程,其子进程会复制父进程的数据与堆栈空间,如果fork()成功则在父进程会返回新建立的子进程代码(PID),而在新建立的子进程中则返回0如果fork 失败则直接返回-1,失败原因存于errno中 在父进程中用fork()创建子进程,通过返回值 if语句判断来进行父子进程代码执行4. 阅读下列程序:/* usage of kill,signal,wait */#include#include#include#includeint flag; void stop(); //该函数是自定义的一个 single()触发的自定义函数int main(void){ int pid1,pid2; //定义了两个进程号参数 signal(3,stop); //signal() 触发软中断 while((pid1=fork()) ==-1); //程序等待成功创建子进程事件的发生 if(pid1>0){ while((pid2=fork()) ==-1); if(pid2>0){ //当前进程为父进程,父进程发出两个中断信号Kill子进程 flag=1; sleep(5); kill(pid1,16); kill(pid2,17); wait(0); //等待子进程死信号 wait(0); printf(“\n parent is killed\n”); //接收到子进程死信号后,杀死父进程 exit(EXIT_SUCCESS); }else{ //当前进程为子进程,则发送子进程Kill信号,杀死该子进程2 flag=1; signal(17,stop); printf(“\n child2 is killed by parent\n”); exit(EXIT_SUCCESS); } }else{ //当前进程为子进程,则发送子进程Kill信号,杀死该子进程1 flag=1; signal(16,stop); printf(“\n child1 is killed by parent\n”); exit(EXIT_SUCCESS); }}void stop(){ //自定义函数,供signal()调用 flag = 0; }编译并运行,等待或者按^C,分别观察执行结果并分析,注释程序主要语句。
flag有什么作用?通过实验说明 每个进程(父进程,子进程)都有一个flag,起状态标志作用,flag=1时,表示进程在运行,flag=0,表示进程结束5. 编写程序,要求父进程创建一个子进程,使父进程和子进程各自在屏幕上输出一些信息,但父进程的信息总在子进程的信息之后出现分别通过一个程序和两个程序实现)代码如下: Ptest.c ---------------一个程序实现方案(fork())#include#include#includemain(){ int p,i; while((p=fork())==-1); //创建子进程直至成功 if(p>0) { wait(0); printf("***\n"); printf("The parent process!\n"); printf("***\n"); exit(0); } else{ printf("***\n"); printf("The child process!\n"); printf("***\n"); sleep(3); exit(0); }}/////////////////////////////////////////////////////////////////////////////////////Ptest2.c ------------------两个程序实现方案(execl())#include#include#includeint main(int argc,char *argv[]){ int p,i; while((p=fork())==-1); //创建子进程直至成功 if(p>0) { wait(0); printf("***\n"); printf("The parent process!\n"); printf("***\n"); exit(0); } else{ printf("***\n"); printf("The child process!\n"); execl("/home/xingkong/ptest22",argv[1],。