D 的个人博客

但行好事莫问前程

  menu
417 文章
3446695 浏览
3 当前访客
ღゝ◡╹)ノ❤️

Linux下进程的创建及其使用管道进行进程之间的通讯[00原创]

/********************************************************************** 

     文件名: multiTasks.c                             
     功能:创建进程,并使用管道进行进程间的通讯
     方法:
     step1:使用fork()函数创建两个子进程1和2              
     step2:创建两个管道1和2
     step3:父进程使用管道1向子进程1传输数据
     step4:子进程1从管道1里读取父进程写的数据
     step5:子进程1使用管道2向子进程2传输数据
     step6:子进程2从管道2里读取子进程1写的数据
     完成step2后父进程进行等待,知道step6完成
     编译环境:Ubuntu 7.04 + gcc 4.1.2    
     作者:88250                             
     日期 :2007/6/25    
     版本:0.0.0.1                 
     作者Blog:
http://blog.csdn.net/DL88250                    
     E-mail & GTalk & MSN:[email protected]                         
     QQ: 845765 or 316281008                              

 *********************************************************************
*/ 

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

#ifndef READ_BUF_MAX
    
#define READ_BUF_MAX 100        /* 定义读缓冲区的大小 */
#endif

#ifndef WRITE_BUF_MAX
    
#define WRITE_BUF_MAX 50        /* 定义写缓冲区的大小 */
#endif

int main(void)
{
    
int pipe_fd[4= {};            /* 创建2条管道 */
        pid_t pids[
2];                /* 分别标识两个子进程 */
     
char r_buf[2][READ_BUF_MAX] = {};    /* 读缓冲区 */
     
char w_buf[2][WRITE_BUF_MAX] = {};    /* 写缓冲区 */
     
int r_num = 0;                /* 读取管道容量的大小 */
          
     
if ((-1 == pids[0]) || (-1 == pids[1]))
     {
/* 创建子进程失败 */
         printf(
"create child tasks error! ");
         
return -1;
     }
     
     
/* 创建管道 */
     
if (pipe(&pipe_fd[0]) < 0)
    {
        printf(
"pipe1 create error! ");
        
return -1;
    }

    
if (pipe(&pipe_fd[2]) < 0)
    {
        printf(
"pipe2 create error! ");
        
return -1;
    }
     
     
if (0 == (pids[0= fork()))
     {
/* 子进程1 */ 
         
/* ---- 读取父进程发送到管道1里的数据 ----*/
         close(pipe_fd[
1]);    /* 关闭父进程的写端 */
         sleep(
1);        /* 确保父进程关闭写端 */
         r_num 
= read(pipe_fd[0], r_buf[0], READ_BUF_MAX);
         printf(
"read num is: %d (in child1) ", r_num);
         printf(
"the data read from the pipe1 is: %s", r_buf[0]);
         close(pipe_fd[
0]);    /* 关闭子进程1的读端 */
         
/* -------------------------------- */
         
         
/* ---- 发送数据到管道2里 ---- */
         close(pipe_fd[
2]);    /* 关闭子进程2的读端 */
         strcpy(w_buf[
1], "you are brother2, I'm you brother1. ");    /* 写数据到缓冲区1 */
         
/* 写数据到管道2 */
         
if (write(pipe_fd[3], w_buf[1], WRITE_BUF_MAX) != -1)
         {
                printf(
"child1 write data to pipe2 finished! ");
         }
         
/* 关闭子进程1的写端 */
         close(pipe_fd[
3]);
         printf(
"child1 close fd[3] finished! ");
         exit(
0);
     }
     
else if (0 == (pids[1= fork()))
     {
/* 子进程2 */ 
         close(pipe_fd[
3]);    /* 关闭子进程1的写端 */
         sleep(
1);        /* 确保子进程1关闭写端 */
         r_num 
= read(pipe_fd[2], r_buf[1], READ_BUF_MAX);
         printf(
"read num is: %d (in child2) ", r_num);
         close(pipe_fd[
2]);    /* 关闭子进程2的读端 */
         printf(
"the data read from the pipe2 is: %s", r_buf[1]);            
         exit(
0);
     }
     
else
     {
/* 父进程 */
         
/* 关闭子进程的读端 */
         close(pipe_fd[
0]);
         
/* 写数据到缓冲区0 */
         strcpy(w_buf[
0], "you are child1, I'm your father. ");    
         
         
/* 写数据到管道 */
         
if (write(pipe_fd[1], w_buf[0], WRITE_BUF_MAX) != -1)
         {
                printf(
"parent write data to pipe1 finished! ");
         }
         
/* 关闭父进程的写端 */
         close(pipe_fd[
1]);
         printf(
"parent close fd[1] finished! ");

         sleep(
3);
         
         
return 0;
     }
}