I've been trying to learn how to use pipes for IPC and, while I can get trivial examples to work , I am unable to get anything past those very simple programs to act correctly.
On the one hand, I think I might have some race condition issues - I planned to get semaphores working once the pipes were working - so if that's the problem, I'm happy to hear about it.
On the other hand, I just don't know where my code is falling over ...
I have 3 pieces to this puzzle:
The 3 processes are forking and execing fine, I just never get anything to read off the INNER pipe which causes message buffer to contain an empty string. Thus, DISPL never displays anything.
I expect DISPL to show each chunk of 9 chars and what was contained. There is no combining of the read buffer for a pretty print since I'd getting nothing at that point anyway.
My questions is, why are these pipes not transferring any data?
As always, all assistance is gladly accepted.
OUTER:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define READ 0
#define WRITE 1
#define READ_BLOCK_SIZE 9
#define PROCESS_COUNT 2
#define SLEEP_TIME 2
int pfdInnerPipe[2];
int pfdDisplPipe[2];
int main()
{
pid_t processID;
char readBuffer[READ_BLOCK_SIZE+1];
ssize_t bytesRead;
char pipeFdStr_inner[10];
char pipeFdStr_displ[10];
if (pipe(pfdInnerPipe) < 0 || pipe(pfdDisplPipe) < 0) exit(1);
sprintf(pipeFdStr_inner, "%d", pfdInnerPipe[WRITE]);
sprintf(pipeFdStr_displ, "%d", pfdDisplPipe[READ]);
for (int count = 0; count < PROCESS_COUNT; count++)
{
processID = fork();
switch (processID)
{
case 0:
if (count == 0) // spawn inner
{
// Inner will only write to pipe 1
close(pfdInnerPipe[READ]);
close(pfdDisplPipe[WRITE]);
close(pfdDisplPipe[READ]);
execl("./pipe_inner.exe", "pipe_inner.exe", pipeFdStr_inner, (char *)NULL);
exit(2);
} else if (count == 1) // spawn display
{
// Display will only read from display pipe
close(pfdDisplPipe[WRITE]);
close(pfdInnerPipe[WRITE]);
close(pfdInnerPipe[READ]);
execl("./pipe_displ.exe", "pipe_displ.exe", pipeFdStr_displ, (char *)NULL);
exit(2);
}
break;
case -1:
perror("fork failed");
exit(3);
break;
default :
continue;
}
}
// parent process
// parent will only read from INNER pipe and write to DISPL pipe
close(pfdDisplPipe[READ]);
close(pfdInnerPipe[WRITE]);
sleep(SLEEP_TIME); // allow time for something to be on the pipe
char messBuffer[] = "";
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", readBuffer);
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
}
close(pipeFdStr_inner[READ]);
write(pipeFdStr_displ[WRITE], messBuffer, strlen(messBuffer));
sleep(SLEEP_TIME); // keep the pipe open to read from
close(pipeFdStr_displ[WRITE]);
}
INNER:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define READ_BLOCK_SIZE 9
#define SLEEP_TIME 10
int main(int argc, char *argv[])
{
int writeFd;
char *strFromChild = "Message from INNER to OUTER";
if(argc != 2) {
exit(1);
}
writeFd = atoi(argv[1]);
write(writeFd, strFromChild, strlen(strFromChild));
sleep(SLEEP_TIME); // keep pipe open for a while
close(writeFd);
}
DISPL:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define READ_BLOCK_SIZE 9
#define SLEEP_TIME 5
int main(int argc, char *argv[])
{
int readFd;
char readBuffer[READ_BLOCK_SIZE+1];
ssize_t bytesRead;
if(argc != 2) {
exit(1);
}
readFd = atoi(argv[1]);
sleep(SLEEP_TIME); // allow time for everything else to happen
bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
while (bytesRead > 0) {
readBuffer[bytesRead] = '\0';
printf("Display: Read %li bytes - '%s'\n", bytesRead, readBuffer);
bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
}
printf("Display: Finished reading from pipe 2\n");
close(readFd);
}
It's kick yourself time… You have:
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", readBuffer);
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
}
close(pipeFdStr_inner[READ]);
write(pipeFdStr_displ[WRITE], messBuffer, strlen(messBuffer));
The 'file descriptors' you're reading from and writing to are the first character of the strings, which automatically convert to int
. You need to use:
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", readBuffer);
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
}
close(pfdInnerPipe[READ]);
write(pfdDisplPipe[WRITE], messBuffer, strlen(messBuffer));
The problem was spotted by error checking the system calls. The checked read()
reported:
outer: 2020-09-30 23:15:48.086 - pid=36674: failed to read from pipe
error (9) Bad file descriptor
It was not hard to take a good look at the file descriptor and see that it wasn't a file descriptor.
I used some code is available in my SOQ (Stack Overflow Questions) repository on GitHub as files stderr.c
and stderr.h
in the src/libsoq sub-directory. The reasonably fully checked version of outer.c
was like this, and I also error checked inner.c
and displ.c
similarly. Note that I changed the program names, removing the pipe_
prefix and .exe
suffix. Also, outer.c
waits for both its children to die before exiting itself.
#include "stderr.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#define READ 0
#define WRITE 1
#define READ_BLOCK_SIZE 9
#define PROCESS_COUNT 2
#define SLEEP_TIME 2
int pfdInnerPipe[2];
int pfdDisplPipe[2];
int main(int argc, char **argv)
{
if (argc > 0)
err_setarg0(argv[0]);
err_setlogopts(ERR_PID | ERR_MILLI);
pid_t processID;
char readBuffer[READ_BLOCK_SIZE + 1];
ssize_t bytesRead;
char pipeFdStr_inner[10];
char pipeFdStr_displ[10];
if (pipe(pfdInnerPipe) < 0 || pipe(pfdDisplPipe) < 0)
exit(1);
err_remark("inner (%d,%d), displ (%d,%d)\n",
pfdInnerPipe[READ], pfdInnerPipe[WRITE],
pfdDisplPipe[READ], pfdDisplPipe[WRITE]);
sprintf(pipeFdStr_inner, "%d", pfdInnerPipe[WRITE]);
sprintf(pipeFdStr_displ, "%d", pfdDisplPipe[READ]);
for (int count = 0; count < PROCESS_COUNT; count++)
{
processID = fork();
switch (processID)
{
case 0:
if (count == 0) // spawn inner
{
// Inner will only write to pipe 1
close(pfdInnerPipe[READ]);
close(pfdDisplPipe[WRITE]);
close(pfdDisplPipe[READ]);
// execl("./pipe_inner.exe", "pipe_inner.exe", pipeFdStr_inner, (char *)NULL);
execl("./inner", "inner", pipeFdStr_inner, (char *)NULL);
err_syserr("failed to execute ./inner");
exit(2);
}
else if (count == 1) // spawn display
{
// Display will only read from display pipe
close(pfdDisplPipe[WRITE]);
close(pfdInnerPipe[WRITE]);
close(pfdInnerPipe[READ]);
// execl("./pipe_displ.exe", "pipe_displ.exe", pipeFdStr_displ, (char *)NULL);
execl("./displ", "displ", pipeFdStr_displ, (char *)NULL);
err_syserr("failed to execute ./displ");
exit(2);
}
break;
case -1:
perror("fork failed");
exit(3);
break;
default:
err_remark("forked %d\n", processID);
continue;
}
}
// parent process
// parent will only read from INNER pipe and write to DISPL pipe
close(pfdDisplPipe[READ]);
close(pfdInnerPipe[WRITE]);
sleep(SLEEP_TIME); // allow time for something to be on the pipe
char messBuffer[] = "";
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
if (bytesRead < 0)
err_syserr("failed to read from pipe\n");
err_remark("read %zd bytes [[%.*s]]\n", bytesRead, (int)bytesRead, readBuffer);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", readBuffer);
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
if (bytesRead < 0)
err_syserr("failed to read from pipe\n");
}
close(pfdInnerPipe[READ]);
write(pfdDisplPipe[WRITE], messBuffer, strlen(messBuffer));
sleep(SLEEP_TIME); // keep the pipe open to read from
close(pfdDisplPipe[WRITE]);
int status;
int corpse;
while ((corpse = wait(&status)) > 0)
err_remark("child %d exited with status 0x%.4X\n", corpse, status);
err_remark("exits\n");
return 0;
}
Sample output:
outer: 2020-09-30 23:26:24.222 - pid=36852: inner (3,4), displ (5,6)
outer: 2020-09-30 23:26:24.224 - pid=36852: forked 36853
outer: 2020-09-30 23:26:24.224 - pid=36852: forked 36854
inner: 2020-09-30 23:26:24.437 - pid=36853: fd = 4
displ: 2020-09-30 23:26:24.590 - pid=36854: fd = 5
outer: 2020-09-30 23:26:26.226 - pid=36852: read 9 bytes [[Message f]]
Outer: Read 9 bytes
Outer: Message Buffer: Message f
Outer: Read 9 bytes
Outer: Message Buffer: rom INNER
Outer: Read 9 bytes
Outer: Message Buffer: to OUTER
inner: 2020-09-30 23:26:34.441 - pid=36853: exiting
outer: 2020-09-30 23:26:36.441 - pid=36852: child 36853 exited with status 0x0000
Display: Finished reading from pipe 2
displ: 2020-09-30 23:26:36.441 - pid=36854: exiting
outer: 2020-09-30 23:26:36.442 - pid=36852: child 36854 exited with status 0x0000
outer: 2020-09-30 23:26:36.442 - pid=36852: exits
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.