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

  免費注冊 查看新帖 |

Chinaunix

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

關(guān)于后臺運行&的一些疑問 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-10-24 16:41 |只看該作者 |倒序瀏覽
平時需要在后臺執(zhí)行任務(wù)的時候,常使用nohup或者screen之類的東東,確實好用。

不過今天有一個情況有點不解,一直都這么認(rèn)為
1、nohup command 登錄后執(zhí)行command,logout后,此command不受影響的繼續(xù)運行
2、command & 把任務(wù)放在后臺 ,用戶logout后,此command也被kill掉

第一點是深信不疑的,對于第二點,發(fā)現(xiàn)有這么一種情況。

  1. $ping [url]www.163.com[/url] >>163.txt &
  2. [1] 25236
  3. $ ps -ef |grep ping
  4. sszheng  25236 25224  0 16:31 pts/0    00:00:00 ping [url]www.163.com[/url]
  5. sszheng  25238 25224  0 16:31 pts/0    00:00:00 grep ping
復(fù)制代碼

很顯然他是bash的子進(jìn)程

  1. $ pstree   
  2. +-sshd---sshd---sshd---bash---ping
  3. ¦                           +-pstre
復(fù)制代碼

$exit
關(guān)閉secureCRT
然后再打開secureCRT,然后登陸

  1. $ ps -ef |grep ping
  2. sszheng  25236     1  0 16:31 ?        00:00:00 ping [url]www.163.com[/url]
  3. sszheng  25250 25243  0 16:34 pts/1    00:00:00 grep ping
  4. 很明顯,上一次退出sshd,ping就給了init了,PPID為1。
復(fù)制代碼

  1. $pstree
  2. +-ping
  3. +-sshd---sshd---sshd---bash---pstree
復(fù)制代碼


如果是nohup的話,可以很好理解,當(dāng)用戶注銷(logout)或者網(wǎng)絡(luò)斷開時,終端會收到 HUP(hangup)信號從而關(guān)閉其所有子進(jìn)程,而nohup會忽略這個hangup 信號信號。

對于&,應(yīng)該怎么理解?
command & 把任務(wù)放在后臺 ,用戶logout后,此command也被kill掉,怎么體現(xiàn)了?
是不是我理解錯什么了?望指教。

論壇徽章:
5
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亞洲杯之朝鮮
日期:2015-03-13 22:47:33IT運維版塊每日發(fā)帖之星
日期:2016-01-09 06:20:00IT運維版塊每周發(fā)帖之星
日期:2016-03-07 16:27:44
2 [報告]
發(fā)表于 2008-10-24 17:04 |只看該作者

回復(fù) #1 無聲無息 的帖子

好問題。
還沒注意過。
研究研究。

論壇徽章:
0
3 [報告]
發(fā)表于 2008-10-24 17:06 |只看該作者

回復(fù) #2 blackold 的帖子

黑哥,你的環(huán)境測試結(jié)果是不是一樣的?

論壇徽章:
0
4 [報告]
發(fā)表于 2008-10-24 17:14 |只看該作者
原帖由 無聲無息 于 2008-10-24 16:41 發(fā)表
平時需要在后臺執(zhí)行任務(wù)的時候,常使用nohup或者screen之類的東東,確實好用。

不過今天有一個情況有點不解,一直都這么認(rèn)為
1、nohup command 登錄后執(zhí)行command,logout后,此command不受影響的繼續(xù)運行
2 ...

