头歌实践教学平台——Linux之进程管理二

目录

第1关:进程等待

第2关:进程创建操作-exec函数族

第3关:进程创建操作-system

第4关:实现一个简单的命令解析器 


第1关:进程等待

任务描述
通过上一个实训的学习,我们学会了使用fork和vfork创建子进程,在使用fork创建子进程的时候,子进程和父进程的执行顺序是无法预知的。本关我们将介绍如何使得fork创建出来的子进程先执行,随后父进程再执行。

本关任务:学会在多进程中,学会进程的等待处理。

编程要求
本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下:

补全waitProcess函数,等待子进程结束,并且返回子进程的退出的代码。

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
/************************
 * 返回值: 调用成功且子进程正常退出返回退出代码,否则返回-1
*************************/
int waitProcess()
{
	int status = -1;
	/********** BEGIN **********/
	 pid_t pid;
    pid = fork();
    if(pid == -1){
        //创建进程失败
        return -1;
    }
    else if(pid == 0){
        //子进程
        sleep(2);
        printf("This is child process\n");
        exit(1);
    }
    else{
        //父进程
        if(waitpid(-1, &status, 0) != -1){
            if(WIFEXITED(status))
                return WEXITSTATUS(status);
        }
        exit(0);
    }
	/********** END **********/
	return status;
}

第2关:进程创建操作-exec函数族

任务描述
在上一个实训中我们学习使用vfork函数创建新进程,本关我们将介绍如何另一种创建新进程的库函数。

本关任务:学会使用C语言在Linux系统中使用exec族的系统调用创建一个新的进程。

编程要求
本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下:

补全execlProcess函数,使用vfork函数创建进程,并在子进程中调用创建一个名为testDir的目录,在父进程中输出"Parent Process"字符串。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
 
/************************
 * 提示: 在子进程中如果执行exec函数失败要使用exit函数正确退出子进程
*************************/
int execlProcess()
{
	pid_t pid = vfork();
	if(pid == -1){
		printf("创建子进程失败(%s)\n", strerror(errno));
		return -1;
	}
	else if(pid == 0){
		//子进程
		/********** BEGIN **********/
		if(execlp("touch", "touch", "testFile",  NULL) < 0){
            //执行execlp函数失败
            exit(-1);
        }
		
		/********** END **********/
	}
	else{
		//父进程
		/********** BEGIN **********/
		printf("Parent Process");
		/********** END **********/
	}
}

第3关:进程创建操作-system

任务描述
在上一关中我们学习使用exec函数族执行新的程序,本关我们将介绍如何另一种执行新的程序的库函数。

本关任务:学会使用C语言在Linux系统中使用system库函数来执行一个新的程序。

编程要求
本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下:

补全createProcess函数,使用system函数创建一个名为testDir的目录(** 调用成功返回命令的状态码,失败返回-1**)。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
 
/************************
 * 返回值: 调用成功返回命令的状态码,失败返回-1
*************************/
int createProcess()
{
	int ret = -1;
	/********** BEGIN **********/
	ret=system("mkdir testDir");
    if(ret == -1){
        return -1;
    }
	/********** END **********/
	return ret;
}

第4关:实现一个简单的命令解析器 

任务描述
本关任务:编写一个简单的命令解析器工具RShell,用于读取用户输入的简单命令,然后执行命令,并将命令执行结果打印出来。

编程要求
本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下:

补全RShell函数,实现一个简单的命令解析器工具。(提示:可以使用system函数或者fork+exec+wait。)

#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
 
void RShell(char *cmd[], int commandNum)
{
    pid_t pid;
    //for (int i = 0; i < commandNum; i++) {
    int i = 0;
    while(commandNum) {
        pid = fork();
        if (pid == -1) {
            printf("创建进程失败(%s)!", strerror(errno));
            return -1;
        } else if (pid == 0) {
            char *file;
            char *point = NULL;
            char *tmpArg[10];
            point = strtok(cmd[i], " ");
            file = point;
            int index = 0;
            while (point != NULL) {
                if (index == 9)
                    break;
                tmpArg[index++] = point;
                point = strtok(NULL, " ");
            }
            char **arg = malloc(sizeof(char *) * (index + 1));
            int i;
            for (i = 0; i < index; i++)
                arg[i] = tmpArg[i];
            arg[index] = NULL;
            if (execvp(file, arg) < 0) {
                //执行execvp函数失败
                exit(-1);
            }
        } else {
            int status;
            wait(&status);
        }
        commandNum--;
        i++;
    }
}
### 头歌平台第一进程等待的解决方案 在多线程或多进程编程环境中,处理进程间的协调是一个重要课题。对于指定条件下的子进程等待机制[^1],可以利用操作系统提供的`wait()`或`waitpid()`系统调用来实现特定子进程结束后的通知接收。 针对头歌平台上提到的第一涉及的进程等待问题,通常会涉及到父进程如何有效地管理其创建的一个或多个子进程的状态监控与资源回收。具体到该场景下: - 当需要等待特定编号的子进程完成时,可以通过传递正整数值给`waitpid(pid, status, options)`函数来精确控制要监听哪个子进程; - 若要让父进程暂停直到任一子进程终止,则可设置`pid=-1`作为参数传入上述API;这使得父进程能灵活响应最先结束的那个子程序实例而不必事先知晓确切ID。 此外,在并发计算模型里引入了管程的概念用于保护临界区内的操作免受竞争条件影响[^2]。虽然这不是直接解决“等待”的方法,但在构建复杂的IPC(Inter-process Communication)应用时,合理运用此类同步原语同样有助于确保整个系统的稳定性和正确性。 为了更直观地理解这一过程,下面给出一段简单的C语言代码片段展示基本用法: ```c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> int main() { pid_t child_pid; // 创建新子进程 switch (child_pid = fork()) { case -1: perror("fork error"); exit(EXIT_FAILURE); case 0: /* 子进程中 */ printf("Child process ID is %d\n", getpid()); sleep(5); // 模拟工作负载延迟 _exit(EXIT_SUCCESS); default: /* 父进程中 */ int stat_val; pid_t returned_pid; // 使用 waitpid 函数等待进程退出 returned_pid = waitpid(child_pid, &stat_val, WUNTRACED | WCONTINUED); if (-1 == returned_pid) { perror("waitpid error"); exit(EXIT_FAILURE); } printf("Parent got signal of child's termination.\n"); break; } return EXIT_SUCCESS; } ``` 此示例展示了通过`waitpid()`函数使父进程安全地获取子进程状态变化的通知方式,并适当加入了错误检测逻辑以增强健壮性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值