I have this if block that is supposed to be creating a pipe then forking and I would like to combine the while loop below it with it. How would I do that?

p = pipe(pipe1);
if (p < 0) {
    printf("pipe error");
    exit(0);
}
else
{
    printf("successful pipe1 = %d\n",p);
}
p = pipe(pipe2);
if (p < 0) 
{
    printf("pipe error");
    exit(0);
}
else
{
    printf("successful pipe2 = %d\n",p);
}
p = pipe(pipe3);
if (p < 0) 
{
    printf("pipe error");
    exit(0);
}
else
{
    printf("successful pipe3 = %d\n",p);
}

//If the parent wants to receive data from the child, it should close fd1, and the child should close fd0.
//If the parent wants to send data to the child, it should close fd0, and the child should close fd1. 
//Since descriptors are shared between the parent and child, we should always be sure to close the end 
//of pipe we aren't concerned with.
//On a technical note, the EOF will never be returned if the unnecessary ends of the pipe are not explicitly closed.     
if ((child_pid1 = fork()) == 0)
{
    if (close(pipe1[1]) == -1)
    {
       printf("close error closing pipe1[1]\n");
    }
    if (close(pipe2[0]) == -1)
    {
       printf("close error closing pipe2[0]\n");
    }


    printf("In the server child: reading file = %s\n\n", argv[1]);
}





while (fscanf(init_dat_fp, "%s %d",  nam, &val) !=EOF)
{
    //sscanf(line1, "%s %s %x", label, mneumonic ,&start_address);
    init_name_table[counter] = malloc(strlen(nam)+1);
    strcpy(init_name_table[counter], nam);
    init_value_table[counter] = val;
    printf(" init_name_table[counter] is %s \n", init_name_table[counter]);
    printf(" init_value_table[counter] is %d \n", init_value_table[counter]);
    counter++;
    memset(nam, 0, 80);
    val = 0; 
}

I have trying to read this documentation but don't understand it.

http://tldp.org/LDP/lpg/node11.html

The while loop you have is the code executed after the if condition is evaluated. Are you saying that you want it to be executed when the if condition is true (or false)?

I'm unclear on what you mean by combining an if statement with a while loop.

willu pls explain

I would like the fork to execute, then I would like all the activities in the while loop to take place in that child process, Then I want to send that stuff to the parent process. I'm not sure how to combine all these activities.

In general, you fork a child process aftersetting up a pipe to be shared between the two. The fork call creates an exact duplicate process that continues (as a child) from where the parent process called fork. The return value from the fork call tells you which process you are currently running in. If you are in the child process, do the work you want to do and write the results to the parent process through the pipe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.