码迷,mamicode.com
首页 > 系统相关 > 详细

shell的多进程并发和控制

时间:2020-03-18 13:23:44      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:click   time   read   test   success   for   技术   echo   shell   

  1. 单个进程的example
    技术图片
     1 #!/bin/bash
     2 start_time=`date "+%s"`
     3 echo "start_time is $start_time"
     4 for((i=1;i<=4;i++))
     5 do
     6    echo success $i;sleep 2
     7 done 
     8 wait
     9 end_time=`date "+%s"`
    10 echo "end_time is $end_time"
    11 echo "TIME: `expr $end_time - $start_time`"
    View Code

    上面的wait是多余的;执行的结果如下:

    技术图片
    1 start_time is 1584504244
    2 success 1
    3 success 2
    4 success 3
    5 success 4
    6 end_time is 1584504252
    7 TIME: 8
    View Code

    由于是顺序执行,每次只执行一个,所以使用8s完成

  2. 未控制的多进程执行example
    技术图片
     1 #!/bin/bash
     2 start_time=`date "+%s"`
     3 echo "start_time is $start_time"
     4 for((i=1;i<=4;i++))
     5 do
     6     {    
     7    echo success $i;sleep 2
     8    }&
     9 done 
    10 wait
    11 end_time=`date "+%s"`
    12 echo "end_time is $end_time"
    13 echo "TIME: `expr $end_time - $start_time`"
    View Code

    执行的结果如下:

    技术图片
    1 start_time is 1584504404
    2 success 1
    3 success 2
    4 success 3
    5 success 4
    6 end_time is 1584504406
    7 TIME: 2
    View Code

    由于是并行执行,所以使用2s完成;加入各个进程执行时间

  3. 控制执行的多进程example
    技术图片
     1 #!/bin/bash
     2 
     3 trap "exec 1000>&-;exec 1000<&-;exit 0" 2
     4 
     5 mkfifo testfifo
     6 exec 1000<>testfifo
     7 rm -rf testfifo
     8 
     9 for((n=1;n<=10;n++))
    10 do
    11     echo >&1000
    12 done
    13 
    14 start=`date "+%s"`
    15 
    16 for((i=1;i<100;i++))
    17 do
    18     read -u1000
    19     {
    20         echo success $i; sleep 2
    21         echo >&1000    
    22     }&
    23 done
    24 
    25 wait
    26 end=`date "+%s"`
    27 
    28 echo "TIME: `expr $end - $start`"
    29 
    30 exec 1000>&-
    31 exec 1000<&-
    View Code

    执行结果如下:

    技术图片
      1 success 1
      2 success 2
      3 success 3
      4 success 4
      5 success 5
      6 success 6
      7 success 7
      8 success 8
      9 success 9
     10 success 10
     11 success 11
     12 success 12
     13 success 13
     14 success 14
     15 success 15
     16 success 16
     17 success 17
     18 success 18
     19 success 19
     20 success 20
     21 success 21
     22 success 22
     23 success 23
     24 success 24
     25 success 25
     26 success 26
     27 success 27
     28 success 28
     29 success 29
     30 success 30
     31 success 31
     32 success 32
     33 success 33
     34 success 34
     35 success 35
     36 success 36
     37 success 37
     38 success 38
     39 success 39
     40 success 40
     41 success 41
     42 success 42
     43 success 43
     44 success 44
     45 success 45
     46 success 46
     47 success 47
     48 success 48
     49 success 49
     50 success 50
     51 success 51
     52 success 52
     53 success 53
     54 success 54
     55 success 55
     56 success 56
     57 success 57
     58 success 58
     59 success 59
     60 success 60
     61 success 61
     62 success 62
     63 success 63
     64 success 64
     65 success 65
     66 success 66
     67 success 67
     68 success 68
     69 success 69
     70 success 70
     71 success 71
     72 success 72
     73 success 73
     74 success 74
     75 success 75
     76 success 76
     77 success 77
     78 success 78
     79 success 79
     80 success 80
     81 success 81
     82 success 82
     83 success 83
     84 success 84
     85 success 85
     86 success 86
     87 success 87
     88 success 88
     89 success 89
     90 success 90
     91 success 91
     92 success 92
     93 success 93
     94 success 94
     95 success 95
     96 success 96
     97 success 97
     98 success 98
     99 success 99
    100 TIME: 20
    View Code

    上述的example每次执行10个线程,每次需要2秒执行完成,总共执行100个线程,总计20秒。

shell的多进程并发和控制

标签:click   time   read   test   success   for   技术   echo   shell   

原文地址:https://www.cnblogs.com/zhiminyu/p/12516600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!