Programing python 4th page 228

 """
IPC
http://www.cnblogs.com/BoyXiao/archive/2011/01/01/1923828.html
"""
import os,time
import threading def child(pipeout):
zzz = 0
while True:
time.sleep(zzz) # make parent wait
msg=('spam %03d \n' % zzz).encode() # pipes are binary bytes
os.write(pipeout,msg) # send to parent
zzz = (zzz+1)%5 def parent(pipein):
while True:
line = os.read(pipein,32) # blocks until data sent
print('parent %d got [%s] at [%s]'%(os.getpid(),line,time.ctime())) pipein,pipeout = os.pipe()
threading.Thread(target = child,args=(pipeout,)).start()
parent(pipein)

2.Bidirectional IPC

 """
location: programing python 4td page 229
spawn a child process/program,connect my stdin/stdput to child process's stdout/stdin
my reads and writes map to output and input streams of the spawn program;much like typing
together streams with subprocess module
"""
import os,sys
def spawn(prog,*args):
stdinFd = sys.stdin.fileno() #pass progname,cmdline args
stdoutFd =sys.stdin.fileno() # get desciptors for streams,normally stdin =0,stdout =1 parentStdin,childStdout = os.pipe() # make two IPC pipe changels
childStdin,parentStdout = os.pipe() # pipe returns(inputfd,outputfd)
pid = os.fork() #make a copy of this process
if pid:
os.close(childStdout) # in parent process after fork
os.close(childStdin) # close child ends in parent
os.dump2(parentStdin,stdinFd) # my sys.stdin copy = pipe1[0]
os.dump2(parentStdout,stdoutFd) # my sys.stdout copy = pipe2[1]
else:
os.close(parentStdin) # in child process afte fork:
os.close(parentStdout) # close parent ends in child
os.dump2(childStdin,stdinFd) # my sys.stdin copy = pipe2[0]
os.dump2(childStdout,stdoutFd) # my sysout copu = pipe1[1]
args = (prog,)+args
os.execvo(prog,args) # new program in this process
assert False,'execvp failed' # os.exec call never returns here if __name__=='__main__':
mypid = os.getpid()
spawn('python','pipes-testchild.py','spawm') # fork child program print('hello 1 from parent',mypid) # to child's stdin sys.stdout.flush() # subvert stdio buffering
reply = input() # from child's stdout
sys.stderr.write('parent got: "%s"\n' % reply) # stderr not tied to pipe print('hello 2 from parent',mypid)
sys.stdout.flush()
reply = sys.stdin.readline()
sys.stderr.write('parent got:"%s"\n' % reply[:-1])

3.Named Pipes(Fifos)pages 234

Create a long-lived pipe that exists as a real named file in the filesystem. such files are called named pipes(or sometime,fifos).

虽然其是任意程序之外的,但是和计算机中真实文件有关,不依赖被其他任务所共享的内存。因此可以用于线程,进程和独立程序的IPC机制。一旦命名管道文件创建,客户端可以通过名字打开并使用正常的文件操作进行读写。其是单向流。典型的操作是,服务器程序从fifos读数据,客户端程序写数据。另外,2个fifos集可以用于实现双向通信,和匿名通信所做的一样。fifos不支持远程网络连接。

 """
name pipes;os.mkfifo is not available on windown
thare is no reason to fork here ,since fifo file ipes
are external to proceses--shared fds in parent/child processes
are irrelevent;
"""
import os,time,sys
fifoname ='/tmp/pipefifo'
def child():
pipeout = os.open(filename,os.O_WRONLY) # open fifo pipe as fd
zzz = 0
while True:
time.sleep(zzz)
msg =('spam %03d\n' %zzz).encode()
os.write(pipeout,msg)
zzz =(zzz+1)%5
def parent():
pipein = open(fifoname,'r') # open file as text file object
while True:
line = pipein.readline()[:-1] #block until data sent
print('parent %d got "%s" at %s' % (os.getpid(),line,time.ctime())) if __name__ =='__main__':
if not os.path.exits(filename):
os.mkfifo(fifename) #create a named pipe file
if len(sys.argv) == 1:
parent() # run as parent if no args
else:
child() # else run as child process

