我如何在Linux中对Shell进行多项工作 [英] How can I Shell have more than one job in Linux

查看:80
本文介绍了我如何在Linux中对Shell进行多项工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linux的初学者,只是对工作和流程组有一些疑问.

I'm a beginner in Linux, just have some questions about job and process group.

我的教科书说'Unix外壳使用作业的抽象来表示由于评估单个命令行而创建的进程.在任何时间点,最多只能有一个前台作业,而最多有零个或多个后台作业.

My textbook says 'Unix shells use the abstraction of a job to represent the processes that are created as a result of evaluating a single command line. At any point in time, there is at most one foreground job and zero or more background jobs.

可以说我们有这个简单的shell代码(我省去了一些不重要的代码,例如,设置argv等):

Lets say we have this simple shell code(I leave out some unimportant code, i.e setup argv etc):

当我们输入第一个命令时,例如:./exampleProgram &

When we type the first commnad, for example:./exampleProgram &

Q1-是否创建工作?如果是,该作业包含什么过程?

Q1- Is a job created? if yes, what process does the job contain?

shellex.c的main()被调用,因此在执行第15行时:fork()创建一个新的子进程,假设父进程为p1,而新创建的子进程为c1 如果它是后台作业,那么我们可以在提示符下键入另一个命令./anotherProgram &并输入enter

the main() of shellex.c is invoked, so when execute the line 15: fork() create a new child process, lets say the parent process is p1, and the newly created child process is c1 and if it is background job, then we can type another command in the prompt, ./anotherProgram & and type enter

Q2-我很确定现在它只是p1和c1,并且p1正在执行第二条命令,当它再次执行第15行时:fork(), 我们有p1,c1和新创建的c2,我的理解正确吗?

Q2- I'm pretty sure it just p1 and c1 at the moment and p1 is executing the second command, when it execute the line 15: fork() again, we have p1, c1 and new created c2, is my understanding correct?

Q3-那里有几份工作?仅仅是包含p1,c1和c2的一项工作吗?

Q3- How many job is there? is it just one job that contains p1, c1, and c2?

Q4-如果它只是一个作业,那么当我们继续键入新命令时,我们将只有一个作业包含一个父进程p1和许多子进程c1,c2,c3,c4 ... 那么为什么我的教科书上说壳牌可以做一份以上的工作呢?以及为什么最多有一个前台作业和零个或多个后台作业.

Q4- if it is only one job, so when we keeps typing new commands we will only have one job contains one parent process p1 and many child processes c1, c2, c3, c4... so why my textbook says that Shell can have more than one job? and why there is at most one foreground job and zero or more background jobs.

推荐答案

关于这个主题,有很多要说的内容,其中一些可以包含在答案中,而大多数都需要进一步阅读.

There is quite a bit to say on this topic, some of which can fit in an answer, and most of which will require further reading.

对于第一季度,我从概念上说可以,但是作业不是自动的,作业跟踪和控制也不是不可思议的.我在您显示的代码片段中看不到任何逻辑,例如建立并维护一个工作表.我了解这只是一个示例,因此工作控制逻辑可能在其他地方.作业控制是现有的普通Unix Shell的功能,但是如果人们从头开始编写新的Unix Shell,则需要添加作业控制功能,如代码/逻辑.

For Q1, I would say conceptually yes, but jobs are not automatic, and job tracking and control are not magical. I don't see any logic in the code snippits you've show that e.g. establishes and maintains a jobs table. I understand it's just a sample, so maybe the job control logic is elsewhere. Job control is a feature of common, existing Unix shells, but if a person writes a new Unix shell from scratch, job control features would need to be added, as code / logic.

对于第二季度,您所说的方式不是我所说的.在第一次调用fork()之后,是的,有一个p1和一个c1,但是首先要认识到p1和c1是 same 程序(shellex)的不同实例.仅在对execve()的调用正在运行exampleProgram之后. fork()创建一个shellex的子实例,并且execve()导致shellex的子实例被exampleProgram替换(在RAM中)(假定为argv[0]的值).

For Q2, the way you've put it is not how I would put it. After the first call to fork(), yes there is a p1 and a c1, but recognize that at first, p1 and c1 are different instances of the same program (shellex); only after the call to execve() is exampleProgram running. fork() creates a child instance of shellex, and execve() causes the child instance of shellex to be replaced (in RAM) by exampleProgram (assuming that's the value of argv[0]).

在真正意义上,父母处决"孩子,或者只是为了让孩子继续前进,而在execve()上替换孩子的过程没有真正意义.父级启动子级,并可能等待完成子级执行,但实际上,父级及其整个子进程层次结构都是各自独立执行的,正在执行通过内核.

There is no real sense in which the parent is "executing" the child, nor the process that replaces the child upon execve(), except just to get them going. The parent starts the child and might wait for the child execution to complete, but really a parent and its whole hierarchy of child processes are all executing each on its own, being executed by the kernel.

但是,是的,如果告诉运行的程序应该在后台运行,则shellex将接受进一步的输入,并且在下一次调用fork()时,将有一个带有两个子对象的父shellex流程.再次,起初,子c2将是shellex的实例,并通过execve()迅速替换为已命名的任何程序.

But yes, if told that the program to run should be run in the background, then shellex will accept further input, and upon the next call to fork(), there will be the parent shellex with two child processes. And again, at first the child c2 will be an instance of shellex, quickly replaced via execve() by whatever program has been named.

(关于在后台运行,&是否具有这种效果取决于示例代码中名为parseline()的函数内部的逻辑.我熟悉的Shell使用&表示运行此在后台",但是这并没有什么特别的或不可思议的.新编写的Unix Shell可以以其他方式做到这一点,在其后跟+或前导BG:,或者该外壳的作者决定要做的任何事情

(Regarding running in the background, whether or not & has that effect depends upon the logic inside the function named parseline() in the sample code. Shells I'm familiar with use & to say "run this in the background", but there is nothing special nor magical about that. A newly-written Unix shell can do it some other way, with a trailing +, or a leading BG:, or whatever the shell author decides to do.

对于 Q3 Q4 ,首先要认识到的是您正在调用p1 的父级是您所使用的shell程序已经显示.因此,不,p1不会成为工作的一部分.

For Q3 and Q4, the first thing to recognize is that the parent you are calling p1 is the shell program that you've shown. So, no, p1 would not be part of the job.

在Unix中,作业是作为单个管道的一部分执行的进程的集合.因此,一项工作可以包含一个或多个过程.此类进程仍附加到运行它们的终端,但可能在前台(正在运行和交互式),已挂起或在后台(正在运行而不是交互式).

In Unix, a job is a collection of processes that execute as part of a single pipeline. Thus a job can consist of one process or many. Such processes remain attached to the terminal from which they are run, but might be in the foreground (running and interactive), suspended, or in the background (running, not interactive).

one process, foreground    : ls -lR
one process, background    : ls -lR &
one process, background    : ls -lR, then CTRL-Z, then bg
many processes, foreground : ls -lR | grep perl | sed 's/^.*\.//'
many processes, background : ls -lR | grep perl | sed 's/^.*\.//' &

要凭经验查看作业与流程,请在后台运行管道(上述5个示例中的第5个),并且在运行时使用ps向您显示进程ID和进程组ID.例如,在我Mac的bash版本上,是:

To see jobs vs. processes empirically, run a pipeline in the background (the 5th of the 5 examples above), and while it is running use ps to show you the process IDs and the process group IDs. e.g., on my Mac's version of bash, that's:

$ ls -lR | grep perl | sed 's/^.*\.//' &
[1] 2454                     <-- job 1, PID of the sed is 2454

$ ps -o command,pid,pgid
COMMAND         PID  PGID
vim            2450  2450    <-- running in a different tab
ls -lR         2452  2452    }
grep perl      2453  2452    }-- 3 PIDs, 1 PGID
sed s/^.*\.//  2454  2452    }

与外壳和终端的此附件相反,守护程序从两者分离.启动守护程序时,父级使用fork()来启动子进程,但随后退出,仅使子级运行,并且现在具有父级PID1.子级关闭stdinstdoutstderr,因为它们毫无意义,因为守护程序运行无头".

In contrast to this attachment to the shell and the terminal, a daemon detaches from both. When starting a daemon, the parent uses fork() to start a child process, but then exits, leaving only the child running, and now with a parent of PID 1. The child closes down stdin, stdout, and stderr, since those are meaningless, since a daemon runs "headless".

但是在外壳程序中,父级(又是外壳程序)保持运行wait() ing(前景子程序)或不运行wait() ing(后台子程序),并且该子项通常运行保留使用stdinstdoutstderr(尽管它们可能会重定向到文件等)

But in a shell, the parent -- which, again, is the shell -- stays running either wait()ing (foreground child program), or not wait()ing (background child program), and the child typically retains use of stdin, stdout, and stderr (although, these might be redirected to files, etc.)

而且,外壳程序可以调用子外壳程序,当然,运行的任何程序都可以fork()自己的子进程,依此类推.因此,流程的层次结构可能会变得很深.如果没有其他特定操作,子进程将与其父进程位于同一进程组中.

And, a shell can invoke sub-shells, and of course any program that is run can fork() its own child processes, and so on. So the hierarchy of processes can become quite deep. Without specific action otherwise, a child process will be in the same process group as it's parent.

这里有一些文章可供进一步阅读:

Here are some articles for further reading:

工作与Unix中的进程?

https://unix.stackexchange .com/questions/4214/工作与流程之间的区别是什么

https://unix.stackexchange.com /questions/363126/为什么流程不是预期流程组的一部分

Bash参考手册;作业控制

Bash参考手册;作业控制基础

这篇关于我如何在Linux中对Shell进行多项工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