设为首页 加入收藏

TOP

Linux下使用C/C++编写一个简单的消息处理程序
2014-11-24 08:45:01 来源: 作者: 【 】 浏览:0
Tags:Linux 使用 C/C 编写 一个 简单 消息 处理 程序

// all.h
// wenxy created in 2005/04/12,AM
// All copyright reserved.



#ifndef _ALL_H
#define _ALL_H


// ANSC C/C++
#include
#include
#include
#include


// linux
#include
#include
#include
#include
#include


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


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



#endif


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


// 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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ubuntu编译安装boost并在eclipse .. 下一篇mini2440编译内核:usr/lib/libst..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·有没有哪些高效的c++ (2025-12-27 08:20:57)
·Socket 编程时 Accep (2025-12-27 08:20:54)
·计算机网络知识点总 (2025-12-27 08:20:52)
·一篇说人话的文章, (2025-12-27 07:50:09)
·Python Web框架哪家 (2025-12-27 07:50:06)