国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

linux中Shell并發編程示例

2024-08-27 23:59:34
字體:
來源:轉載
供稿:網友

Shell是系統的用戶界面,提供了用戶與內核進行交互操作的一種接口,它接收用戶輸入的命令并把它送入內核去執行了,下文就來介紹linux中Shell并發編程示例,希望能幫助到各位.

在Python中,有很多模塊都可以實現并發編程,比如 threading,processing,eventlet 與 Stackless Python 等.

那么對于Shell而言,又如何實現呢?其實原理很簡單,我采用的方法是.

1.將需要執行的任務分批放入后臺執行;

2.將后臺執行的命令結果匯總到指定的文件中;

3.使用wait命令來等待所有任務執行結束。

下面的腳本就用到了這樣的并發編程方法,實現的功能是,快速(3-4秒內)對相同C網內的所有IP(255個)通過命令ping進行測試并返回結果,代碼如下:

  1. vim fastping.sh 
  2. #!/bin/bash 
  3. default settings 
  4. subnet=$1 # C type subnet 
  5. retry=2 # retry times 
  6. timeout=3 # timeout seconds 
  7. output=/tmp/ping.output # output file 
  8. function print_help 
  9. function print_help(){ 
  10.   echo "Examples:" 
  11.   echo ${0} 172.17.32 
  12.   echo ${0} 192.168.1 unable 
  13.   exit 1 
  14. # check the parameter 
  15. if [ $# -lt 1 ]; then 
  16.   print_help 
  17. fi 
  18. # check the network parameter's format 
  19. count=0 
  20. for i in $(echo $1 |sed 's/\./ /g'
  21. do 
  22.   count=$((${count}+1)) 
  23. done 
  24. if [ ${count} -ne 3 ]; then 
  25.   print_help 
  26. fi 
  27. # clean the output file 
  28. > ${output} 
  29. function pingable(){ 
  30.   ping -c ${retry} -w ${timeout} -q ${subnet}.${i} &> /dev/null && echo ${i} >> ${output} 
  31. function unpingable(){ 
  32.   ping -c ${retry} -w ${timeout} -q ${subnet}.${i} &> /dev/null || echo ${i} >> ${output} 
  33. # get the check type 
  34. if [ "$2" == "unable" ]; then 
  35.   status="unpingable" 
  36. else 
  37.   status="pingable" 
  38. fi 
  39. # ping as paraller mode and write output into file 
  40. for i in {1..255} 
  41. do  
  42.   ${status} & 
  43. done 
  44. # wait for all ping processes done 
  45. wait 
  46. # print output with better order 
  47. sum=$(wc -l ${output} |awk '{print $1}'
  48. echo "There are \"${sum}\" \"${status}\" IPs begin with \"${subnet}.\" :" 
  49. cat ${output} | sort -t"." -k1,1n -k2,2n -k3,3n -k4,4n | xargs -n 20 echo " " 
  50. chmod +x fastping.sh 
  51. ./fastping.sh 
  52. Examples: 
  53. ./fastping 172.17.32 
  54. ./fastping 192.168.1 unable 
  55. time ./fastping.sh 192.168.1 
  56. There are "142" "pingable" IPs begin with "192.168.1" : 
  57.   1 10 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29 30 
  58.   31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 48 49 50 51 
  59.   52 53 54 55 56 57 59 60 61 62 63 64 65 66 67 68 69 70 71 72 
  60.   73 74 75 76 77 78 80 81 83 84 85 86 87 88 89 90 91 92 93 94 
  61.   95 96 97 98 99 100 101 102 103 104 105 106 107 108 112 113 114 115 116 117  //Vevb.com 
  62.   118 119 120 121 122 123 124 125 126 127 128 133 134 135 136 137 138 139 140 141 
  63.   142 143 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 
  64.   170 254 
  65. real    0m3.201s 
  66. user    0m0.135s 
  67. sys     0m0.495s

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 来宾市| 乃东县| 库尔勒市| 博罗县| 宁南县| 卓尼县| 章丘市| 新源县| 陆丰市| 正蓝旗| 遵化市| 聂拉木县| 白朗县| 府谷县| 壤塘县| 正宁县| 湖北省| 临汾市| 武川县| 龙州县| 长丰县| 陵川县| 中牟县| 靖远县| 始兴县| 晴隆县| 永善县| 湖州市| 合江县| 留坝县| 斗六市| 涡阳县| 沈丘县| 南溪县| 遂宁市| 宁阳县| 香格里拉县| 五大连池市| 博乐市| 牙克石市| 资溪县|