99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋(píng)果6袋
6
麥子學(xué)院

Linux學(xué)習(xí)之 exec和source的區(qū)別詳解

發(fā)布時(shí)間:2017-07-06 10:55  回復(fù):0  查看:2426   最后回復(fù):2017-07-06 10:55  
本文和大家分享的主要是linux中exec和source相關(guān)內(nèi)容,一起來(lái)看看吧,希望對(duì)大家學(xué)習(xí)linux 有所幫助。
  Linux種exec和.(Source)都可以同樣來(lái)執(zhí)行程序或者腳本,要區(qū)別二者區(qū)別,首先了解linux下的2種命令,內(nèi)部命令和外部命令:
  內(nèi)部命令是特殊文件格式.def實(shí)現(xiàn)的。
  外部命令是通過(guò)系統(tǒng)調(diào)用或者獨(dú)立程序?qū)崿F(xiàn)的。
  其次shell執(zhí)行腳本的時(shí)候有兩種方式:
  1、當(dāng)前shell下執(zhí)行
  2、啟動(dòng)子shell在子shell種執(zhí)行
  當(dāng)shell啟動(dòng)子shell時(shí)候,通過(guò)fork創(chuàng)建進(jìn)程子進(jìn)程,首先子進(jìn)程會(huì)繼承父進(jìn)程的很多屬性,而非共享,再?gòu)?fù)制了父進(jìn)程數(shù)據(jù)之后,2者就基本沒(méi)有關(guān)系了,簡(jiǎn)單表示就是 父進(jìn)程屬性→子進(jìn)程。fork函數(shù)和一般的函數(shù)不同,在他成功創(chuàng)建出子進(jìn)程之后會(huì)返回兩個(gè)值,一個(gè)返回給父進(jìn)程中的pid變量(值為子進(jìn)程ID),一個(gè)返回給自進(jìn)程中的pid變量(值為0)當(dāng)然,如果fork失敗了,則只需要返回給父進(jìn)程pid變量一個(gè)-1(子進(jìn)程不存在)。子進(jìn)程確實(shí)復(fù)制了父進(jìn)程的數(shù)據(jù),叫做繼承了父進(jìn)程的部分屬性,這樣一來(lái),子進(jìn)程和父進(jìn)程中的變量就不是同一個(gè)變量了。
  在shell種腳本得的第一行通常是/bin/bash,這種方式就是使用subshell執(zhí)行,見(jiàn)《shell腳本編程》p36。當(dāng)shell打開(kāi)一個(gè)可執(zhí)行文件時(shí),系統(tǒng)調(diào)用fork創(chuàng)建進(jìn)程,用于執(zhí)行程序,內(nèi)核執(zhí)行飛編譯程序
  返回錯(cuò)誤"NOT excutable format file”,shell收到錯(cuò)誤信息啟動(dòng)一個(gè)新shell(shell副本)來(lái)執(zhí)行,
  #?。╯habang)用于告訴內(nèi)核使用哪個(gè)shell來(lái)執(zhí)行。
  現(xiàn)在我們查看系統(tǒng)幫助文檔怎么介紹的:
  source (.):
  Read and execute commands from  filename in the  current  shell
  environment  and return the exit status of the last command exe-
  cuted from filename.
  exec :
  If command is specified, it replaces the shell.  No  new  process
  is  created.
  由此可見(jiàn)source執(zhí)行的時(shí)候是當(dāng)前shell環(huán)境下執(zhí)行,執(zhí)行完成后把狀態(tài)返回給當(dāng)前shell。
  exec執(zhí)行時(shí)候會(huì)關(guān)閉當(dāng)年shell進(jìn)程,并且fork一個(gè)相同pid的shell進(jìn)程來(lái)執(zhí)行,系統(tǒng)調(diào)用新的exec的process來(lái)替代原來(lái)的進(jìn)程執(zhí)行。從表面上看沒(méi)有新的進(jìn)程創(chuàng)建,原來(lái)進(jìn)程的代碼段、數(shù)據(jù)段、堆棧都被新的process所代替。
  exec系統(tǒng)調(diào)用過(guò)程
  fork()執(zhí)行創(chuàng)建一個(gè)new_process,程序執(zhí)行exec系統(tǒng)調(diào)用,fork()執(zhí)行后父子進(jìn)程共享代碼段,數(shù)據(jù)空間分開(kāi),父進(jìn)程copy自己的數(shù)據(jù)空間內(nèi)容和上下文到子進(jìn)程。采用寫(xiě)時(shí)copy的策略:在創(chuàng)建子進(jìn)程時(shí)不會(huì)不copy父進(jìn)程的地址空間,共用,當(dāng)子進(jìn)程寫(xiě)入數(shù)據(jù)時(shí),這時(shí)候copy空間到子進(jìn)程,這種策略提高效率并且執(zhí)行fork()完執(zhí)行exec后,子進(jìn)程的數(shù)據(jù)會(huì)被新的進(jìn)程代替。
  文件描述符FD(file-descriptor)
  文件在打開(kāi)時(shí)候系統(tǒng)給每一個(gè)打開(kāi)的文件分配用于維護(hù)的描述符,這通常包括系統(tǒng)打開(kāi)文件描述符表,進(jìn)程級(jí)的文件描述符表(文件操作符標(biāo)志和文件句柄的引用),文件系統(tǒng)i-node表。(以后會(huì)單獨(dú)寫(xiě)一個(gè)對(duì)內(nèi)核源碼的解釋?zhuān)?/span>
  exec的用法表
