IPC-->PIPO
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的更多相关文章
- Android之使用Bundle进行IPC
一.Bundle进行IPC介绍 四大组件中的三大组件(Activity.Service.Receiver)都是支持在Intent中传递Bundle数据的,由于Bundle实现了Parcelable接口 ...
- Android之使用文件进行IPC
一.文件进行IPC介绍 共享文件也是一种不错的进程间通信方式,两个进程通过读/写同一个文件来交换数据.在Windows上,一个文件如果被加了排斥锁将会导致其他线程无法对其进行访问,包括读写,而由于An ...
- IPC操作时IPC_CREAT和IPC_EXCL选项的说明
IPC(包括消息队列,共享内存,信号量)的xxxget()创建操作时,可以指定IPC_CREAT和IPC_EXCL选项.以共享内存为例:当只有IPC_CREAT选项打开时,不管是否已存在该块共享内存, ...
- TaintDroid剖析之IPC级污点传播
TaintDroid剖析之IPC级污点传播 作者:简行.走位@阿里聚安全 前言 在前三篇文章中我们详细分析了TaintDroid对DVM栈帧的修改,以及它是如何在修改之后的栈帧中实现DVM变量级污点跟 ...
- 为什么使用Binder而不是其他IPC机制
本文搬运自:Advantages of using Binder for IPC in Android 使用Binder而不是其他(Semaphores , Message Queue, PIPES) ...
- 002:IPC与system函数简介
1:IPC名字mq_XXX,sem_XXX,shm_XXX. 消息队列 信号量 共享内存区 <mqueue.h> <semaphore.h> <sys.mman.h> ...
- linux应用程序开发-进程通信(IPC)
IPC why: 1.数据传输 2.资源共享 目的: 3.通知事件 4.进程控制 发展: 1.UNIX进程间通信 2.基于SYStem V 3.POSIX 方式分类: 1.pipe(管道) FIFO( ...
- UNIX:高级环境编程 - 第十五章 IPC:进程间通信
IPC(InterProcess Communication)进程间通信.为啥没有进程间通信,这是因为进程间都是同步的关系,不需要通信. 1.管道 1.1管道特点: (1)半双工的(即数据只能在一个方 ...
- (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- Anciroid的IPC机制-Binder概述
在Linux系统中,是以进程为单位分配和管理资源的.出于保护机制,一个进程不能直接访问另一个进程的资源,也就是说,进程之间互相封闭.但是,在一个复杂的应用系统中,通常会使用多个相关的进程来共同完成一项 ...
随机推荐
- 在Lingo中输入矩阵(通过Excel)
举例说明:我要将'C:\Users\Lenovo\Desktop\lingodata.xlsx'里的数据导入lingo1.左键拖动选中'C:\Users\Lenovo\Desktop\lingodat ...
- c#内部类的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 五分钟理解一致性哈希算法(consistent hashing)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法 ...
- macos开发pgsql数据库
mac安装Postgresql作为数据库 最简单的方式是安装Postgres.App. 这个应用里自带了最新版本的PostgreSQL而且不需要学习数据库服务器启动和关闭的命令.程序安好后(别忘了拖拽 ...
- 初用idea建立javaweb遇到的问题与心得
1.直接用idea建立的web项目,其自动生成的web.xml里version=3.1,这样的话建立servlet-name等标签会报错(因为3.1不支持这种做法,更提倡用注解的办法),解决办法是将w ...
- css3动态旋转魔方练习
图片素材: html部分: <!DOCTYPE html><html><head> <meta charset="utf-8" /> ...
- #20145205 《Java程序设计》第10周学习总结
教材学习内容总结 教材学习内容总结 Java的网络编程 •网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. •java.net包中J2SE的API包含有类和接口,它们提供低 ...
- Git系列教程三 配置与基本命令
一.安装Git 网上有很多安装教程,可以参考.这里使用的是Windows版本的Git,点击这里下载. 二.基本设置 安装完成后,通过点击鼠标右键就可以看到新添加了俩个Git命令:Git GUI Her ...
- 利用xtrabackup备份mysql数据库
利用xtrabackup备份mysql数据库 一.安装1.直接下载二进制文件wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2. ...
- java基础总结——基础语法2(语句)
1.1. 判断语句(if语句) 1.1.1. If语句的三种格式: 1.1.2. if语句特点: 每一种格式都是单条语句(单条不是单行,单条是一个整体). 第二种格式与三元运算符的区别:三 ...