免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3226 | 回复: 2

协同进程示例 [复制链接]

论坛徽章:
0
发表于 2009-05-07 23:31 |显示全部楼层
#!/bin/ksh
#############################################################
#
# COPYLEFT OF WRL, 2009
#
#############################################################
# SCRIPT: coproc22.sh
# AUTHOR: Randy/wrl
# DATE:   20090507
# REV:    1.0
#
# PLATFORM: RHEL5
#
# PURPOSE: demo program of co-process
#
# REV LIST:
#        DATE:
#        BY:
#        MODIFICATION:
#
# set -n   # Uncomment to check your syntax, without execution.
#          # NOTE: Do not forget to put the comment back in or
#          #       the shell script will not execute!
# set -x   # Uncomment to debug this shell script (Korn shell only)
#         
##########################################################
########### DEFINE FILES AND VARIABLES HERE ##############
##########################################################
total_seconds=10
break_out="N"


##########################################################
############### DEFINE FUNCTIONS HERE ####################
##########################################################
function trap_exit
{
        break_out="S"
        print -p $break_out                #send instruction S to proc_watch
        exit 1
}

function proc_watch
{
        echo "" >/dev/pts/2
        while true
        do
                read break_out
                if [ "x$break_out" = "xT" ]; then
                        echo "function exits after time out." >/dev/pts/2
                        return 0
                elif [ "x$break_out" = "xS" ]; then
                        echo "function exits after received terminal signal!" >/dev/pts/2
                        return 1
                else
                        echo "function proc_watch is running at `date` ..." >/dev/pts/2
                fi
        done
}

##########################################################
################ BEGINNING OF MAIN #######################
##########################################################
trap 'trap_exit' 1 2 3 15                #capture terminal signal

proc_watch |&                        #send the function to background       

while [ $total_seconds -gt 0 ]
do
        print -p $break_out                #send instruction "N" to proc_watch
        total_seconds=`expr $total_seconds - 1`
        sleep 1
done

break_out="T"
print -p $break_out                        #send instruction "T" to proc_watch

exit 0
# End of script

评分

参与人数 1可用积分 +5 收起 理由
寂寞烈火 + 5 精品文章

查看全部评分

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2009-05-08 00:52 |显示全部楼层
不错,,,顶一个!

论坛徽章:
0
发表于 2009-05-08 00:54 |显示全部楼层
谢谢分享。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP