栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

linux 有名管道c++,golang

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux 有名管道c++,golang

linux C++ 有名管道_dasgk的专栏-CSDN博客

golang 有名管道通信 总结分析

golang 有名管道通信 总结分析_wjw7869的专栏-CSDN博客

 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//#define FIFO_READ "/tmp/writefifo"
#define FIFO_WRITE "/tmp/readfifo"
 
#define BUF_SIZE 1024
 
int main()
{
   int wfd;
    char ubuf[BUF_SIZE]={0};
   umask(0);
   if(mkfifo(FIFO_WRITE,S_IFIFO|0666))
   {
      cout<<"sorry the namedpipe created error"<>ubuf;
	  if(strcmp(ubuf,"exit")==0)
	  {
		  exit(0);
	  }
	 int leng= write(wfd,ubuf,strlen(ubuf));
	 if(leng>0)
	 {
		 cout<<"write..."< 

 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define FIFO_READ "/tmp/readfifo"
 
#define BUF_SIZE 1024
 
int main()
{
   int rfd;
   char ubuf[BUF_SIZE]={0};
   umask(0);
   while((rfd=open(FIFO_READ,O_RDONLY))==-1)
   {
	  cout<<"open..."<0)
     {
       ubuf[len]='';
       cout<<"server:"< 

package main
 
import (
    "fmt"
    "log"
    "os"
    "time"
    "bufio"
    "syscall"
)
 
var pipeFile = "/tmp/pipe.ipc"
 
func main() {
    os.Remove(pipeFile)
    err := syscall.Mkfifo(pipeFile, 0666)
    if err != nil {
        log.Fatal("create named pipe error:", err)
    }
    go write()
    go read()
    for {
        time.Sleep(time.Second * 1000)
    }
}
 
func read(){
    fmt.Println("open a named pipe file for read.")
    file, _ := os.OpenFile(pipeFile, os.O_RDWR, os.ModeNamedPipe)
    reader := bufio.NewReader(file)
 
    for {
        line, err := reader.ReadBytes('n')
        fmt.Println("read...")
        if err == nil {
            fmt.Print("load string: " + string(line))
        }
    }
}
 
func write() {
    fmt.Println("start schedule writing.")
    f, err := os.OpenFile(pipeFile, os.O_RDWR, 0777)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
    }
    i := 0
    for {
        fmt.Println("write string to named pipe file.")
        f.WriteString(fmt.Sprintf("test write times:%dn", i))
        i++
        time.Sleep(time.Second)
        if i == 10{
            break
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号