Linux學(xué)習(xí)之 exec和source的區(qū)別詳解 
Example:
  1、使用exec cmd
  [yemo@localhost /]$ exec ls #ls 替換掉當(dāng)前shell進(jìn)程
  bin   dev  home  lib64       media  opt   root    selinux  sys  usr
  boot  etc  lib   lost+found  mnt    proc  sbin srv  tmp  var
  
  Connection closed by foreign host.   #shell已經(jīng)被ls替代,ls執(zhí)行完成進(jìn)程結(jié)束退出
  
  Disconnected from remote host(cent6mini_eth0) at 03:59:43.
  
  Type `help' to learn how to use Xshell prompt.
  [c:\~]$
  執(zhí)行完成后關(guān)閉了shell
  2、使用exec控制FD1(stdout)
  [root@localhost tmp] # echo 'im lovin it'> echo.txt      #簡(jiǎn)單的輸出重定向
  [root@localhost tmp] # echo echo.txt
  echo .txt
  [root@localhost tmp] # ls /dev/fd/
  0  1  2  3
  [root@localhost tmp] # exec >echo.t             #把當(dāng)前所有stdout定向到文件
  [root@localhost tmp] # ls
  [root@localhost tmp] # echo "i did it"
  [root@localhost tmp] # cat echo.txt
  cat : echo .txt: input file is output file #打開(kāi)會(huì)死循環(huán)系統(tǒng)保護(hù)          
  [root@localhost tmp] # exec >/dev/tty            #把stdin重新定向會(huì)屏幕(tty設(shè)備)
  [root@localhost tmp] # cat echo.txt             #正常輸出內(nèi)容
  echo
  echo .txt
  haha
  netstat
  pass
  rc
  re.txt
  sed_passwd
  sudoers
  yum.log
  i did it
  4、創(chuàng)建一個(gè)FD4
  [root@localhost yemo] # ls /dev/fd/
  0  1  2  3
  [root@localhost tmp] # exec 4>4.txt                       #生成一個(gè)文件描述符fd4指向文件
  [root@localhost tmp] # ls /dev/fd/      
  0  1  2  3  4
  [root@localhost tmp] # echo "i feel i lose my heart" >&4   #把流通過(guò)fd4到文件中
  [root@localhost tmp] # ls >&4      
  [root@localhost tmp] # exec 4>&-                                 #關(guān)閉fd4
  [root@localhost tmp] # cat 4.txt
  i feel i lose my heart
  4.txt
  echo
  echo .txt
  haha
  netstat
  pass
  rc
  re.txt
  sed_passwd
  sudoers
  yum.log
  exec創(chuàng)建FD4指向文件4.txt,系統(tǒng)創(chuàng)建了FD4管道,通過(guò)管道4的內(nèi)容到會(huì)傳到文件4.txt中,關(guān)閉管道,否則文件占用無(wú)法打開(kāi)。

來(lái)源: Linux公社
您還未登錄,請(qǐng)先登錄

熱門(mén)帖子

最新帖子

?