自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

按下ls -l *.py并回車,Shell都為我們做了什么?

系統(tǒng)
shell 是如何理解和解釋這些命令的?屏幕的背后都做些什么?比如說,當(dāng)我們執(zhí)行 ls -l *.py 的時(shí)候,shell 都做了哪些事情?了解了這些,可以更好的使用 Unix 類操作系統(tǒng),今天我們就來一探究竟。

[[437059]]

你是否想過,當(dāng)你在 shell 上執(zhí)行一個(gè)命令時(shí),unix 的 shell 到底做了哪些事情?shell 是如何理解和解釋這些命令的?屏幕的背后都做些什么?比如說,當(dāng)我們執(zhí)行 ls -l *.py 的時(shí)候,shell 都做了哪些事情?了解了這些,可以更好的使用 Unix 類操作系統(tǒng),今天我們就來一探究竟。

0、什么是 shell

shell 通常是一個(gè)命令行界面,它將操作系統(tǒng)的服務(wù)暴露給人類使用或其他程序。在 shell 啟動(dòng)后,shell 通常會(huì)通過顯示提示來等待用戶的輸入。下圖描述了基本的 UNIX 和 Windows shell 提示。

所以 shell 會(huì)提示用戶輸入命令?,F(xiàn)在是用戶輸入命令的時(shí)候了。那么 shell 是如何獲取用戶輸入的命令并進(jìn)行解釋的呢?為了理解這一點(diǎn),讓我們將它們分為 4 個(gè)步驟,分別是:

  • 獲取并解析用戶輸入
  • 識(shí)別命令及命令的參數(shù)
  • 查找命令
  • 執(zhí)行命令

現(xiàn)在詳細(xì)展開:

1、獲取并解析用戶輸入

比如說,在 shell 上輸入了 ls -l *.py 并回車,shell 內(nèi)部會(huì)調(diào)用一個(gè)叫 getline()「聲明在#include 中,下同」 的函數(shù)來讀取用戶輸入的命令,用戶輸入的命令字符串作為標(biāo)準(zhǔn)輸入流,一旦按下回車,表示一行結(jié)束,getline() 就會(huì)將輸入的字符串存儲(chǔ)到緩沖區(qū)中。

  1. ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream); 

函數(shù)參數(shù)說明:

  • lineptr: 緩沖區(qū)
  • n: 緩沖區(qū)大小
  • stream: 流,這里就是標(biāo)準(zhǔn)輸入流

現(xiàn)在讓我們看一下代碼:

  1. char *input_buffer; 
  2.  
  3. size_t b_size; 
  4.  
  5. b_size = 32; // size of the buffer 
  6.  
  7. input_buffer = malloc(sizeof(char) * b_size); // the buffer to store the user input 
  8.  
  9. getline(&input_buffer, &b_size, stdin); // gets the line and stores it in input_buffer 

一旦用戶按下回車,就會(huì)調(diào)用 getline() ,將用戶輸入的字符串或命令將存儲(chǔ)在 input_buffer 中。所以現(xiàn)在 shell 已經(jīng)獲取了用戶輸入,那么下一步是什么?

2、識(shí)別命令及命令的參數(shù)

現(xiàn)在 shell 已經(jīng)知道你輸入了字符串是 'ls -l *.py' 但是,還需要知道這里面哪個(gè)是命令,哪個(gè)是命令的參數(shù),誰來做這個(gè)事情呢?那就是函數(shù) strtok()「#include 」。

strtok() 將一個(gè)字符串標(biāo)記為分隔符,在這個(gè)例子中分隔符是一個(gè)空格。所以一個(gè)空格告訴 strtok() 它是一個(gè)詞的結(jié)尾。因此 input_buffer 中的第一個(gè)標(biāo)記或單詞是命令 (ls),其余的單詞或標(biāo)記(-l 和 *.py)是命令的參數(shù)。因此,一旦 shell 標(biāo)記了字符串,它就會(huì)將它們存儲(chǔ)在一個(gè)變量中,以便以后使用。

  1. char *strtok(char *restrict str, const char *restrict delim); 

參數(shù)說明:

  • str: 要標(biāo)記的字符串
  • delim: 分隔符

函數(shù) strtok() 接受字符串和分隔符作為參數(shù),返回一個(gè)指向標(biāo)記字符串的指針。具體的執(zhí)行代碼如下所示:

  1. char *input_buffer, *args, *delim_args, *command_argv[50]; 
  2. int i; 
  3.  
  4. i = 0; 
  5. delim_args = " \t\r\n\v\f"; // the delimeters 
  6. args = strtok(input_buffer, delim_args); // stores the token inside args 
  7. while (args) 
  8.  command_argv[i] = args; // stores the token in command_argv 
  9.  args = strtok(NULL, delim_args); 
  10.  i++; 
  11. command_argv[i] = NULL; // sets the last entity of command_argv to NULL 

command_argv[i] = NULL; // sets the last entity of command_argv to NULL

