-2

Question is simple

I've searched around but I couldn't find the solution

char *data1;
char *data2;
pid_t pid = fork();
int stat;

if (pid == 0){
    execlp("Program B");
} else {
    wait(&stat);
    if (WIFEXITED(stat))
        printf("%d\n", WEXITSTATUS(stat));
}

the thing is I need to send data1 and data2 to Program B as stdin

but I couldn't find the solution

how can I deal with this?

Haris
  • 12,120
  • 6
  • 43
  • 70
  • You may take the example in this manual page as a starting point: http://linux.die.net/man/2/pipe – Lee Duhem Mar 07 '14 at 08:57
  • I have de-ja-vu - been asked recently http://stackoverflow.com/questions/22244812/read-from-pipe-c/22244897#22244897 (or have I reached senility) – Ed Heal Mar 07 '14 at 09:21

3 Answers3

1
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    char *data1 = "First message.\n";
    char *data2 = "What the rest of the world has been waiting for.\n";
    pid_t pid;
    int p[2];
    if (pipe(p) < 0)
        perror("pipe() failed");
    else if ((pid = fork()) < 0)
        perror("fork() failed");
    else if (pid == 0)
    {
        dup2(p[0], STDIN_FILENO);
        close(p[0]);
        close(p[1]);
        execlp("cat", "cat", (char *)0);
        perror("execlp() failed");
    }
    else
    {
        close(p[0]);
        write(p[1], data1, strlen(data1));
        write(p[1], data2, strlen(data2));
        close(p[1]);
        int status;
        int corpse = wait(&status);
        if (WIFEXITED(status))
            printf("%d exited with status %d\n", corpse, WEXITSTATUS(status));
    }
    return 0;
}

Note how many closes are necessary.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

You can provide data as argument list to the new process.

Syntax:- int execlp(const char *path, const char *arg0, ..., NULL);

So your call can look something like this

// convert the input data into string format i.e data1 and data2 should be strings 
execlp("Program B","Program B",data1,data2,NULL); 

In program B use appropriately to convert it into whatever type you want to.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
KARTHIK BHAT
  • 1,410
  • 13
  • 23
0

Building a pipe to stdin is the way to go,

Like

char *data1;
char *data2;
int stat;
pid_t pid;

if( pipe(pfd) < 0 ) {
perror("pipe");
return 1;
}

pid = fork();

if (pid == 0)
{
    // Close the writing end of the pipe
    close(pfd[1]);
    execlp("Program B");
} 
else if(pid==-1) 
{
    perror("fork");
}
else    
{
    // Write to the pipe.
    if (write(pfd[1], "This is my data \n", 16) != 16)
        perror("write");

    close(pfd[1]);

    wait(&stat);
    if (WIFEXITED(stat))
    printf("%d\n", WEXITSTATUS(stat));
}
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • Nearly. In the child, you need to connect the read end of the pipe to the standard input (probably `dup2()` followed by another `close()`). The parent should close the read end of the pipe, though in this case it won't do any harm, I think. – Jonathan Leffler Mar 07 '14 at 14:59