实验2 进程的管理

题目一

编写代码,实现以下功能:

  1. 打印当前所有环境变量的值;

  2. 添加新的环境变量NEWENV=first;

  3. 修改环境变量NEWENV的值为second;

  4. 打印环境变量NEWENV的值。

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <unistd.h>
    extern char **environ;
    
    int main() {
    	char **env = environ;
    	while(*env) {
    		printf("The env is: %s\n",*env);
    		env++;
    	}
    	putenv("NEWENV=first");
    
    	char *str;
    	str = getenv("NEWENV");
    	printf("The NEWENV is: %s\n",str);
    
    	if(setenv("NEWENV","second",1) < 0)
    		perror("setenv");
    
    	str = getenv("NEWENV");
    	printf("The NEWENV is: %s\n",str);
    
    	return 0;
    }
    

运行截图

1

2

题目二

编写代码实现以下功能:

  1. 打印字符串“hello world!”

  2. 在打印字符串“hello world!”前调用三次fork,分析打印结果。

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    int main() {
    	fork();
    	fork();
    	printf("hello world!!!\n");
    	return 0;
    }
    

    运行截图

    3

    结果分析

    我们在编写程序之前,我们先分析一下,应该会打印出几个“hello world”。

    首先我们要了解fork函数的工作机制。首先,调用一次fork函数会返回两个返回值,父进程会返回子进程的pid,子进程会返回0。

    父子进程都会执行fork函数下的第一条语句。

    父子进程的执行顺序是不确定的,取决于操作系统的调度。

    综上所诉,调用三次fork数后,将会打印八个“hello world”。

题目三

编写代码实现以下功能:

  1. 创建子进程

  2. 在子进程中打开文件file1,写入自己的“班级_姓名_学号”,

  3. 父进程读取file1中的内容,并且打印显示。

  4. 在父进程中获取已经结束的子进程的状态信息,打印该信息,并且打印结束的子进程的进程号。

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    int main() {
    	int fd,pid;
    	fd = open("file.txt",O_CREAT|O_RDWR,S_IRWXU);
    	if(fd< 0)
    		perror("open");
    	pid = fork();
    
    	if(pid  == 0) {
    		printf("This is the child!\n");
    		char str[128] = "这里填写自己的信息\n";
    
    		if(write(fd,str,128) < 0)
    			perror("write");
    
    		exit(5);
    	} else {
    		printf("This is the father!\n");
    
    		char buf[128];
    		int n,status;
    		if(read(fd,buf,128) < 0)
    			perror("read");
    		printf("The buf is: %s\n",buf);
    
    		if(wait(&status) < 0)
    			perror("perror");
    
    		if(WIFEXITED(status))
    			n = WEXITSTATUS(status);
    		else
    			printf("wait error!\n");
    
    		printf("The child's pid is: %d\n",pid);
    		printf("The child exit status is: %d\n",n);
    	}
    	return 0;
    }
    

    运行截图

    4

题目四

编写程序实现以下功能:

  1. 在父进程中定义变量n,在子进程中对变量n进行++操作;并且打印变量n的值,打印子进程pid;

  2. 在父进程中打印变量n的值,并且打印父进程pid。

  3. 要求分别用fork和vfork创建子进程。

    fork.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    int main() {
    	int n = 1;
    	if(fork() == 0) {
    		printf("This is child,the pid is%d\n",getpid());
    		printf("The n is: %d\n",++n);
    	} else {
    		printf("This is father,the pid is%d\n",getpid());
    		printf("The n is: %d\n",n);
    	}
    
    	return 0;
    }
    

    vfork.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main() {
    	int n = 1;
    	pid_t pid;
    	pid = vfork();
    	if(pid < 0)
    		perror("vfork");
    	else if(pid == 0) {
    		printf("This is child,the child's pid is: %d\n",getpid());
    		printf("The n is: %d\n",++n);
    		exit(0);
    	} else {
    		printf("This is father,the father's pid is: %d\n",getpid());
    		printf("The n is: %d\n",n);
    	}
    	return 0;
    }
    

    运行截图

    5

    程序结果分析:

    假设变量n的值为1,在vfork函数中,父进程打印的n值是2,而fork函数中的父进程打印的n值是1。

题目五

编写程序实现以下功能:

  1. 创建子进程一,在子进程中递归打印/home目录中的内容(用exec系列函数调用第二次实验中的代码完成此功能);

  2. 子进程结束的时候完成以下功能:

  3. 打印字符串“Child process exited!”

  4. 打印子进程标识符,打印父进程标识符。

  5. 创建子进程二, 打印子进程运行环境中环境变量“USER”的值,通过exec系列中的某个函数设置子进程”USER”环境变量值为“zhangsan”,并且让该子进程完成以下命令:“ls –li /home”.

    a1.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    void test1(void ) {
    	printf("This is the test1!\n");
    }
    
    void test2(void) {
    	printf("This is the test2!\n");
    }
    
    int main() {
    	if(fork() == 0) {
    		printf("This is the child!\n");
    		atexit(test2);
    		atexit(test1);
    		exit(1);
    	} else {
    		printf("This is father!\n");
    	}
    	return 0;
    }
    

    a2.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    void fun(void) {
    	printf("\n");
    	printf("Child process exited!!!\n");
    	printf("The child's pid is: %d\n",getpid());
    	printf("The father's pid is %d\n",getppid());
    	printf("\n");
    }
    
    int main() {
    	pid_t pid;
    	pid = vfork();
    	if(pid <0)
    		perror("vfork");
    	else if(pid == 0) {
    		printf("This is the child1 !!!\n");
    		atexit(fun);
    
    		if((execl("/home/jia/Desktop/jincheng/5","5",NULL)) < 0) {
    			perror("execl");
    			exit(0);
    		}
    	} else {
    		printf("This is the father !!!\n");
    		if(vfork() == 0) {
    			printf("This is the child2 !!!\n");
    			printf("The child2's father's pid is: %d\n",getppid());
    			char * env[] = {"USER=zhangsan",NULL};
    			char *p;
    
    			p = getenv("USER");
    			if(p) {
    				printf("The user is: %s\n",p);
    			}
    
    			system("ls -li /home");
    			if((execle("/bin/env","env",NULL,env)) < 0)
    				perror("execle");
    
    			exit(1);
    		}
    	}
    	return 0;
    }
    

    运行截图

    6

Logo

更多推荐