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. GridView,Repeater增加自动序号列

    有三种实现的方式, 第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. <Columns> <asp:TemplateField Hea ...

  2. Android服务(Service)研究

    Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...

  3. EasyUI配置和组件

    首先在webcontent添加配置文件 新建静态或动态网站,在title的下面加入五个配置文件路径,注意:循序不能乱 <!-- 顺序不可以乱 --> <!-- 1.jQuery的js ...

  4. UIPickerView理解

  5. 20145205 实验一 Java开发环境的熟悉

    实验内容 命令行下Java程序开发 IDEA下Java程序开发.调试 练习(通过命令行和Eclipse两种方式实现,在Eclipse下练习调试程序) 实现凯撒密码,并进行测试 实验要求 使用JDK编译 ...

  6. Oracel EBS - Response

    Oracle ERP Response:

  7. 【emWin】例程三:显示方向的切换

    实验指导书及代码包下载: http://pan.baidu.com/s/1pK9o0xP

  8. iOS UITableViewCell的"滑动出现多个按钮"

    本文授权转载,作者:@夏天是个大人了 前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉 ...

  9. 关于IOS音频的开发积累

    1.设置类别,表示该应用同时支持播放和录音 OSStatus error; UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; ...

  10. [原创]Hadoop默认设置导致NameNode启动失败一例

    看到市面上很多书在讲解Hadoop的时候都轻描淡写的提到了HDFS的设置问题.大多采取的是默认设置,最多也就是设置一些副本数量之类. 笔者在工作中遇到了这样一种情况:每次重启系统之后,NameNode ...