亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 3396 | 回復(fù): 14
打印 上一主題 下一主題

[系統(tǒng)管理] 關(guān)于這個shell為什么沒有停止 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-10-27 17:36 |只看該作者 |倒序瀏覽
本帖最后由 homerzhou 于 2015-10-27 17:38 編輯

我有一個test.sh,內(nèi)容如下:
  1. #!/bin/sh
  2. while true
  3. do
  4.     ping baidu.com
  5. done
復(fù)制代碼
我運行./test.sh 然后使用 ctrl + c ,可是test.sh沒有終止,ping終止了,由于循環(huán),ping之后又運行了,在網(wǎng)上查到ping是捕捉SIGINT信號的

我理解 ctrl + c 是向前臺進程組發(fā)送 SIGINT信號的,即使ping捕捉了SIGINT信號,test.sh 也應(yīng)該能終止

如果把ping 改成 sleep ,就沒有這個問題了,求高手請教

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
2 [報告]
發(fā)表于 2015-10-28 00:22 |只看該作者
按住 Ctr+C 不要松手

論壇徽章:
0
3 [報告]
發(fā)表于 2015-10-28 17:25 |只看該作者
其實我關(guān)心的不是用什么辦法停下來,是為什么會出現(xiàn)這個情況 回復(fù) 2# yjh777


   

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
4 [報告]
發(fā)表于 2015-10-28 18:05 |只看該作者
我的理解是這樣:因為你只安一次 Ctr+C 只發(fā)一個信號給前臺進程,如果這是正好是ping占據(jù)前臺執(zhí)行, 就是ping收到信號停止,
但是循環(huán)語句沒有停,因為循環(huán)耗時很少 所以碰到ping在執(zhí)行的概率很大。
如果連續(xù)按多次,從ping停掉,到下一個ping產(chǎn)生之前,bash在前臺 這時收到信號就會終止 當(dāng)前循環(huán)語句

如果在 ping 后面插入一個其他語句比較容易發(fā)現(xiàn)差異:
while :; do
        ping localhost
        /usr/bin/sleep 1
done

BTW: 發(fā)現(xiàn)在一個單核的VBox虛擬機上,如果沒有插入sleep 1,即使連續(xù)按Ctr+C 也無法使腳本停下來
物理機上測試行為都類似,連續(xù)按總能停下來

論壇徽章:
0
5 [報告]
發(fā)表于 2015-10-29 09:47 |只看該作者
你好,其實我產(chǎn)生這個以為是在看一篇文檔的時候遇到的,由于我沒有權(quán)限發(fā)送網(wǎng)址,所以我這邊就截取那邊的一段
  1. 5. Special Note On SIGINT

  2. If you choose to set up a handler for SIGINT (rather than using the EXIT trap), you should be aware that a process that exits in response to SIGINT should kill itself with SIGINT rather than simply exiting, to avoid causing problems for its caller. Thus:


  3. trap 'rm -f "$tempfile"; trap - INT; kill -INT $' INT
  4. We can see the difference between a properly behaving process and a misbehaving process. On most operating systems, ping is an example of a misbehaving process. It traps SIGINT in order to display a summary at the end, before exiting. But it fails to kill itself with SIGINT, and so the calling shell does not know that it should abort as well. For example,


  5. # Bash.  Linux ping syntax.
  6. for i in {1..254}; do
  7.   ping -c 2 192.168.1.$i
  8. done
  9. Here, if the user presses Ctrl-C during the loop, it will terminate the current ping command, but it will not terminate the loop. This is because Linux's ping command does not kill itself with SIGINT in order to communicate to the caller that the SIGINT was fatal. (Linux is not unique in this respect; I do not know of any operating system whose ping command exhibits the correct behavior.)

  10. A properly behaving process such as sleep does not have this problem:


  11. i=1
  12. while [ $i -le 100 ]; do
  13.   printf "%d " $i
  14.   i=$((i+1))
  15.   sleep 1
  16. done
  17. echo
  18. If we press Ctrl-C during this loop, sleep will receive the SIGINT and die from it (sleep does not catch SIGINT). The shell sees that the sleep died from SIGINT. In the case of an interactive shell, this terminates the loop. In the case of a script, the whole script will exit, unless the script itself traps SIGINT.
