简体   繁体   English

在C编程中使用管道

[英]using pipe in c programming

Im using pipe in c to create a programme that determine if the entered integer number by user is even or odd. 我使用c中的管道来创建一个程序,该程序确定用户输入的整数是偶数还是奇数。 Also, It should achieve the following specifications : The Parent process should send the integer number to the forked child process. 另外,它应满足以下规范:父进程应将整数发送给派生的子进程。 The child process should receive the sent integer number to determine his type (even or odd). 子进程应接收发送的整数以确定其类型(偶数或奇数)。 Then, the result should be returned back to parent process in order to display it on the shell for the user. 然后,应将结果返回给父进程,以便在外壳上为用户显示。 I have to Use Pipes IPC to exchange data between parent and child processes. 我必须使用管道IPC在父进程和子进程之间交换数据。

my code is as shown below 我的代码如下所示

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>

void sendNumber(int);
void RecResult();
int f_des[2];

int main(int argc, char *argv[])
{
    static char message[BUFSIZ];
    if ( pipe(f_des) == -1 )
    {
        perror("Pipe");
        exit(2);
    }
    switch ( fork() )
    {

        case -1: perror("Fork");
            exit(3);

        case 0: /* In the child */
        {

            // read a meesage
            close(f_des[1]);
            read(f_des[0], message, BUFSIZ  );

            int num=atoi(message);
            printf("Message received by child: [%d]\n", num);


            if(num % 2 == 0)

                sprintf(message,"%d is Even \0",num);

            else

                sprintf(message,"%d is Odd \0",num);

            close(f_des[0]);
            write(f_des[1], message, BUFSIZ);
            exit(0);
        }

            break;
        default: /* In the parent */
        {
            int num;
            printf("\n Enter an Integer \n");
            scanf("%d",&num);
            char msg[BUFSIZ];
            sprintf(msg,"%d",num);

            // write a message
            close(f_des[0]);
            write(f_des[1], msg, BUFSIZ);

            printf("\n Waiting Child \n");

            wait(NULL); 
            // read a mesaage
            static char message[BUFSIZ];
            close(f_des[1]);
            read(f_des[0], message, BUFSIZ);

            printf("========> Result:  %s . \n",message);
        }
    }
}

the child receive the message successfully but the parent does not receive any result :S any one can help here is my output 孩子成功收到消息,但父母没有收到任何结果:S任何人都可以帮忙的是我的输出

rose@ubuntu:~$ gcc -o test_3 test_3.c
rose@ubuntu:~$ ./test_3

 Enter an Integer 
3

 Waiting Child 
Message received by child: [3]
========> Result:   . 
rose@ubuntu:~$ 

thaanks all ;) 谢谢所有;)

A PIPE channel is a uni-directional communication channel. PIPE通道是单向通信通道。 If you want the child process to write back to the parent process, you will need a second PIPE channel, different than the one used for receiving the message from the parent. 如果希望子进程写回父进程,则将需要第二个PIPE通道,该通道不同于用于从父进程接收消息的通道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM