免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1751 | 回复: 4
打印 上一主题 下一主题

[文本处理] 请教个管道和后台问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-19 13:07 |只看该作者 |倒序浏览
近日做项目遇到使用无限循环的问题,查到一些资源可以是用协作进程来解决。建议在主脚本里面将需要无限循环的部分写成函数的形式,然后执行“函数名 | &"这样的方式将无限循环做成协作进程,这样当主脚本推出的时候这个函数就不会无限在后台运行。我的疑问:

直接执行”函数名 &"这样的方式与"函数名 | &"有啥区别?为啥要用管道?请大家指点,谢谢!

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
2 [报告]
发表于 2012-11-21 10:47 |只看该作者
你是什么系统, 我在Solaris下不认 |& 方式

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
3 [报告]
发表于 2012-11-21 12:29 |只看该作者
可以与协进程进行双向通信,省得自己建命名管道

论坛徽章:
0
4 [报告]
发表于 2012-12-03 15:20 |只看该作者
娘度了下,搜到了类似的问题:http://bbs.csdn.net/topics/370234709  但是木有人回答。
回2楼:要在bash下试,solaris是c shell吧?
回3楼:双向通信?怎么实现?能否给个例子参考下?

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
5 [报告]
发表于 2012-12-03 16:45 |只看该作者

  1. Co-Processes

  2. A co-process, which is a pipeline created with the |& operator, is an asynchronous process that the shell can both write to (using print -p) and read from (using read -p). The input and output of the co-process can also be manipulated using >&p and <&p redirections, respectively. Once a co-process has been started, another can't be started until the co-process exits, or until the co-process input has been redirected using an exec n>&p redirection. If a co-process's input is redirected in this way, the next co-process to be started will share the output with the first co-process, unless the output of the initial co-process has been redirected using an exec n<&p redirection.
  3. Some notes concerning co-processes:

  4. the only way to close the co-process input (so the co-process reads an end-of-file) is to redirect the input to a numbered file descriptor and then close that file descriptor (e.g., exec 3>&p;exec 3>&-).
  5. in order for co-processes to share a common output, the shell must keep the write portion of the output pipe open. This means that end of file will not be detected until all co-processes sharing the co-process output have exited (when they all exit, the shell closes its copy of the pipe). This can be avoided by redirecting the output to a numbered file descriptor (as this also causes the shell to close its copy). Note that this behaviour is slightly different from the original Korn shell which closes its copy of the write portion of the co-processs output when the most recently started co-process (instead of when all sharing co-processes) exits.
  6. print -p will ignore SIGPIPE signals during writes if the signal is not being trapped or ignored; the same is not true if the co-process input has been duplicated to another file descriptor and print -un is used.
复制代码
man ksh
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP