2019-2020-1 学号《Linux内核原理与分析》第五周作业

实验

实验一

课上布置了一个作业,是学习waitpid的方法,当时不懂如何看函数的使用方法,结果直接调用了没有参数的waitpid()函数,结果出现了错误,最后查看了man命令的帮助,结果如下:

最后明白了参数的含义,并且参照以下网上的代码完成了任务,参考代码如下:

#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include<stdio.h>
main()
{
	int status;
        pid_t pc,pr;
        pc=fork();
 
       	if(pc<0) /* 如果出错 */
               printf("error ocurred!/n");
    	  else if(pc==0){ /* 如果是子进程 */
        	       printf("This is child process with pid of %d
",getpid());
       		       sleep(10); /* 睡眠10秒钟 */
           		 }
   else{ /* 父进程 */
           pr=waitpid(pc,&status,0);
           if(WIFEXITED(status)){ /* 如果WIFEXITED返回非零值 */
                   printf("the child process %d exit normally.
",pr);
                   printf("the return code is %d.
",WEXITSTATUS(status));
           }else /* 如果WIFEXITED返回零 */
               printf("the child process %d exit abnormally.
",pr);
    }
    
       exit(0);
}

实验二

编译时出现了一下问题:

上网百度后,发现时因为gcc版本太高,Linux内核版本太低,故我这里解决方法是降低gcc版本。
代码如下:

sudo apt-get install gcc-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 100
原文地址:https://www.cnblogs.com/ring3toring0/p/13945957.html