I have a logging process and 2 childen. The two children are suppose to use 1 pipe to send messages back to the parent. The problem I am having is that I am apparently sending the info at the same time. Is there a command like wait(&status) that I have to use in B before I write to the pipe again.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
void cat(char times[]);
int main(void)
{
int randomNumber;
int child4[2];
int child3[2];
int tochild[2];
int child2[2];
char buffer[300];
char buffer2[300];
char buffer3[300];
char buffer4[300];
int status;
int pid;
int pid2;
buffer[0]='\0';
buffer2[0]='\0';
buffer3[0]='\0';
buffer4[0]='\0';
pipe(tochild);
pipe(child2);
char *p=buffer;
char *r=buffer3;
if((pid=fork())==0)
{
int w=0;
while(w<10)
{
char astring[3];
char message[20];
randomNumber=rand() %2;
if(randomNumber==0)
{
astring[0]='D';
astring[1]=w+48;
astring[2]='\0';
}
if(randomNumber==1)
{
astring[0]='c';
astring[1]=w+48;
astring[2]='\0';
}
strcat(buffer, astring);
strcpy(message, "A sends B ");
strcat(message, astring);
strcat(buffer3, message);
w++;
}
close(child2[0]);
while(*p)
{
//if(rand() &1) sleep(1);
write(tochild[1], p++, 1);
}
close(tochild[1]);
while(*r)
{
write(child2[1],r++,1);
}
close(child2[1]);
printf("returned to main");
_exit(1);
}
int V=0;
int pd=0;
while(pd<10)
{
if((V=read(child2[0], buffer4, 13))==-1)
{
perror("child failed");
_exit(1);
}
close(child2[0]);
buffer4[13]='\0';
char mytime[27];
cat(mytime);
strcat(buffer4, " ");
strcat(buffer4, mytime);
pd++;
printf("%s\n", buffer4);
}
//printf("%s\n", buffer4);
sleep(15);
int i;
int p3[2];
int p4[2];
if((pid2=fork())==0) //code for process B
{
int V;
char Bstring[3];
Bstring[0]='\0';
pipe(p3);
close(p3[0]);
close(p3[0]);
pipe(p4);
close(p4[0]);
char bstring[50];
char cstring[50];
char log[160];
bstring[0]='\0';
cstring[0]='\0';
log[0]='\0';
int atempvar=0;
while(atempvar<10)
{
if((V=read(tochild[0], Bstring, 2))==-1)
{
printf("yuck");
_exit(1);
}
Bstring[2]='\0';
printf("%s\n", Bstring);
if(Bstring[0]=='c')
{
strcat(bstring, Bstring);
char message[20];
strcpy(message, "B SENDS C ");
strcat(message, Bstring);
printf("%s\n", message); //remove
}
if(Bstring[0]=='D')
{
strcat(cstring, Bstring);
char message[20];
strcpy(message, "B sends D ");
strcat(message, Bstring);
printf("%s\n", message); //remove
}
atempvar++;
}
char* bpointer= bstring;
while(*bpointer)
{
sleep(1);
write(p3[1], bpointer++, 1);
}
close(p3[1]);
char* cpointer=cstring;
while(*cpointer)
{
sleep(1);
write(p4[1], cpointer++, 1);
}
close(p4[1]);
close(child2[0]);
char* logpointer = log;
sleep(10);
while(*logpointer)
{
sleep(1);
write(child2[1], logpointer++, 1);
}
close(child2[1]);
};
//logging process
int ph=0;
char logstream[150];
logstream[0]='\0';
if((ph=read(child2[0], logstream, 150))==-1)
{
printf("error reading input from B");
return(1);
}
// printf("%s\n", logstream);
return 0;
}
void cat(char times[])
{
char *timetoday;
time_t rawtime;
time(&rawtime);
//printf("current data and time are %s\n", ctime(&rawtime));
strcpy(times, ctime(&rawtime));
return;
}