简体   繁体   English

在C中实现类似Shell的作业控制

[英]Implementing shell-like job control in C

I am trying to implement simple shell in C language and i am having a hard time implementing job control. 我试图用C语言实现简单的shell,而我却很难实现工作控制。 Everything online seems complicated enough and i think some simplicity is always good. 网上的一切似乎都足够复杂,我认为一些简单性总会很好。 So let me ask this ... After fork() is called can i handle Ctrl-Z signal with just 2 function and just with the pid ? 因此,让我问一下...在调用fork()之后,我是否可以仅使用2个函数并使用pid处理Ctrl-Z信号?

I want to call a function ex put_background(pid_t pid) when i hit Ctrl-Z and make process with pid = pid to run background and finally call another function ex put_foreground(pid_t pid) when i write fg and i want the process with pid = pid to go to foreground again. 我想在按Ctrl-Z时调用ex put_background(pid_t pid)函数,并使用pid = pid进行运行以运行背景,最后在我写fg并希望使用pid时调用另一个函数ex put_foreground(pid_t pid) = pid再次转到前景。

So, is this possible? 那么,这可能吗? Any help is appreciated.. code more however. 任何帮助表示赞赏..更多代码。

I am trying to implement simple shell in C language and i am having a hard time implementing job control. 我试图用C语言实现简单的shell,而我却很难实现工作控制。 Everything online seems complicated enough and i think some simplicity is always good. 网上的一切似乎都足够复杂,我认为一些简单性总会很好。

So let me ask this ... After fork() is called can i handle Ctrl-Z signal with just 2 function and just with the pid ? 因此,让我问一下...在调用fork()之后,我是否可以仅使用2个函数并使用pid处理Ctrl-Z信号?

Note that Ctrl-Z is meaningful primarily to the terminal driver. 请注意,Ctrl-Z主要对终端驱动程序有意义。 It causes a SIGTSTP to be sent to the foreground process group of the terminal in which that character was typed -- that is, the process group that has that terminal as its controlling one, and has permission to read from it. 它会导致将SIGTSTP发送到在其中键入了该字符的终端的前台进程组-即,以该终端作为其控制端并有权从中读取的进程组。 By default, this causes the processes in that group to stop, but that's it. 默认情况下,这将导致该组中的进程停止,仅此而已。 You don't need to do anything to achieve that. 您无需执行任何操作即可实现该目标。 * *

I want to call a function ex put_background(pid_t pid) when i hit Ctrl-Z and make process with pid = pid to run background and finally call another function ex put_foreground(pid_t pid) when i write fg and i want the process with pid = pid to go to foreground again. 我想在按Ctrl-Z时调用ex put_background(pid_t pid)函数,并使用pid = pid进行运行以运行背景,最后在我写fg并希望使用pid时调用另一个函数ex put_foreground(pid_t pid) = pid再次转到前景。

By definition and design, at most one process group has control of a given terminal at any particular time. 根据定义和设计,最多一个过程组可以在任何特定时间控制给定的终端。 Thus, to move a foreground job to the background, all you need to do is move a different one to the foreground. 因此,要将前台作业移至后台,您要做的就是将另一份作业移至前台。 That can be the shell itself or some other job under its control. 这可以是外壳本身,也可以是受其控制的其他作业。 The tcsetpgrp() library function accomplishes this. tcsetpgrp()库函数可以完成此任务。 Unless it's the shell itself, you would also want to send a SIGCONT to that process group in case it was stopped. 除非它是外壳本身,否则您还希望将SIGCONT发送到该进程组,以防其停止。

You additionally need a mechanism to resume a stopped background job, but that's easy: just send that process group a SIGCONT . 您还需要一种机制来恢复已停止的后台作业,但这很简单:只需向该进程组发送SIGCONT

So, is this possible? 那么,这可能吗? Any help is appreciated.. code more however. 任何帮助表示赞赏..更多代码。

Well sure, you could write one function for moving a job to the foreground and resuming it, and one for resuming a background job. 可以肯定的是,您可以编写一个将作业移至前台并恢复它的功能,以及一个用于恢复后台作业的功能。 The only information these functions need about the jobs they operate on is their process group IDs (which is the same as the process IDs of their initial processes). 这些功能所需的有关其操作的作业的唯一信息是它们的进程组ID(与它们的初始进程的进程ID相同)。

But you also need to maintain some bookkeeping of the current active jobs, and you need to take some care about starting new jobs, and you need to monitor current jobs -- especially the foreground job -- so as to be able to orchestrate all of the transitions appropriately. 但是您还需要对当前的活跃工作进行一些记账,并且需要谨慎地开始新工作,并且需要监视当前的工作(尤其是前台工作),以便能够协调所有工作。适当的过渡。

The GLIBC manual has an entire chapter on job control , including a substantial section specifically on implementing a job-control shell. GLIBC手册有一整章有关作业控制 ,包括相当一部分专门介绍如何实现作业控制外壳。 This would probably be useful to you even if you are not writing for a GLIBC-based system. 即使您不编写基于GLIBC的系统,这对您可能也会很有用。 The actual code needed is not all that complicated, but getting it right requires a good understanding of a fairly wide range of concepts. 所需的实际代码并不十分复杂,但正确地编写代码需要对相当广泛的概念有很好的理解。


* But you do need to ensure that your shell puts commands it launches into process groups different from its own, else a Ctrl-Z will stop it, too. *但是,您确实需要确保外壳程序将其启动的命令放入与其自身不同的进程组中,否则Ctrl-Z也将其停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM