Chinaunix首页 | 论坛 | 博客
  • 博客访问: 968494
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

2008-11-28 13:57:18

9.8 job control

在没有图形界面的时候,job control是很有用的,在有了window系统之后,这些用于job control的管理似乎就没有用了,但是posix.1的标准中依然有他们。

job control的支持需要做到三点:

1Kernel要支持job control

2. shell 要支持

3Terminal driver也要支持job control

有了上述三点的支持,才能说你的系统支持job control

 

Terminal driver要向foreground process group发送如下signal

1Interrupt信号,即SIGINT,DELETECtl-C产生

2Quit 信号,即SIGQUIT,由Ctl-Backslash产生

3Suspend信号,即SIGTSTP,由ctl-z产生

产生上述具体信号的字符时可以订制的,而且我们还可以设置terminal driver禁止发送这些信号。

 

Terminal driverbackground process group发送如下signal:

1.当background process读取controlling terminal的时候,terminal driver会发送SIGTTIN信号给background process, 然后他就被stopped了,我们可以用fg命令将该background process或者是一个job置成foreground process,这样terminal driver就又会向它发送SIGCONT信号使其继续执行,此时就能够读取controlling terminal的数据了。

2.当background processscontrolling terminal的时候,有时候可以直接写,有时候不可以,当不可以的时候,该process会收到SIGTTOU信号,然后被stopped。接下来我们可以采用上面的方法,用fg命令将其置为foreground process…。那么靠什么控制是否允许background processcontrolling terminal写数据呢? 使用stty命令。

下面就是书上的例子:

(一)建立background job

$ make all > Make.out &
   [1]     1475
   $ pr *.c | lpr &
   [2]     1490
   $                                just press RETURN
   [2] +  Done                 pr *.c | lpr &
[1] +  Done                 make all > Make.out &

注意,倒数第三行必须打一个回车才会显示出2job的状态不是因为你会扯了他们才会变化,而是因为他们的状态发生变化及工作完成后,系统不能立刻就让 shell输出到controlling terminal,而是等待我们新输入一个command line,这样做可以避免随意的输出会搅乱我们正在书写的内容。

(二)尝试让background job去读取controlling terminal

$ cat > temp.foo &          start in background, but it'll read from standard input
   [1]     1681
   $                           we press RETURN
   [1] + Stopped (SIGTTIN)     cat > temp.foo &
   $ fg %1                     bring job number 1 into the foreground
   cat > temp.foo              the shell tells us which job is now in the foreground
   
   hello, world                enter one line
   
   ^D                          type the end-of-file character
   $ cat temp.foo              check that the one line was put into the file
   hello, world

 

(三)尝试让background job去写controlling terminal

$ cat temp.foo &             execute in background
   [1]     1719
   $ hello, world               the output from the background job appears after the prompt
                                we press RETURN
   [1] + Done              cat temp.foo &
   $ stty tostop                disable ability of background jobs to output to
 controlling terminal
   $ cat temp.foo &             try it again in the background
   [1]     1721
   $                            we press RETURN and find the job is stopped
   [1] + Stopped(SIGTTOU)               cat temp.foo &
   $ fg %1                      resume stopped job in the foreground
   cat temp.foo                 the shell tells us which job is now in the foreground
hello, world                 and here is its output

 

最后又一个图,描述了foreground/background job, controlling terminal, terminal dirver之间的关系:

Figure 9.8. Summary of job control features with foreground and background jobs, and terminal driver

阅读(1133) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~