Reputation:
I am writing a C program on working of transport layer multiplexing using OS tools. There is 3 stream of bits, in file1
, file2
and file3
respectively. I read 10 bits from file1
, file2
, file3
in sequence and combine them into a frame and store it in file multiplex
.
Given, the only means of communication is pipe created by you.
Please Note: I didn't use fwrite
function and wanted to explore more with pipes.
The corresponding code is
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int fd[3][2], pid,i, ch_count=10, count, status,f;
FILE *fp, *fp1;
fp1=fopen("multiplex.txt", "w");
char ch, inp[10];
for(i=0;i<3;i++)
pipe(fd[i]);
for(i=0;i<3;i++)
{
pid=fork();
if(pid==0)
{
printf("Inside Child process \n");
f=fileno(fp1);
close(fd[i][0]);
dup2(fd[i][1],f);
if(i==0)
fp=fopen("file1.txt","r");
else if(i==2)
fp=fopen("file2.txt","r");
else
fp=fopen("file3.txt", "r");
while(1)
{
count=0;
while(( ch = fgetc(fp) ) != EOF && count<=ch_count)
inp[count++]=ch;
if(ch==EOF)
exit(0);
printf("The string read is %s \n", inp);
write(fd[i][1], inp, 10);
sleep(3-i);
}
close(fd[i][1]);
}
else
printf("Parent process \n");
}
return 0;
}
The problem is:
i) I can't write anything in multiplex
file.
ii) I read alien characters when I print the inp
string. Moreover the strings read aren't in sequence(i.e, file1
first, file2
second then file3
) . The output is:
The string read is s not goldAR^�q�@� //from file 2
�7�U
The string read is l that glitR^�q�@� //from file 2 again
�7�U
The string read is s over the R^�q�@� //from file 1
�7�U
The string read is well that eR^�q�@� //from file 3
�7�U
The string read is ers is not R^�q�@� //from file 2
�7�U
The string read is oldAll thatR^�q�@�
�7�U
The string read is ds well.AllR^�q�@�
�7�U
The string read is glitters isR^�q�@�
�7�U
The string read is azy dogThe R^�q�@�
�7�U
The string read is not goldAllR^�q�@�
What is the problem?
Edit: The contents of file1
The_quick_brown_fox_jumps_over_a_lazy_dog_The_quick_brown_fox_jumps_over_a_lazy_dog_`
The contents of file2
The_quick_brown_fox_jumps_over_a_lazy_dog_The_quick_brown_fox_jumps_over_a_lazy_dog_
The contents of file3
All's_well_that_ends_well_All's_well_that_ends_well_All's_well_that_ends_well_
Upvotes: 0
Views: 187
Reputation: 6994
i) I can't write anything in multiplex file.
by
fp1=fopen("multiplex.txt", "w");
/* ... */
f=fileno(fp1);
dup2(fd[i][1],f);
you disassociate fp1
from "multiplex.txt" (within the children). Using fp1
within the children would be risky/undefined because FILE
is usually buffered and changes in buffer position can not be shared between processes.
You will have to redesign your program (write into pipe in the children, read within the parent) to implement the wanted functionality.
ii) I read alien characters when I print the inp string.
while(( ch = fgetc(fp) ) != EOF && count<=ch_count)
inp[count++]=ch;
/* ... */
printf("The string read is %s \n", inp);
does not zero-terminate it. Either do a inp[count] = '\0'
before the printf, or use
printf("The string read is %.*s \n", (int)count, inp);
Upvotes: 2