How do I pipe into a third process?

Apple

This is what my code looks like:

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

char* reverseString(char*);

int main(void)
{
    pid_t id;
    int fd[2], nbytes;
    char string[80];
    char readbuffer[80];
    char* reversed;
    pipe(fd);

    if((id = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }
    if(id != 0)
    {
        close(fd[0]);
        printf("PARENT\n--------------------\n");
        // printf("fork: %d\n", id);
        printf("my pid is %d\n", getpid());
        // printf("my parent's pid is %d\n", getppid());
        printf("enter string: ");
        scanf("%s", string);
        write(fd[1], string, (strlen(string)+1));

    }
    else
    {
        close(fd[1]);
        printf("CHILD\n--------------------\n");
        // printf("fork: %d\n", id);
        printf("my pid is %d\n", getpid());
        // printf("my parent's pid is %d\n", getppid());
        nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
        printf("Received string: %s\n", readbuffer);
        reversed = reverseString(readbuffer);
        printf("Reversed string: %s\n", reversed);

        pid_t id2;
        int fd2[2];
        char readbuffer2[80];

        pipe(fd2);

        if((id2 = fork()) != 0)
        {
            close(fd2[0]);
            printf("My pid: %d\n", getpid());
            write(fd2[1], reversed, (strlen(reversed)+1));
        }
        else
        {
            close(fd2[1]);
            printf("My Child's PID: %d\n", getpid());
            nbytes = read(fd2[0], readbuffer2, sizeof(readbuffer2));
            printf("Received string2: %s\n", readbuffer2);
        }

    }
    return (0);
}

char* reverseString(char* str)
{
    char reversed[80];
    int n = strlen(str);
    for(int i = n - 1, j = 0; i >= 0; i--, j++)
        reversed[j] = str[i];
    reversed[n] = '\0';
    return reversed;
}

I'm trying to pipe the reversed string into the third process forked in the second process, but it is not showing up.

Also, when I do scanf after enter, the printing keeps going until the child part is printed, then I enter the string. How do I keep it from printing everything. It looks like this:

PARENT
--------------------
my pid is 821
enter string: CHILD
--------------------
my pid is 822
$
Haris

In reverseString function you are returning the array address which is an automatic local variable. Such variables gets destroyed when the function returns.

char* reverseString(char* str)
{
    char reversed[80];
    // local array, cannot use it after the function returns

Either use malloc() to allocate memory which will be preserved in between function calls, or reverse the string inplace.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Process grep with pipe returns itself. How do I exclude it?

From Dev

How do I use pipe to keep the value that is in a child process?

From Dev

How can I pipe input to a process?

From Dev

Bash: how do I pipe stdout and stderr of one process to two different processes?

From Dev

How do I use the third argument in a for loop?

From Dev

How do I cancel third party task?

From Dev

Bash -- how do I extract the third number

From Dev

How do I cancel a PIPE operation?

From Dev

How do I pipe HEREDOC to head?

From Dev

How do I use subset in a ggplot pipe?

From Dev

How do I pipe HEREDOC to head?

From Dev

How do I write a pipe filter in Rust?

From Dev

How can I pipe a Python process' output in Python?

From Dev

Where do my ANSI escape codes go when I pipe to another process? Can I keep them?

From Dev

How to launch a pipe as a persistent process

From Dev

How do I install third-party rhythmbox plugins?

From Java

How do I integrate a third-party library into october cms?

From Dev

Bash: How do I combine two arrays into a third array?

From Dev

How do I alternate every second and third background of a list/row?

From Dev

How do I find the third most frequent value in google sheets

From Dev

How do I call third party APIs from with firebase?

From Dev

How do I mock Third parties in cucumber Stepdefs?

From Dev

How do I make the Caps Lock key a third Shift key?

From Dev

How do I install third-party rhythmbox plugins?

From Dev

How do i find the nearest value and place it in a third column?

From Dev

How do I alternate every second and third background of a list/row?

From Dev

How do I copy columns of two files as rows of a third file

From Dev

How do I add a third criteria to this Excel formula?

From Dev

How do I make the Caps Lock key a third Shift key?

Related Related

  1. 1

    Process grep with pipe returns itself. How do I exclude it?

  2. 2

    How do I use pipe to keep the value that is in a child process?

  3. 3

    How can I pipe input to a process?

  4. 4

    Bash: how do I pipe stdout and stderr of one process to two different processes?

  5. 5

    How do I use the third argument in a for loop?

  6. 6

    How do I cancel third party task?

  7. 7

    Bash -- how do I extract the third number

  8. 8

    How do I cancel a PIPE operation?

  9. 9

    How do I pipe HEREDOC to head?

  10. 10

    How do I use subset in a ggplot pipe?

  11. 11

    How do I pipe HEREDOC to head?

  12. 12

    How do I write a pipe filter in Rust?

  13. 13

    How can I pipe a Python process' output in Python?

  14. 14

    Where do my ANSI escape codes go when I pipe to another process? Can I keep them?

  15. 15

    How to launch a pipe as a persistent process

  16. 16

    How do I install third-party rhythmbox plugins?

  17. 17

    How do I integrate a third-party library into october cms?

  18. 18

    Bash: How do I combine two arrays into a third array?

  19. 19

    How do I alternate every second and third background of a list/row?

  20. 20

    How do I find the third most frequent value in google sheets

  21. 21

    How do I call third party APIs from with firebase?

  22. 22

    How do I mock Third parties in cucumber Stepdefs?

  23. 23

    How do I make the Caps Lock key a third Shift key?

  24. 24

    How do I install third-party rhythmbox plugins?

  25. 25

    How do i find the nearest value and place it in a third column?

  26. 26

    How do I alternate every second and third background of a list/row?

  27. 27

    How do I copy columns of two files as rows of a third file

  28. 28

    How do I add a third criteria to this Excel formula?

  29. 29

    How do I make the Caps Lock key a third Shift key?

HotTag

Archive