linuxC多进程通讯---使用fifo实现一个log日志系统

本文介绍了如何使用Linux的FIFO管道实现一个多进程的日志系统。各个进程通过FIFO向守护进程发送日志信息,守护进程接收到后将其存储到相应的日志文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录

任务

Log日志系统
•各个进程往FIFO管道写入数据
•守护进程使用FIFO接收各个进程的输出日志信息
•并将FIFO中的数据写到对应的日志文件中
在这里插入图片描述

举例

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define handle_error(msg) \
    {perror(msg);exit(EXIT_FAILURE);}
#define FIFO_SERVER "fifo_log_server"
#define LOG_PATHNAME    "/var/log/process.log"

int main (void)
{
	mkfifo (FIFO_SERVER, 0644);
	int ret_from_fork;
	char public_buf[100];
		
	int fifo_fd;
	fifo_fd = open (FIFO_SERVER, O_RDONLY);
	memset (public_buf, 0, 100);

	int fd;
	fd = open (LOG_PATHNAME, O_WRONLY | O_CREAT | O_APPEND);
    if (fd == -1)
        handle_error("open");
	int read_len;
	while (1)
	{
		read_len = read (fifo_fd, public_buf, 100);
		if (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值