While i am reading in Server add result , i am getting garbage value.
Please let me know where i am doing wrong.
<code>#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
struct Operation
{
int num1;
int num2;
char operator;
int result;
};
int main()
{
pid_t ret,add_pid,Client_pid;
pid_t child1,child2,child3,child4;
int pipefd_Client[2] , pipefdadd[2];
int temp;
char *message,run_time;
char *file_message = "In process one\r\n";
char sfdread[4],sfdwrite[4],tempBuf[4];
int i=0,exit_code = 0,status,exec_error ;
struct Operation objptr,readptr;// = {0,0,'0',0};
printf("Service Handler Start \n");
printf("Switch \n");
int fd = pipe(pipefdadd);
if(fd == -1)
{
perror("Pipe open error");
exit(1);
}
fd = pipe(pipefd_Client);
if(fd == -1)
{
perror("Pipe open error");
exit(1);
}
Client_pid = fork();
switch(Client_pid)
{
case -1:
perror("Fork Failed");
break;
case 0:
sprintf(sfdread,"%d",pipefd_Client[0]);
sprintf(sfdwrite,"%d",pipefd_Client[1]);
exec_error = execl("./client.o","client.o",sfdread,sfdwrite,0);
if(exec_error == -1)
{
perror("Execl failed");
}
break;
default:
close(pipefd_Client[1]);
fcntl(pipefd_Client[0],F_SETFL,O_NONBLOCK);
printf("IN Service Handler reading from client pipe\n");
sleep(4);
read(pipefd_Client[0],&objptr,sizeof(objptr));
if(objptr.operator == '+')
{
write(STDOUT_FILENO,"Done",5);
printf("Addition %d \n",objptr.num1+objptr.num2 );
}
//write(pipefd[1],"Lol is everywhere\r\n",strlen(file_message));
close(pipefd_Client[0]);
//wait(Client_pid);
//waitpid(Client_pid, &status, WNOHANG);
break;
}
add_pid = fork();
switch(add_pid)
{
case -1:
perror("Fork Failed");
break;
case 0:
sprintf(sfdread,"%d",pipefdadd[0]);
sprintf(sfdwrite,"%d",pipefdadd[1]);
exec_error = execl("./add.o","add.o",sfdread,sfdwrite,0);
if(exec_error == -1)
{
perror("Execl failed");
}
break;
default:
close(pipefdadd[0]);
write(pipefdadd[1],&objptr,sizeof(struct Operation));
//write(pipefd[1],"Lol is everywhere\r\n",strlen(file_message));
close(pipefdadd[1]);
// objptr.num1 = objptr.num1 + objptr.num2 ;
// printf("Sum is %d\n",objptr.num1 );
//sleep(4);
waitpid(add_pid, &status, 0);
//sleep(14);
//close(pipefdadd[1]);
//fcntl(pipefdadd[0],F_SETFL,O_NONBLOCK);
read(pipefdadd[0],&readptr,sizeof(struct Operation));
//write(pipefd[1],"Lol is everywhere\r\n",strlen(file_message));
close(pipefdadd[0]);
printf("Service Handler Sum is %d %d\n",readptr.result,readptr.num1);
close(pipefd_Client[0]);
printf("Writing Result to client pipe\n");
//sleep(4);
write(pipefd_Client[1],&temp,sizeof(int));
//write(pipefd[1],"Lol is everywhere\r\n",strlen(file_message));
close(pipefd_Client[1]);
//wait(NULL);
//waitpid(Client_pid, &status, WNOHANG);
break;
}
exit (0);
}
</code>
It looks like you're new here. If you want to get involved, click one of these buttons!