In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to achieve pipeline communication in Linux, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.
I. Definitions
The pipeline is one-way, first-in, first-out. It connects the input of one program to the output of another. After the data is read by a process, it is deleted from the pipeline. It is divided into nameless and famous pipes. The former is used for communication between parent and child processes, while the latter is used for communication between two processes in the same system.
II. Unnamed pipeline
The code is as follows:
int pipe(int fd[2]);
Where fd[0] is used to read pipes and fd[1] is used to write pipes. Returns zero if successful,-1 otherwise, error reason stored in errno.
Third, the famous pipeline: FIFO
The code is as follows:
int mkfifo(const char* pathname,mode_t mode)
Use O_NONBLOCK when open, and immediately return an error if the access requirements cannot be met. Erron is ENXIO.
Examples:
The code is as follows:
fread. c//Read files
#include
#include
#define FIFO "myfifo"
main(){
int fd;
char buff[100];
if(access(FIFO,F_OK) == -1){
mkfifo(FIFO,0777);
}
fd=open(FIFO,O_RDONLY| O_NONBLOCK); //Set non-blocking on, otherwise it will block the read function when there is no input
int num;
while(1){
memset(buff,'\0', 100); //garbled if last character is not cleared
if((num=read(fd,buff,100))==0){
printf("waiting.....\ n");
sleep(1);
continue;
}
printf("read %d in fifo , it's %s",num,buff);
sleep(1);
}
}
fwrite. c//Write a file
#include
#include
#include
#define FIFO "myfifo"
main(){
int fd;
char buff[100];
memset(buff,'\0',100);
scanf("%s",buff);
if(access(FIFO,F_OK) == -1){
mkfifo(FIFO,0777);
}
fd=open(FIFO,O_WRONLY);
int num;
num=write(fd,buff,strlen(buff));
printf("%d char is written! It's %s\n",num,buff);
}
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.