实验六 进程基础

|这个作业属于哪个课程|内容|
| ---- | ---- | ---- |
|这个作业属于哪个课程|2021春季Linux系统与应用 (南昌航空大学 - 信息工程学院)|
|这个作业要求在哪里|实验六 进程基础|
|学号-姓名|18043215-李国鹏|
|作业学习目标|1.掌握Linux系统环境C语言编程概念 2.学习Linux系统进程概念|

1。请举例说明静态链接库的创建与使用。

答:
ar:静态函数库创建的命令
-c :create
-r :replace
表示当前插入的模块名已经在库中存在,则替换同名的模块;
如果若干模块中有一个模块在库中不存在,ar显示一个错误信息,并不替换其他同名的模块。

2。请举例说明共享库的创建与使用。

3.编程实现一个简单文件复制命令。



比较复制前后的文件

4.使用fork创建一个子进程,进程创建成功后父子进程分别输出不同的内容



全缓冲:
指的是系统在填满标准IO缓冲区之后才进行实际的IO操作;对于驻留在磁盘上的文件来说通常是由标准IO库实施全缓冲。
行缓冲:
在这种情况下,标准IO在输入和输出中遇到换行符时执行IO操作;当流涉及终端的时候,通常使用的是行缓冲。

删除fflush(NULL);//查看运行结果区别?
答:./fork1 > /tmp/out 输出结果删除后子程序工作前多个 Begin!

进一步删除15行 \n ,查看运行结果区别?
答:删除后 Begin! 与 父进程、子进程间不换行

5.使用fork创建多个子进程。

int i; 
pid_t pid; 
for (i = 0; i < 3; i++) 
pid = fork();

上面代码段会产生多少子进程?
答:2^3-1=7,产生7个子进程

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
	int i;
	pid_t pid;
	printf("[%d] Begin! \n",getpid());
	for (i = 0;i < 3; i++)
	{
		if((pid = fork()) ==0 )
			break;
	}
	if(pid<0)
	{
		perror("fork()");
		exit(1);
	}
	else if(pid > 0)
	{
		printf("[%d] Parent process is working!\n",getpid());
	}
	else
	{
		printf("[%d] Child process %d is working!\n",getpid(),i);
	}
	return 0;
}


使用sleep函数简单控制进程输出顺序

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
	int i;
	pid_t pid;
	printf("[%d] Begin! \n",getpid());
	for (i = 0;i < 3; i++)
	{
		if((pid = fork()) ==0 )
			break;
	}
	if(pid<0)
	{
		perror("fork()");
		exit(1);
	}
	else if(pid > 0)
	{
		sleep(3);
		printf("[%d] Parent process is working!\n",getpid());
	}
	else
	{
		sleep(i);
		printf("[%d] Child process %d is working!\n",getpid(),i+1);
	}
	return 0;
}

6.在 fork 之前以写的方式创建了一个文件 test.txt。然后 fork 出的子进程立即向文件中写入“world”,然后睡眠5秒。而父进程在 fork 后睡眠3秒后向 test.txt 写入 "hello",并关闭描述符。子进程恢复后,又向 test.txt 文件中写入 "lalala"后关闭描述符,结束。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main() {
	int fd = open("test.txt",O_WRONLY | O_CREAT,0664);
	if (fd == -1){
		perror("open");
		return 1;
	}
	printf("I'm father\n");
	printf("before fork\n");
	pid_t pid = fork();
	if (pid > 0){
	sleep(3);
	printf("I'm father; I'm writing test.txt...\n");
	write(fd, "hello", 5);
	close(fd);
	}
	else if (pid ==0){
	printf("I'm child; I'm writing test.txt...\n");
	write(fd, "world", 5);
	sleep(5);
	write(fd, "lalala", 6);
	close(fd);
	}
	else {
		perror("fork");
		return 1;
	}
	return 0;
}


7.分别在主函数中使用execvp启动ls命令以及使用fork函数产生子进程调用execvp启动ls。

execvp 启动 ls 命令以及使用 fork 函数产生子进程调用 execvp 启动 ls 。
程序调用execvp,实现一个程序运行另一个程序

(1)使用execvp启动ls命令

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
	char* argv[] = {"ls","-l",NULL};
	if (execvp("ls",argv) == -1){
	perror("exec");
	return 1;
	}
	return 0;
}


(2)使用fork函数产生子进程调用execvp启动ls

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
	char* argv[] = {"ls","-l",NULL};
	pid_t pid = fork();
	if (pid > 0){
		printf("I'm father\n");
	}
	else if (pid == 0) {
		printf("I'm child\n");
		if (execvp("ls",argv) == -1){
			perror ("exec");
			return 1;
		}
	}
	else {
		perror("fork");
		return 1;
	}
	return 0;
}


8.创建5个僵尸进程,并在终端通过ps axf命令查看僵尸进程信息。

#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main() {
	printf("before fork\n");
	pid_t pid, n = 5;
	while(n--) {
		pid = fork();
		if (pid == 0)
			break;
		else if (pid < 0){
			perror("fork");
			return 1;
		}
	}
	if (pid == 0) {
	printf("hello, I'm child %d; my father is %d\n", getpid(),getppid());
	//getpid()  获取当前进程的pid
	//getppid() 获取当前进程的父进程的pid
	return 0;
	}
	while(1) {
		sleep(3);
		printf("hello, I'm father %d\n", getpid());
	}
	return 0;
}


ps axf 显示进程见关联的树状结构图
另开一终端输入 ps axf 查看僵尸进程,显示如下:

9.通过wait来清理僵尸进程。

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
int main() {
	printf("before fork\n");
	pid_t pid, n = 5;
	while(n--) {
		pid = fork();
		if (pid == 0)
			break;
		else if (pid < 0) {
			perror("fork");
			return 1;
		}
	}
	if (pid == 0) {
		printf("hello, I'm child %d;my father is %d\n",getpid(),getppid());
		return 0;
	}
	while(1) {
		sleep(3);
		pid = wait(NULL);
		if (pid == -1) {
			perror("wait");
			sleep(10);
			printf("I'm father %d;I have wiped out all zombies\n",getpid());
			return 1;
		}
		printf("Hello, I'm father %d; child %d exit\n",getpid(),pid);
	}
	return 0;
}


10.父进程通过waitpid函数等待特定子进程结束,若该子进程不结束,父进程一直阻塞。

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
void handler(int sig)
{
	pid_t pid;
	while ((pid = waitpid(-1,NULL,WNOHANG)) > 0)
	{
		printf("wait child sucess : %d\n",pid);
		
	}
}
int main()
{
	signal(SIGCHLD,handler);
	pid_t pid = fork();
	if (pid == 0)
	{
		printf("child1 pid : %d\n",getpid());
		sleep(3);
		exit(1);
	}
	pid_t pid2 = fork();
	if (pid2 == 0)
	{
		printf("child2 pid2 : %d\n",getpid());
		sleep(5);
		exit(2);
	}
	pid_t pid3 = fork();
	if (pid3 == 0)
	{
		printf("child3 pid3 : %d\n",getpid());
		sleep(7);
		exit(3);
	}
	printf("father pid : %d\n",getpid());
	while (1)
	{
		printf("father do self\n");
		sleep(1);
	}
	return 0;
}


posted on 2021-06-20 21:31  菜鸡小鹏  阅读(65)  评论(0编辑  收藏  举报