command_argv 保存了命令字符串,其內(nèi)容如下:

  1. command_argv[0] = "ls" 
  2.  
  3. command_argv[1] = "-l" 
  4.  
  5. command_argv[2] = "*.py" 
  6.  
  7. command_argv[3] = NULL 

好了,command_argv[0] 是命令,其他的都是它的參數(shù),最后一個(gè)是 NULL,表示命令的結(jié)束。命令字符串已經(jīng)拆解完畢了,下一步就是查找命令。

3、查找命令

第二步已經(jīng)知道,用戶要執(zhí)行的命令就是 ls,那么去哪里查找這個(gè)命令呢?shell 回去環(huán)境變量 PATH 中去查找,PATH 這個(gè)環(huán)境變量就是存儲(chǔ)可執(zhí)行命令的位置的。

不過,一個(gè) PATH 存儲(chǔ)的路徑可不止一個(gè):

如何在這么多路徑中高效的查找到 ls 命令呢?這就需要 access() 「#include 」 函數(shù):

  1. int access(const char *pathname, int mode); 

參數(shù)及返回值說明:

  • pathname: 文件/可執(zhí)行文件的路徑
  • mode: 模式,我們使用 X_OK 來檢查文件是否存在
  • 返回值:如果文件存在,返回 0,否則返回 -1
  1.  char *path_buff, *path_dup, *paths, *path_env_name, *path[50]; 
  2.  int i; 
  3.  
  4.  i = 0; 
  5.  path_env_name = "PATH"
  6.  path_buff = getenv(path_env_name); /* get the variable of PATH environment */ 
  7.  path_dup = _strdup(path_buff); /* this function is found below */ 
  8.  paths = strtok(path_dup, ":"); /* tokenizes it */ 
  9.  while (paths) 
  10.  { 
  11.   path[i] = paths; 
  12.   paths = strtok(NULL":"); 
  13.   i++; 
  14.  } 
  15.  path[i] = NULL; /* terminates it with NULL */ 
  16.  
  17. /** 
  18. * _strdup - duplicates a string 
  19. * @from: the string to be duplicated 
  20. Return: ponter to the duplicated string 
  21. */ 
  22. char *_strdup(char *from
  23.  int i, len; 
  24.  char *dup_str; 
  25.  
  26.  len = _strlen(from) + 1; 
  27.  dup_str = malloc(sizeof(int) * len); 
  28.  i = 0; 
  29.  
  30.  while (*(from + i) != '\0'
  31.  { 
  32.   *(dup_str + i) = *(from + i); 
  33.   i++; 
  34.  } 
  35.  *(dup_str + i) = '\0'
  36.  
  37.  return (dup_str); 

上面代碼中的 path 數(shù)組存儲(chǔ)所有 PATH 位置并且以 NULL 終止。因此,可以將每個(gè) PATH 位置與命令連接起來,并使用 access() 函數(shù)執(zhí)行存在性檢查:

  1.  char *command_file, *command_path, *path[50]; 
  2.  int i; 
  3.  
  4.  i = 0; 
  5.  command_path = malloc(sizeof(char) * 50); 
  6.  while (path[i] != NULL
  7.  { 
  8.   _strcat(path[i], command_file, command_path); /* this function is found below */ 
  9.   stat_f = access(command_path, X_OK); /* and checks if it exists */ 
  10.   if (stat_f == 0) 
  11.    return (command_path); /* returns the concatenated string if found */ 
  12.  
  13.   i++; 
  14.  } 
  15.  return NULL; /* otherwise returns NULL */ 
  16.  
  17. /** 
  18. * _strcat - concatenates two strings and saves it to a blank string 
  19. * @path: the path string 
  20. * @command: the command 
  21. * @command_path: the string to store the concatenation 
  22. Return: Always void 
  23. */ 
  24. void _strcat(char *path, char *command, char *command_path) 
  25.  int i, j; 
  26.  
  27.  i = 0; 
  28.  j = 0; 
  29.  
  30.  while (*(path + i) != '\0'
  31.  { 
  32.   *(command_path + i) = *(path + i); 
  33.   i++; 
  34.  } 
  35.  *(command_path + i) = '/'
  36.  i++; 
  37.  
  38.  while (*(command + j) != '\0'
  39.  { 
  40.   *(command_path + i) = *(command + j); 
  41.   i++; 
  42.   j++; 
  43.  } 
  44.  *(command_path + i) = '\0'

一旦找到命令,就會(huì)返回命令的完整路徑,否則就返回 NULL,然后 shell 會(huì)顯示命令不存在的錯(cuò)誤。

現(xiàn)在假如命令找到了,然后呢?

4、執(zhí)行命令

命令一旦找到,就是執(zhí)行它的時(shí)候了,問題是怎么執(zhí)行呢?

執(zhí)行命令,需要借助函數(shù) execve()「#include 」中:

  1. int execve(const char *pathname, char *const argv[], 
  2.  
  3. char *const envp[]); 

參數(shù)說明:

  • pathname: 可執(zhí)行文件的完整路徑
  • argv: 命令的參數(shù)
  • envp: 環(huán)境變量列表

execve() 會(huì)執(zhí)行找到的命令,返回一個(gè)整數(shù)表示執(zhí)行結(jié)果。

但是現(xiàn)在如果 shell 只是運(yùn)行 execve(),就會(huì)出現(xiàn)問題。execve() 調(diào)用后不返回標(biāo)準(zhǔn)輸出的信息,這是不好的,因?yàn)橛脩粜枰獔?zhí)行的結(jié)果。所以為了解決這個(gè)問題,shell 在子進(jìn)程中執(zhí)行命令。因此,一旦在子進(jìn)程內(nèi)執(zhí)行完成,父進(jìn)程就會(huì)收到信號(hào)并且程序流繼續(xù)。所以為了執(zhí)行命令,shell 使用 fork() 創(chuàng)建了一個(gè)子進(jìn)程。(fork 聲明在#include 中)

  1. pid_t fork(void); 

fork() 通過復(fù)制調(diào)用進(jìn)程來創(chuàng)建一個(gè)新進(jìn)程。新進(jìn)程稱為子進(jìn)程。調(diào)用進(jìn)程稱為父進(jìn)程。fork() 在父進(jìn)程中返回子進(jìn)程的進(jìn)程 ID,在子進(jìn)程中返回 0:

  1.  char *command, *command_argv[50], **env; 
  2.  pid_t child_pid; 
  3.  int status; 
  4.  
  5.  get_each_command_argv(command_argv, input_buffer); /* this function is found below */ 
  6.  child_pid = fork(); 
  7.  if (child_pid == -1) 
  8.   return (0); 
  9.  
  10.  if (child_pid == 0) 
  11.  { 
  12.   if (execve(command, command_argv, env) == -1) 
  13.    return (0); 
  14.  } 
  15.  else 
  16.   wait(&status); 
  17.  
  18. /** 
  19. * get_each_command_argv - stores all the arguments \ 
  20. *             of the input command to the list 
  21. * @command_argv: the command argument list 
  22. * @input_buffer: the input buffer 
  23. Return: Always void 
  24. */ 
  25. void get_each_command_argv(char **command_argv, char *input_buffer) 
  26.  char *args, *delim_args; 
  27.  int i; 
  28.  
  29.  delim_args = " \t\r\n\v\f"
  30.  args = strtok(input_buffer, delim_args); 
  31.  
  32.  i = 0; 
  33.  while (args) 
  34.  { 
  35.   command_argv[i] = args; 
  36.   args = strtok(NULL, delim_args); 
  37.   i++; 
  38.  } 
  39.  command_argv[i] = NULL

shell 使用 wait()(函數(shù)聲明在#include ) 在程序流繼續(xù)之前等待子進(jìn)程的狀態(tài)變化,并再次為用戶顯示提示。

  1. pid_t wait(int *wstatus); 

wstatus:是一個(gè)指向整數(shù)的指針,可以用來標(biāo)識(shí)子進(jìn)程是如何終止的。

shell 在子進(jìn)程內(nèi)執(zhí)行命令,然后 wait() 等待子進(jìn)程完成。所以這樣用戶就可以得到命令的結(jié)果,并且可以在 shell 顯示其提示后輸入另一個(gè)命令。

所以最后當(dāng)子進(jìn)程完成時(shí)顯示 ls -l *.py 的結(jié)果,并且由于我們已經(jīng)等待子進(jìn)程結(jié)束,這意味著給出了命令的結(jié)果。所以現(xiàn)在 shell 可以再次顯示它的提示以再次等待用戶輸入。這將繼續(xù)循環(huán),除非用戶鍵入 exit。

 

責(zé)任編輯:武曉燕 來源: Python七號(hào)
相關(guān)推薦

2024-09-02 13:59:16

2011-04-19 10:04:25

NeopPIshell網(wǎng)站后門

2020-09-01 11:40:01

HTTPJavaTCP

2020-08-13 08:04:31

配置跨域框架

2010-03-12 14:28:52

三層交換技術(shù)

2020-10-09 08:59:55

輸入網(wǎng)址解密

2015-08-04 09:33:24

Coding.net

2011-03-31 09:20:45

URLDNSWeb應(yīng)用程序

2020-12-15 06:57:24

java服務(wù)器

2016-01-29 10:05:13

酒店服務(wù)業(yè)大數(shù)據(jù)大數(shù)據(jù)分析

2022-12-07 07:33:54

Java啟動(dòng)類項(xiàng)目

2014-07-17 15:52:00

Android L

2021-05-07 11:25:29

項(xiàng)目網(wǎng)關(guān)流量

2021-03-15 08:40:42

Vue組件函數(shù)

2025-02-21 10:01:35

2020-09-13 09:22:51

Linuxls命令

2011-11-29 09:10:11

Hadoop

2021-02-25 10:02:32

開機(jī)鍵Linux內(nèi)存

2020-04-01 17:39:57

天翼云新基建

2022-03-11 21:28:31

部署開發(fā)服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)