简体   繁体   中英

C program having issues inside "fork()" and pipes

Hi I am a very beginner in parent and child communication in C. I am trying to build a calculator with C programming that would just read the data from the file compute some calculations using fork() and pipe and then determine the output. I was able to read the operators from the file and also the numbers from the file but I am having trouble in in using dup inside the if(fork() == 0) condition. Everything which is above close(1); is working but everything after that is not working. I am using input stream 0 to read the first argument, use stream 3 to read the 2nd argument and use standard output stream 1 for output. I know after that I have to just setup conditions for addition, subtraction, multiplication and division and then use execl to compute the calculations after I figure that out but I am not able to figure out why I am having that problem any help will be appreciated thanks.. Here is the code and input file below, In order the program. we need to pass the file name as the argument. For example ./calc data.txt

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

#define MAXLEN 1000

int fds[100][2];
char operators[100];

int main(int argc, char *argv[]) {
    char line[MAXLEN], *temp;

    FILE *dataFile = fopen(argv[1], "r");
    //read the first line - it contains the configuration
    fgets(line, MAXLEN, dataFile);

    // sample content for line: a + b - c
    strtok(line, " \n"); //skip the symbol representing variable/parameter
    int count=0;
    while (temp = strtok(NULL, " \n")) {
            operators[count] = temp[0];
            printf("operator: %c\n", operators[count]);
            count++;
            strtok(NULL, " \n"); //skip the symbol representing variable/parameter
    }
    int x[count+1];
    int num = 0;
    while (fscanf(dataFile, "%d", &x[num]) > 0){
            printf("%d\n", x[num]);
            num++;
            }
    for(int h=0; h<count*2+1; h++){
            pipe(fds[h]);
            printf("%d \n",h);
    }
    printf("Count: %d\n", count);
    for(int i = 0; i < count; i++){
            //printf("%c\n", operators[i]);
            if (fork() == 0){
                    //printf("Value of operators: %c\n",operators[i]);
                    close(0);
                    dup(fds[2*i][0]);
                    close(3);
                    dup(fds[2*i+1][0]);
                    //printf("After close(3)\n");
                    if(operators[i] == '+')
                            printf("Add \n");
                    close(1);
                    printf("After close(0) \n");
                    dup(fds[2*i+2][1]);
                    printf("Value of operators: %c\n", operators[i]);
            if(operators[i] == '+')
                            printf("Add %n");
            }`
          }
   }`

File descriptor 1 refers to stdout . If you close that, your call to printf will fail because it attempts to print to a closed stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM