سؤال

I have a very simple doubt. I am trying two processes to comm using named pipes. It's working fine but as I press n for ans, it's not executing return of main. but printing "RECEIVED From the CHILD : " which is in the PARENT PROCESS.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include<string.h>
#include<ctype.h>
#include<wait.h>
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE    255
#include <sys/types.h>

int main()
{
    int rdfd, wrfd, ret_val, count, numread;
    char buf[MAX_BUF_SIZE];
    char ans;
    char cp[50];
    pid_t pid;

    ret_val = mkfifo(NP1,0666);
    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (1);
    }
    ret_val = mkfifo(NP2, 0666);
    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (1);
    }
    printf("\nSEND SOMETHING TO PARENT PROCESS\t");
    scanf("%[^\n]%*c",&ans);

    pid=fork();
    if (pid==-1) {
        printf("\nERROR IN PID");
        exit(1);
    }
    while (ans=='y' || ans=='Y') {
        if (pid!=0) {
            //PARENT PROCESS
            /* Open the first named pipe for reading */
            rdfd = open(NP1, O_RDONLY);
            numread = read(rdfd, buf, MAX_BUF_SIZE);
            buf[numread] = '\0';
            printf("RECEIVED From the CHILD : %s\n", buf);

            /* Convert to the string to upper case */
            count = 0;
            while (count < numread) {
                buf[count] = toupper(buf[count]);
                count++;
            }

            wrfd = open(NP2, O_WRONLY);

            /* Write the converted string back to the second pipe */
            write(wrfd, buf, strlen(buf));
        } else {
            //CHILD PROCESS
            printf("\nEnter data to be sent to PARENT\t");
            fgets(cp, 50, stdin);
            wrfd = open(NP1, O_WRONLY);
            /* Write to the pipe */
            write(wrfd, cp, strlen(cp)+1);
            /* Open the second named pipe for reading */
            rdfd = open(NP2, O_RDONLY);
            /* Read from the pipe */
            numread = read(rdfd, buf, MAX_BUF_SIZE);
            buf[numread] = '\0';
            printf("RECEIVED From the PARENT : %s\n", buf);
            printf("\nSEND SOMETHING TO PARENT PROCESS\t");
            fflush(stdin);
            scanf(" %[^\n]%*c",&ans);
        }
    }

    return 0;
}
هل كانت مفيدة؟

المحلول

The problem with the code is when u give n as input to the child, the parent is still alive so in order to kill the parent use return from the read in parent to see if the child has exited or not and then if the child is already exit, then exit the parent too. this can be done by editing your code with
if( (numread = read(rdfd, buf, MAX_BUF_SIZE)) == 0) return 0;

actually once you give n as input in the child, the child exits and then old value of ans which is y is compared in parent process and so it doesn't exit....

check n reply... thanks...

نصائح أخرى

scanf(" %[^\n]%*c",&ans);

why you are using so much formatting may be this is the problem, to avoid new line problem we just put data

scanf(" %c",&ans);

another reason it is two different process and another one is waiting for read and you are trying to writing or opposite may be again problem. Some string and char reading creates problem I face this one and to avoid this I use different input functions e.g. for string puts and gets etc.

I think while(ans=='y' || ans=='Y') you code, So in that loop following code is worthless, just return form main.

 if(ans=='n')
     return(0); //NOT WORKING

UPDATE

Your problem looks for sharing variable between child and parents

Each process has its own memory space. A process can't normally access another process's memory.

In the case of fork, the child process's memory space starts as an exact copy of the parents. That includes variables, code, etc. Changing any of these in one will not change any similar variable in the othe

So here ans variable you modified in child which never affect in parent process. So can make some kill routine to terminate when n you press. Add following code in your child block after scanf function

        if(ans=='n' || ans == 'N')
        {
           printf("going to exit communication:\n");
           kill(pid, SIGTERM);  
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top