Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C linux管道和缓冲区,不确定如何使循环工作_C_Linux_Pipe_System Calls - Fatal编程技术网

C linux管道和缓冲区,不确定如何使循环工作

C linux管道和缓冲区,不确定如何使循环工作,c,linux,pipe,system-calls,C,Linux,Pipe,System Calls,我正在用C语言为一个Unix类编写一个DB程序。虽然我已经设法让所有的管道、缓冲区和命令都能正常工作,但While循环让我陷入了困境。程序将完全正确地执行第一个命令,接收第二个命令,然后立即退出。我已经加载了错误检查,没有抛出任何东西,所以机制是好的,但是在循环的某个地方有一个逻辑问题,我无法识别 以下是存储库: 运行示例: 1234567 102 08/11/18 4.00 1234567 101 08/14/18 14.00 3456787 9873 08/30/18 100.00 1234

我正在用C语言为一个Unix类编写一个DB程序。虽然我已经设法让所有的管道、缓冲区和命令都能正常工作,但While循环让我陷入了困境。程序将完全正确地执行第一个命令,接收第二个命令,然后立即退出。我已经加载了错误检查,没有抛出任何东西,所以机制是好的,但是在循环的某个地方有一个逻辑问题,我无法识别

以下是存储库:

运行示例:

1234567 102 08/11/18 4.00
1234567 101 08/14/18 14.00
3456787 9873 08/30/18 100.00
1234567 100 08/16/18 35.00
3456787 9874 09/30/18 4.00
12345 1010 09/01/18 34.00
1001001 905 08/14/18 9.00
1001001 903 08/30/18 11.00
12345 1001 09/14/18 16.00
12345 1111 08/24/18 2.00
12345 1112 08/31/18 44.00
1001001 902 09/25/18 19.00

Enter a command: add,1234567,999,01/01/01,99.99

Enter a command: list
do while在slave.c文件中给我带来麻烦:

// start the do-while loop for command stuff

// read in from the pipe
error_check = read(read_pipe, buffer, 1000);
if(error_check<0){
    perror("child process error, reading in from pipe");
    exit(-3);
}

// null terminate the end of buffer 
buffer[error_check] = '\0';

// here's where the command stuff starts 
char *first_command;
while(strcmp(first_command, "exit\n") != 0){
    // grab the first thing from the buffer, it'll be the command
    char *command = strtok(buffer, ",");
    first_command = command;
    printf("first command: %s\n", first_command);


    // now for the parameters
    int parameter_count = 0;
    char *parameters[4];

    while(command != NULL){
        command = strtok(NULL, ",");
        parameters[parameter_count] = command;
        parameter_count++;
    }

    // exit appropriately
    if(strcmp(first_command, "exit\n") == 0)
        return 9;

    // add a record
    else if(strcmp(first_command, "add") == 0){
        Record temp_record;
        temp_record.account_number = atoi(parameters[0]);
        temp_record.check_number = atoi(parameters[1]);
        temp_record.amount = atof(parameters[3]);
        strcmp(temp_record.transaction_date, parameters[2]);

        records[record_count] = temp_record;
        record_count++;

        error_check = write(write_pipe, "add completed", strlen(buffer));
        if(error_check<0){
            perror("error writing in add function");
            exit(-6);
        }
    }

    // delete a record
    else if(strcmp(first_command, "delete") == 0){
        for (int i = 0; i < record_count; i++){
            if(
            atoi(parameters[0]) == records[i].account_number && 
            atoi(parameters[1]) == records[i].check_number){

                records[i].account_number = 0;
                records[i].check_number = 0;
                records[i].amount = 0.0;
                strcpy(records[i].transaction_date, "\0");

            }
        }
    }

    // list all the records contained
    else if(strcmp(first_command, "list\n") == 0){

        // write all the records to the buffer 
        position = 0;
        for(int i = 0; i < record_count; i++){
            position += sprintf(buffer+position, "%d %d %s %.2f\n", 
            records[i].account_number,
            records[i].check_number,
            records[i].transaction_date,
            records[i].amount);
        }

        printf("%s\n", buffer);

        // write the buffer to the pipe
        error_check = write(write_pipe, buffer, strlen(buffer));

        // check for errors
        if(error_check<0){
            perror("child process write error");
            exit(-4);
        }

        // make sure the length of the buffer was proper
        if(error_check!=strlen(buffer)){
            printf("child process error, buffer was a weird size\n");
            exit(-5);
        }
    }

    else{
        printf("you didn't input a correct command\n");
    }


    // empty out everything for reuse
    strcpy(buffer, "\0");
    command = "\0";
    first_command = "\0";
    for(int i = 0; i < 4; i++)
        parameters[i] = "\0";

    // grab the new command 
    error_check = read(read_pipe, buffer, 1000);
    if(error_check<0){
        perror("child process error, reading in from pipe");
        exit(-5);
    }

    // null terminate the end of buffer 
    buffer[error_check] = '\0';

    printf("end of child do while buffer: %s\n", buffer);

}
//为命令启动do-while循环
//从管子里读入
错误检查=读取(读取管道,缓冲区,1000);

如果(error_check首先,我们可以注意到在第一个命令(add…)发送到子(slave)之后,子(slave)应该输出的内容没有出现在屏幕上

在第二个命令提示下,如果您停止程序并执行
ps
,您将注意到子级已死亡:

$ ps
  PID TTY          TIME CMD
27069 pts/29   00:00:00 master
27070 pts/29   00:00:00 slave <defunct>
(或者更好,使用
do…while
循环)


您的代码中还有其他一些小问题,有些问题已经在注释中报告给您,有些问题在使用
-Wall
编译时出现得很清楚(始终使用该标志并修复警告),有些问题正在等待您的睿智;-)

在此处发布代码,而不仅仅是在远程站点。我们不需要完整的代码,请将其简化为一个演示您遇到的问题的值。在进入
while
循环之前,您不需要设置
first\u命令。您应该将
\n
放在
strok()
的分隔符列表中,这样就不需要将其包含在
strcmp()中
调用。无需在
while
条件下测试
first\u命令,只需使用
while(1)
。当用户进入
退出时,您将返回
,因此,在这种情况下,您将永远无法返回测试。这不是问题。首先,它是不完整的;甚至没有一个
main
char *first_command = "";