java linuc进程间通信对比
编程技术  /  houtizong 发布于 3年前   62
import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;public class CommunicateWhitPiping {public static void main(String[] args) {PipedOutputStream pos = new PipedOutputStream();PipedInputStream pis = new PipedInputStream();try {pos.connect(pis);//将管道输入流与输出流连接 此过程也可通过重载的构造函数来实现} catch (IOException e) {e.printStackTrace();}Producer p = new Producer(pos);//创建生产者线程Consumer c = new Consumer(pis);//创建消费者线程p.start();//启动线程c.start();}}class Producer extends Thread {//生产者线程(与一个管道输入流相关联)private PipedOutputStream pos;public Producer(PipedOutputStream pos) {this.pos = pos;}public void run() {int i = 99;//256,257,注意这里!!!!!!!!try {pos.write(i);} catch (IOException e) {e.printStackTrace();}}}class Consumer extends Thread {//消费者线程(与一个管道输入流相关联)private PipedInputStream pis;public Consumer(PipedInputStream pis) {this.pis = pis;}public void run() {try {System.out.println(pis.read());} catch (IOException e) {e.printStackTrace();}}}
#include "unpipc.h"void client(int, int), server(int, int);intmain(int argc, char **argv){ int pipe1[2], pipe2[2]; pid_t childpid; Pipe(pipe1); /* create two pipes */ Pipe(pipe2); if ( (childpid = Fork()) == 0) { /* child */ Close(pipe1[1]); Close(pipe2[0]); server(pipe1[0], pipe2[1]); exit(0); } /* 4parent */ Close(pipe1[0]); Close(pipe2[1]); client(pipe2[0], pipe1[1]); Waitpid(childpid, NULL, 0); /* wait for child to terminate */ exit(0);}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接