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. CSS生僻问题一网打尽

    CSS生僻问题一网打尽 伪类和伪元素 伪类 何为伪类? 像类不是类,不是自己声明的类(不写样式也存在). 对伪元素的认识在早期网页上的超链接.链接(锚啊)用下划线标出来,点击后链接变紫色,鼠标悬上去变 ...

  2. uva10986 堆优化单源最短路径(pas)

    var n,m,s,t,v,i,a,b,c:longint;//这道题的代码不是这个,在下面 first,tr,p,q:..]of longint; next,eb,ew:..]of longint; ...

  3. 如何让一个类可以被foreach枚举?

    答:实现IEnumerable或其IEnumerable<T>.该接口只有一个函数 public IEnumerator GetEnumerator(); 在这里,IEnumerator也 ...

  4. xcode8插件无法使用

    一,xcode8无法使用插件的问题 创建新的XcodeSigner代码证书,并执行"sudo codesign -f -s XcodeSigner /Applications/Xcode.a ...

  5. 02.JavaScript基础上

    JavaScript组成 ECMAScript:解释器.翻译 .平时我们写的代码都是用英文数字之类,而计算机只能读懂0和1,ECMAScript可以把我们写的翻译给计算机,把计算机写的传达给我们DOM ...

  6. python3 抓取网页资源的 N 种方法

    1. 最简单 import urllib.request response = urllib.request.urlopen('http://python.org/') html = response ...

  7. ps工具箱总结

    1.矩形工具 四个属性 1.选区2.重叠选区3.减去选区4.区域化缩小选区 样式:固定比例 固定大小 正常 //前两项可以设置宽高 3.快速选择工具.魔棒工具 快速选择工具: 三个属性 1.选区2.增 ...

  8. SQL SERVER 数据库操作脚本

    创建数据库 create Database MYDB on ( Name=mydb_dat, FileName='c:\data\mydate.mdf',size=10,maxsize=50 ) LO ...

  9. canvas中的rotate的使用方法

    今天在绘制一个足球滚动的时候,想使用rotate方法,之前看到这个方法的时候,并没有引起任何重视,无非就是和CSS3里的rotate一样的用么... 遗憾的是,事实并非如此,由于代码在公司,我也就不去 ...

  10. js中event的target和currentTarget

    js 中的event是个很有用的对象,不同的浏览器,处理方式可能不一样. 我们现在只考虑标准的浏览器的情况 event是表示时间触发的产生的对象,以click事件为例. 由于冒泡的存在,event对象 ...