IPC-->PIPO的更多相关文章

  1. Android之使用Bundle进行IPC

    一.Bundle进行IPC介绍 四大组件中的三大组件(Activity.Service.Receiver)都是支持在Intent中传递Bundle数据的,由于Bundle实现了Parcelable接口 ...

  2. Android之使用文件进行IPC

    一.文件进行IPC介绍 共享文件也是一种不错的进程间通信方式,两个进程通过读/写同一个文件来交换数据.在Windows上,一个文件如果被加了排斥锁将会导致其他线程无法对其进行访问,包括读写,而由于An ...

  3. IPC操作时IPC_CREAT和IPC_EXCL选项的说明

    IPC(包括消息队列,共享内存,信号量)的xxxget()创建操作时,可以指定IPC_CREAT和IPC_EXCL选项.以共享内存为例:当只有IPC_CREAT选项打开时,不管是否已存在该块共享内存, ...

  4. TaintDroid剖析之IPC级污点传播

    TaintDroid剖析之IPC级污点传播 作者:简行.走位@阿里聚安全 前言 在前三篇文章中我们详细分析了TaintDroid对DVM栈帧的修改,以及它是如何在修改之后的栈帧中实现DVM变量级污点跟 ...

  5. 为什么使用Binder而不是其他IPC机制

    本文搬运自:Advantages of using Binder for IPC in Android 使用Binder而不是其他(Semaphores , Message Queue, PIPES) ...

  6. 002:IPC与system函数简介

    1:IPC名字mq_XXX,sem_XXX,shm_XXX. 消息队列 信号量 共享内存区 <mqueue.h> <semaphore.h> <sys.mman.h> ...

  7. linux应用程序开发-进程通信(IPC)

    IPC why: 1.数据传输 2.资源共享 目的: 3.通知事件 4.进程控制 发展: 1.UNIX进程间通信 2.基于SYStem V 3.POSIX 方式分类: 1.pipe(管道) FIFO( ...

  8. UNIX:高级环境编程 - 第十五章 IPC:进程间通信

    IPC(InterProcess Communication)进程间通信.为啥没有进程间通信,这是因为进程间都是同步的关系,不需要通信. 1.管道 1.1管道特点: (1)半双工的(即数据只能在一个方 ...

  9. (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  10. Anciroid的IPC机制-Binder概述

    在Linux系统中,是以进程为单位分配和管理资源的.出于保护机制,一个进程不能直接访问另一个进程的资源,也就是说,进程之间互相封闭.但是,在一个复杂的应用系统中,通常会使用多个相关的进程来共同完成一项 ...

随机推荐

  1. package.json 字段全解析

    Name 必须字段. 小提示: 不要在name中包含js, node字样: 这个名字最终会是URL的一部分,命令行的参数,目录名,所以不能以点号或下划线开头: 这个名字可能在require()方法中被 ...

  2. Mac下抓包

    Wireshark针对UNIX Like系统的GUI发行版界面采用的是X Window(1987年更改X版本到X11).Mac OS X在Mountain Lion之后放弃X11,取而代之的是开源的X ...

  3. BICEP单元测试——随机四则运算升级版

    一.测试方法 6个值得测试的具体部位: Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制错误条件发生? ...

  4. Linux下安装搭建WordPress网站

    WordPress简介 WordPress 是一种使用 PHP语言和 MySQL数据库开发的开源.免费的Blog(博客,网志)引擎,用户可以在支持 PHP 和 MySQL 数据库的服务器上建立自己的 ...

  5. git clone时,报403错误,完美解决方案

    首先命令行操作结果如下: root@zhiren-PowerEdge-T110-II:/zrun# git clone https://git.coding.net/xxxxxxxx/xxxx.git ...

  6. Xcode清除缓存等

    Xcode出现一些错误的时候,有时候不是代码的问题,需要清理一下Xcode的缓存或者项目的Product等: 1. Product清理 1.1  Product-Clean 1.2  Product- ...

  7. 微博开放平台api使用

    前言:微博开放平台提供了微博数据的api接口,不仅可以直接通过api调用微博服务发布微博查询微博,更重要的是,可以在自己的网站上获得新浪微博api的授权,调用微博的某些内容,就好像我们再网站中看到好文 ...

  8. 延时调用的php代码

    比如我们想做一个类似于康盛uchome的定时触发任务,任务靠用户访问触发的,但是你触发任务是不能影响用户本身对页面的访问速度(也就是说不能任务执行十秒钟你就让用户等待十秒钟)刚好昨天把这个弄完了.拿出 ...

  9. linux命令日常总结

    1.date 显示系统日期2. mkdir xx 创建xx目录 rmdir xx 删除xx目录(空目录) rm -rf xx 删除xx目录(非空目录) 3. vi xx 创建某文件 写入-->e ...

  10. Spring MVC配置

    web配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="ht ...