Reputation: 115
I am learning about fork() and pipes by writing a simple program. I have it returning results successfully but one of the results repeats itself. I have been experimenting with the code to figure it out but can't manage to get it right.
The sleep times are just there to simulate system adjustment. I have been trying to get the navigation sleep time to be random number of seconds between 0-6 inclusive but I can't manage to get it to do that and print out the random number generated to the report. That is an issue I am willing to live with but would like if it worked.
Current code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>
#include <time.h>
#define MAX 150
#define PipeStdIn 0 //UNIX StdIn
#define PipeStdOut 1 //UNIX StdOut
void lifeSupport();
void navigation();
int main()
{
const char *message = {"Calibrate Systems\n"};
int pipes[2], ret;
char buf[MAX + 1];
if(pipe(pipes) == 0)
{
if(fork() == 0)
{
ret = read(pipes[PipeStdIn], buf, MAX);
printf("Life Support receives instruction: %s\n", buf);
sleep(5);
sleep(4);
const char *breathGL = {"Breathing gas levels have been adjusted\nAdjustment time: 5 seconds\nLighting and temperature levels have been adjusted\nAdjustment time: 4 seconds\n"};
ret = write(pipes[PipeStdOut], breathGL, strlen(breathGL) + 1);
}
else
{
ret = write(pipes[PipeStdOut], message, strlen(message) + 1); //write
ret = wait(NULL);
ret = read(pipes[PipeStdIn], buf, MAX);
time_t now;
time(&now);
printf("Report received: %s\n", buf);
printf("Report time: %s\n", ctime(&now));
}
}
if(pipe(pipes) == 0)
{
if(fork() == 0)
{
ret = read(pipes[PipeStdIn], buf, MAX);
printf("Navigation receives instruction: %s\n", buf);
sleep(3);
const char *nav = {"Navigation system has been adjusted\nAdjustment time: 3 seconds\n"};
ret = write(pipes[PipeStdOut], nav, strlen(nav) + 1);
}
else
{
ret = write(pipes[PipeStdOut], message, strlen(message) + 1); //write
ret = wait(NULL);
ret = read(pipes[PipeStdIn], buf, MAX);
time_t now;
time(&now);
printf("Report received: %s\n", buf);
printf("Report time: %s\n", ctime(&now));
}
}
close(pipes[PipeStdIn]);
close(pipes[PipeStdOut]);
return 0;
}
Expected results:
Life Support receives instruction: Calibrate Systems
Report received: Breathing gas levels have been adjusted
Adjustment time: 5 seconds
Lighting and temperature levels have been adjusted
Adjustment time: 4 seconds
Report time: Tue Apr 28 08:48:49 2020
Navigation receives instruction: Calibrate Systems
Report received: Navigation system has been adjusted
Adjustment time: 3 seconds
Report time: Tue Apr 28 08:48:52 2020
Current results:
Life Support receives instruction: Calibrate Systems
Navigation receives instruction: Calibrate Systems
Report received: Navigation system has been adjusted
Adjustment time: 3 seconds
Report time: Tue Apr 28 08:48:49 2020
Report received: Breathing gas levels have been adjusted
Adjustment time: 5 seconds
Lighting and temperture levels have been adjusted
Adjustment time: 4 seconds
Report time: Tue Apr 28 08:48:49 2020
Navigation receives instruction: Calibrate Systems
Report received: Navigation system has been adjusted
Adjustment time: 3 seconds
Report time: Tue Apr 28 08:48:52 2020
Upvotes: 1
Views: 216
Reputation: 84521
If the explanation wasn't clear from the comment, you need to move the complete second if(fork() == 0) {...} else {...}
inside the first else
to prevent both parent and child from executing that block. After you move the if(fork() == 0) {...} else {...}
up, your logic would look like:
if(pipe(pipes) == 0)
{
if(fork() == 0)
{
ret = read(pipes[PipeStdIn], buf, MAX);
...
ret = write(pipes[PipeStdOut], breathGL, strlen(breathGL) + 1);
}
else
{
ret = write(pipes[PipeStdOut], message, strlen(message) + 1);
...
if(fork() == 0)
{
ret = read(pipes[PipeStdIn], buf, MAX);
...
const char *nav = "Navigation system has been adjusted\n"
"Adjustment time: 3 seconds\n";
ret = write(pipes[PipeStdOut], nav, strlen(nav) + 1);
}
else
{
ret = write(pipes[PipeStdOut], message, strlen(message) + 1);
...
// time_t now;
time(&now);
printf("Report received: %s\n", buf);
printf("Report time: %s\n", ctime(&now));
}
}
}
close(pipes[PipeStdIn]);
close(pipes[PipeStdOut]);
Also note, the C compile will concatenate adjacent strings into a single during compile allowing you to break up long lines into a series of double-quoted strings that will be joined at compile time, e.g.
const char *breathGL = "Breathing gas levels have been adjusted\n"
"Adjustment time: 5 seconds\n"
"Lighting and temperature levels have been adjusted\n"
"Adjustment time: 4 seconds\n";
and
const char *nav = "Navigation system has been adjusted\n"
"Adjustment time: 3 seconds\n";
(also note enclosing {...}
are not required)
Look things over and let me know if you have further questions.
Upvotes: 1