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.