復(fù)制代碼
回復(fù) 4# yjh777


   

論壇徽章:
0
6 [報告]
發(fā)表于 2015-10-29 10:11 |只看該作者
我覺得出現(xiàn)ctrl + c 無效,并不和ping是不是正在執(zhí)行有關(guān),而是和ping捕捉信號有關(guān),因為如果把ping修改為sleep 2000,其實sleep也是一直在運行,可是這時只要按一次ctrl + c 就能停止test.sh運行,而且我覺得 ctrl + c 是發(fā)給前臺進程組,不是單單一個前臺進程的回復(fù) 4# yjh777


   

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
7 [報告]
發(fā)表于 2015-10-29 11:55 |只看該作者
homerzhou 發(fā)表于 2015-10-29 10:11
我覺得出現(xiàn)ctrl + c 無效,并不和ping是不是正在執(zhí)行有關(guān),而是和ping捕捉信號有關(guān),因為如果把ping修改為sleep 2000,其實sleep也是一直在運行,可是這時只要按一次ctrl + c 就能停止test.sh運行,而且我覺得 ctrl + c 是發(fā)給前臺進程組,不是單單一個前臺進程的


嗯,也不是進程組,就是前臺進程;但確實跟該進程是否處理 信號有關(guān);

根據(jù)你粘貼的文檔,應(yīng)該是兩種:
    1. 程序接收到 SIGINT 異常退出,這是他的父進程也會接收到相應(yīng)子進程異常退出的信號,然后一起退出;比如 sleep
    2. 進程里面對 SIGINT 處理函數(shù)做了注冊,收到信號后正常 exit,這是不影響父進程 循環(huán)繼續(xù)執(zhí)行

所以有的循環(huán)你一個 Ctr+C 就全到停掉了
而如果循環(huán)體有處理 SIGINT, 你就需要分別給 子進程和父進程 發(fā)信號,讓他們都停下來

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
8 [報告]
發(fā)表于 2015-10-29 12:09 |只看該作者
不是很確定 如果說的不對,請糾正我,非常感謝!
    回去溫習(xí)一下APUE~

論壇徽章:
0
9 [報告]
發(fā)表于 2015-10-29 16:33 |只看該作者
本帖最后由 homerzhou 于 2015-10-29 16:34 編輯

很開心能不厭其煩的回答我的問題
我重新找了一遍APUE關(guān)于 控制終端的那一節(jié),里面有寫到 ctrl + c 是發(fā)送給前臺進程組中所有進程的,
關(guān)于你說的第一點,父進程接收到子進程異常退出 而退出,我覺得是不成立的,我覺得子進程異常退出如果沒有人為的發(fā)送信號,那么默認(rèn)的應(yīng)該就是發(fā)送信號SIGCHLD,這個信號不會是導(dǎo)致父進程退出。而且在 那篇文檔中講到 正確的做法應(yīng)該是
  1. trap 'rm -f "$tempfile"; trap - INT; kill -INT
復(fù)制代碼
,我理解這個shell的意思是當(dāng)捕捉到 SIGINT信號后,處理一下,接著把SIGINT的信號處理方式設(shè)置成默認(rèn),再給自己發(fā)送SIGINT信號,并沒有給父進程發(fā)送SIGINT信號
我把 這篇文檔網(wǎng)址(有一些空格)發(fā)給你 http : // mywiki. wooledge. org / SignalTrap 回復(fù) 8# yjh777



   

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設(shè)計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
10 [報告]
發(fā)表于 2015-10-29 17:58 |只看該作者
嗯,我也找APUE看了一下,確實是發(fā)給進程組;而且看了一下不管是 ping 還是 sleep,兩個腳本的進程組ID都是父進程shell的進程ID;
謝謝分享鏈接。

不過到底是什么原因?qū)е掠胮ing的時候,Ctr+C 無法停掉shell進程呢???
    這個從那個文檔里還是沒有看太明白,,有事先撤了,回頭再研究研究;

@homerzhou
  如果有新的發(fā)現(xiàn),請分享你的成果,謝謝啦
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP