Linux下,使用C/C++一个简单的消息处理程序

本文介绍了一个简单的父子进程间通过管道进行通信的C语言程序示例。该程序创建了一个子进程,并利用管道在父进程和子进程之间传递字符串数据。父进程写入数据到管道,而子进程从管道读取数据并打印。

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

// all.h

#ifndef _ALL_H
#define _ALL_H

// ANSC C/C++
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <assert.h>

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

// micro define
//#define OutErrStr(info) (printf("Error : %s/n", info))

//#define OutErrInt(info) (printf("Error : %d/n", info))
#define BUFF_SIZE 1024 * 1


#endif

 

/* end all.h ******************************************************* */

 

 

// main.c

#include "all.h"

// function
static int ChildProcessFun(void *pParam);

// struct
struct _PROCESS_PARAM
{
 #define STR_1 "This is date on pipe."
 #define DADA_LEN sizeof(STR_1)
 char chBuff[BUFF_SIZE];
 int nPipe[2];
 pid_t nProcess;
};


int main()
{
 printf("Run .../n");
 

 struct _PROCESS_PARAM ProcessParam;

 int nFlag = pipe(ProcessParam.nPipe);
 if (nFlag == -1)
 {
  printf("Error: crrete pipe failed!/n");
  printf("------------------------------/n/n");
 }
 assert(ProcessParam.nPipe[0]);
 assert(ProcessParam.nPipe[1]);
 
 // create a child press
 ProcessParam.nProcess = fork();
 if (ProcessParam.nProcess < 0) /* create process failed */
 {
  printf("Error: create child process failed!/n");
  printf("------------------------------/n/n");
 }
 else if (ProcessParam.nProcess > 0) /* parent process */
 {
  printf("/nNote:  in parent process .../n");
  printf("New child process ID = %d/n", ProcessParam.nProcess);
 
  // write pipe
  close(ProcessParam.nPipe[0]);
  write(ProcessParam.nPipe[1],  STR_1, strlen(STR_1));
 }
 else /* child process */
 {
  if(ChildProcessFun(&ProcessParam) == 0)
  {
   printf("Child process exit .../n");
  }
  else
  {
   printf("Error: Child process dead lock .../n");
   printf("------------------------------/n/n");
  }
 }

 
 // close pipe
 close(ProcessParam.nPipe[0]);
 close(ProcessParam.nPipe[1]);
 printf("parent process exit .../n");
 return 0;
}


// child process funtion
static int ChildProcessFun(void *pParam)
{
 struct _PROCESS_PARAM ProcessParam = /
          (struct _PROCESS_PARAM) /
          *(struct _PROCESS_PARAM *)pParam;

 printf("Note: in child process .../n");
 printf("The child process ID = %d/n", ProcessParam.nProcess);
 
 // read pipe
 close(ProcessParam.nPipe[1]);
 read(ProcessParam.nPipe[0], ProcessParam.chBuff, DADA_LEN);
 ProcessParam.chBuff[DADA_LEN] = '/0'; /* good action */
 printf("Read the pipe data: %s/n", ProcessParam.chBuff);
 printf("*********************************/n/n");
 return 0; 
}


/* end main.c ******************************************************* */

// -----------------------------------------------------------------------------------------------

# makefile

objets = main.o
rubbish = $(objets) main


main : main.o
 g++ -o main main.o

main.o : main.c all.h
 g++ -c main.c


.PHONY : clean
clean :
 -rm $(rubbish)
 


# end makefile

 

/* end makefile ******************************************************* */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值