0

We were tasked to create a two-way communication simulation in one c code. It's my first time dabbling with this kind of code so I have created this simple code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include<wait.h>
int main(void)
{
    pid_t pid;
    char buf[1024];
    char cp[50];
    char ex[100]="exit";
    int readpipe[2];
    int writepipe[2];
    long int a;
    int b;
    a=pipe(readpipe);
    b=pipe(writepipe);
    int test=1;
    int length;

    if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
    if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); } 
    fflush(stdin);
    pid=fork();
    if(pid==-1)
        {
            printf("pid:main");
            exit(1);
        }
    while(test==1)
        {
            if(pid==0)
                { 
                    close(readpipe[1]);
                    close(writepipe[0]);
                    if(read(readpipe[0],buf,sizeof(buf)) < 0)
                        {
                            exit(1);
                        }
                    printf("\nSEND TO USER 1:");
                    fflush(stdin);
                    fgets(cp, 50, stdin);
                    length = strlen(cp);
                    if(cp[length-1] == '\n') {
                        --length;
                        cp[length] = '\0';
                    }   
                    if(strcmp(cp,ex)==0) {
                        test=0;
                        break;
                    }
                    if(write(writepipe[1],cp,strlen(cp)+1) < 0)
                        {
                            exit(1);
                        }
                }
            else
                {
                    close(readpipe[0]);
                    close(writepipe[1]);
                    printf("\nSEND TO USER 2:");
                    fflush(stdin);
                    fgets(cp, 50, stdin);
                    length = strlen(cp);
                    if(cp[length-1] == '\n') {
                        --length;
                        cp[length] = '\0';
                    }   
                    if(strcmp(cp,ex)==0) {
                        test=0;
                        break;
                    }
                    if(write(readpipe[1],cp,strlen(cp)+1) < 0)
                        {
                            exit(1);
                        }        

                    if(read(writepipe[0],buf,sizeof(buf)) < 0)
                        {
                            exit(1);
                        }        
                }
        }
    close(readpipe[1]);
    close(writepipe[0]);
    close(readpipe[0]);
    close(writepipe[1]);
    return 0;
}

The program terminates when USER 1 or USER 2 inputs exit. However....

The error is that whenever I press exit, it will print the "SEND TO USER x" first then proceeds to exit. How can I fix this? Any help? Thanks.

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

When you type exit into the sending process, it breaks out of the loop, closes its end of the pipes, and exits.

When the other process tries to read from the pipe, it will get an EOF, which is indicated by read() returning 0. But your code never checks for this, it only exits when read() or write() returns a negative value to indicate an error (EOF is not an error). So it goes back to the top of the loop and asks for input to send to the other process. When it tries to write to the closed pipe it will get an EPIPE error or a SIGPIPE signal, and it will then exit.

Change your reading code to:

int n = read(readpipe[0],buf,sizeof(buf));
if (n == 0) {
    break;
} else if (n < 1) {
    exit(1);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Do I need to change both reading code to both pipes? Because I have tried it and after inputting to "SEND TO USER 2" the program prints SEND TO USER 1 and then terminates. I have tried changing just the reading code for SEND TO USER 2 first, and it works, but only if you type exit to SEND TO USER 2. Thanks for the help by the way. – Alotta Warmheart Nov 11 '16 at 01:07
  • Yes, you need to change both, since you can type `exit` to either process and then the other process is supposed to exit as well. – Barmar Nov 11 '16 at 01:09
  • It terminated immediately after typing something to SEND TO USER 2. Any idea why this is happening? – Alotta Warmheart Nov 11 '16 at 01:19
  • No idea. Run the program under a debugger to see what's happening. – Barmar Nov 11 '16 at 01:22
  • I fixed it :D I found out that something's wrong with my readpipes and writepipes. Thanks! – Alotta Warmheart Nov 11 '16 at 01:33