調(diào)用兩次fork()就會出現(xiàn)你說的情況
APUE上的一段
  1. If we want to write a process so that it forks a child but we don't want to wait for the child to complete and we don't want the child to become a zombie until we terminate, the trick is to call fork twice. The program in Figure 8.8 does this.

  2. We call sleep in the second child to ensure that the first child terminates before printing the parent process ID. After a fork, either the parent or the child can continue executing; we never know which will resume execution first. If we didn't put the second child to sleep, and if it resumed execution after the fork before its parent, the parent process ID that it printed would be that of its parent, not process ID 1.

  3. Executing the program in Figure 8.8 gives us

  4.    $ ./a.out
  5.    $ second child, parent pid = 1


  6. Note that the shell prints its prompt when the original process terminates, which is before the second child prints its parent process ID.
  7. Figure 8.8. Avoid zombie processes by calling fork twice

  8. #include "apue.h"
  9. #include <sys/wait.h>

  10. int
  11. main(void)
  12. {
  13.     pid_t   pid;

  14.     if ((pid = fork()) < 0) {
  15.         err_sys("fork error");
  16.     } else if (pid == 0) {     /* first child */
  17.         if ((pid = fork()) < 0)
  18.             err_sys("fork error");
  19.         else if (pid > 0)
  20.             exit(0);    /* parent from second fork == first child */
  21.         /*
  22.          * We're the second child; our parent becomes init as soon
  23.          * as our real parent calls exit() in the statement above.
  24.          * Here's where we'd continue executing, knowing that when
  25.          * we're done, init will reap our status.
  26.          */
  27.         sleep(2);
  28.         printf("second child, parent pid = %d\n", getppid());
  29.         exit(0);
  30.     }
  31.    
  32.     if (waitpid(pid, NULL, 0) != pid)  /* wait for first child */
  33.         err_sys("waitpid error");

  34.     /*
  35.      * We're the parent (the original process); we continue executing,
  36.      * knowing that we're not the parent of the second child.
  37.      */
  38.     exit(0);
  39. }
復(fù)制代碼

論壇徽章:
5
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亞洲杯之朝鮮
日期:2015-03-13 22:47:33IT運維版塊每日發(fā)帖之星
日期:2016-01-09 06:20:00IT運維版塊每周發(fā)帖之星
日期:2016-03-07 16:27:44
5 [報告]
發(fā)表于 2008-10-24 17:23 |只看該作者
原帖由 無聲無息 于 2008-10-24 17:06 發(fā)表
黑哥,你的環(huán)境測試結(jié)果是不是一樣的?

我這里沒有這種情況。
重新登錄后,原來處于后臺運行的ping沒了。
啟動后:
% ps -ef |grep ping
robert    3425  3416  0 17:31 pts/0    00:00:00 ping www.163.com
robert    3437  3416  0 17:32 pts/0    00:00:00 grep ping

退出,再重新登錄后:
% ps -ef|grep ping
robert    3468  3455  0 17:36 pts/0    00:00:00 grep ping

[ 本帖最后由 blackold 于 2008-10-24 17:25 編輯 ]

論壇徽章:
0
6 [報告]
發(fā)表于 2008-10-24 17:25 |只看該作者
這時候你應(yīng)該查看一下你的ping 進(jìn)程的狀態(tài)是啥..Zombie?

論壇徽章:
0
7 [報告]
發(fā)表于 2008-10-24 18:00 |只看該作者
會不會你的 huponexit 選項是關(guān)閉的,shell 退出時就不會發(fā)送SIGHUP到后臺jobs, 用shopt 看看,
順便要指出的是,要使huponexit生效,條件是
1. 打開這個選項,shopt -s huponexit   
2. login shell (如ssh登錄的shell) 退出,才會發(fā)送 SIGHUP 至后臺all jobs

[ 本帖最后由 seeLnd 于 2008-10-24 18:26 編輯 ]

論壇徽章:
0
8 [報告]
發(fā)表于 2008-10-24 18:43 |只看該作者

回復(fù) #6 nhw_cs 的帖子

額,都說了“Avoid zombie processes by calling fork twice”
對待zombie,ps命令會給出“Z”來標(biāo)識...

論壇徽章:
0
9 [報告]
發(fā)表于 2008-10-24 18:48 |只看該作者

回復(fù) #7 seeLnd 的帖子

經(jīng)過試驗,樓上分析的都對了

問題在于huponexit關(guān)閉了

shopt -s huponexit
shell 退出時才會會發(fā)送SIGHUP到后臺jobs

$pstree
+-ping
+-sshd---sshd---sshd---bash---pstree

否則的話,向nhw_cs所說的那樣子,ping就成為Zombie了,所以前面顯示的一樣,
bash進(jìn)程終止后,init 進(jìn)程會接管父進(jìn)程留下的這些“孤兒進(jìn)程”,所以PPID是1了

這個問題讓我查資料,學(xué)習(xí)了,謝謝大家。

論壇徽章:
5
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亞洲杯之朝鮮
日期:2015-03-13 22:47:33IT運維版塊每日發(fā)帖之星
日期:2016-01-09 06:20:00IT運維版塊每周發(fā)帖之星
日期:2016-03-07 16:27:44
10 [報告]
發(fā)表于 2008-10-24 19:15 |只看該作者

回復(fù) #9 無聲無息 的帖子

查到什么資料了?記得發(fā)給你瞧瞧啊。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(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