简体   繁体   中英

Named pipes in c

I am trying to use named pipes in C and am running into some difficulty. In terms of anonymous pipes, I just create the pipe with the r/w descriptors and then close the opposite end every time I want to do a read or write. This is easy to do since I can just open() and close() the other end every time.

With named pipes, I am a bit confused, I found the instruction mkfifo() which creates the named pipe but don't understand how to read and write to it properly.

Thanks

After the pipe has been created with mkfifo() (which could have been done at any point in the past - named pipes exist in the filesystem until they're unlinked), the reading side opens it using open("/path/to/pipe", O_RDONLY) and the writing side opens it with open("/path/to/pipe", O_WRONLY) .

After that it can be used just like an anonymous pipe.

There's nothing much to it. Use mkfifo to make the pipe and then have your processes read and write to it like any file. It's not C specific either. You can do this:

mkfifo testfifo

cat testfifo

And then in another window

echo "hello, world" > testfifo

我认为您应该只使用管道,因为不管每个过程花费的时间如何,它们都会处理不同进程之间的数据传输

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM