Linux进程间的通信——有名管道fifo与守护进程deamon
来源:互联网 发布:linux mint和ubuntu 编辑:程序博客网 时间:2024/04/30 09:11
前面已经有两篇文章分别对有名管道fifo和守护进程deamon有过介绍。
这里主要是以一个简单的实例来介绍如何结合使用fifo与deamon。
eg:通过fifo有名管道实现一个窗口收取信息,另一个窗口发送信息,其中收取信息窗口将收取信息进程设置为守护进程。
/* ============================================================================ Name : readfifo.c Author : Allen Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include<string.h>#include<errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void setdaemon(){ pid_t pid = fork(); if (pid ==-1) { printf("fork failed: %s\n", strerror(errno)); exit(0); } if (pid == 0) { setsid(); chdir("/"); umask(0); /*close(STDOUT_FILENO);//这里要注释掉,否则屏幕不能输入信息 close(STDIN_FILENO); close(STDERR_FILENO); */ } if (pid > 0) { exit(0); }}int readfifo(){ int fd = open("/home/vicent/6/fifo1", O_RDONLY); if (fd == -1) { printf("open failed %s\n", strerror(errno)); return -1; } char buf[1024]; memset(buf, 0, sizeof(buf)); int len = 0; while ((len = read(fd, buf, sizeof(buf)))>0) { write(STDOUT_FILENO,buf,sizeof(buf)); //printf("%s",buf); memset(buf, 0, sizeof(buf)); } close(fd); return 0;}int main(){ setdaemon(); readfifo(); return 0;}
/* * writefifo.c * * Created on: 2016年10月16日 * Author: Allen */#include <stdio.h>#include <stdlib.h>#include<string.h>#include<errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int writefifo(){ int fd = open("/home/vicent/6/fifo1", O_WRONLY); if (fd == -1) { printf("open failed %s\n", strerror(errno)); return -1; } char buf[1024]; memset(buf, 0, sizeof(buf)); while (1) { memset(buf, 0, sizeof(buf)); read(STDIN_FILENO, buf, sizeof(buf)); write(fd, buf, sizeof(buf)); } close(fd); return 0;}int main(){ writefifo(); return 0;}
结果:
写窗口
读窗口
读取进程为守护进程,过程中可以做其他内容,一旦有消息发送过来,就会显示在屏幕
1 0
- Linux进程间的通信——有名管道fifo与守护进程deamon
- Linux进程间通信——有名管道 FIFO 详解
- 进程间的通信----有名管道fifo
- linux下进程间的通信——有名管道fifo学习笔记
- 【七】 进程间通信——(FIFO)有名管道
- 进程间通信——有名管道(FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- linux进程通信----FIFO(有名管道)
- 进程间通信______有名管道(FIFO)
- 进程通信-有名管道FIFO
- linux进程间通信-有名管道(FIFO)
- linux进程间通信-有名管道(FIFO)
- Linux进程间通信二--有名管道FIFO
- linux编程---进程间通信---FIFO---有名管道
- Linux环境进程间通信之有名管道(FIFO)的注意事项
- Linux进程通信 有名管道实现守护进程
- Error parsing D:\AndroidSDK\sdk\system-images\android-24\android-wear\x86\devices.xml
- strongswan 配置ikev2 for iOS and Android
- 面向对象和面向过程有什么区别
- 10,信号
- 浮点型和十六进制之间的转换
- Linux进程间的通信——有名管道fifo与守护进程deamon
- Java并发包:阻塞队列(BlockingQueue)
- 强大UI控件Telerik UI for Xamarin发布R3 2016 BETA|附下载
- Mysql与Oracle区别
- empty、isset、isnull的区别
- phpMyAdmin查看创建表SQL语句的方法
- android Vulkan 图形新特性研究
- Android中静态方式破解某App实现所有视频app去除广告功能
- Func与Action委托泛型介绍