modify text in child process to use in parent

Viewed 27

I am trying to create a simple pipe/fork function, so that child process modifies the value of text, then it is printed by the parent. I have checked a similar question on Modify variable in child process, but I am unable to print the text variable in the parent.

int main()
{
pid_t childp;
char text[100];
pipe(text);
childp = fork();
if (childp ==0){
    strncpy(text, "Hello world", 100); // child running
}
else{
    printf("%s\n", text); // parent prints "Hello world"
}
return 1;
}

Any help is appreciated (I am very new to C language)

2 Answers

look into this website :

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

int main(void)
{

    int     fd[2], nbytes;
    pid_t   childpid;
    char    string[] = "Hello, world!\n";
    char    readbuffer[80];

    pipe(fd);
    
    if((childpid = fork()) == -1)
    {
            perror("fork");
            exit(1);
    }

    if(childpid == 0)
    {
            /* Child process closes up input side of pipe */
            close(fd[0]);

            /* Send "string" through the output side of pipe */
            write(fd[1], string, (strlen(string)+1));
            exit(0);
    }
    else
    {
            /* Parent process closes up output side of pipe */
            close(fd[1]);

            /* Read in a string from the pipe */
            nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
            printf("Received string: %s", readbuffer);
    }
    
    return(0);
}

It explain how to properly use C pipes. Take a look to this too.

Add this for error handling :

if (pipe(fd) == -1) {
    fprintf(stderr, "Pipe Failed");
    return 1;
}

The closest to your code working example I can come up with.

int main()
{
   pid_t childp;
   char text[100];
   int fd[2];
   pipe(fd);
   childp = fork();
   if (childp ==0){
      FILE *f=fdopen(fd[1], "w"); // Write into this "file" what you want the other end to read.
      fprintf(f, "Hello world\n"); 
   }
   else{
      FILE *f=fdopen(fd[0], "r"); // We can read from this "file"
      fgets(text, 100, f); // Read one line of text from the "file" up to 100 bytes
      printf("read <%s> from by new child\n", text)
   }
return 1;
}

Note again that this is not a shared buffer. So you need both end to agree on a "protocol". Because everything that is written by one end must be read by the other (otherwise the "write" instruction will be blocked), and everything that is read by one end, must be writter by the other (otherwise the "read" instruction will be blocked).

So, either you use a fixed size message, for example. If you choose 100, you need to write 100 bytes exactly at one end (fill with 0 if needed), and read 100 bytes exactly at the other. Or you find some protocol so that the reading end knows exactly when to stop reading.

I choose the latter (because it is the closest to your code). By using fgets to read, that stop to read at each newline, and fprintf a message ended by a newline at the writing end.

Related