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

时间:2018-10-05 18:41:30

标签: c linux pipe system-calls

我正在用C编写一个我要学习的Unix类的数据库程序。虽然我设法使所有管道,缓冲区和命令都能正常工作,但是while循环使我陷入了循环。该程序将完全执行第一个命令,接受第二个命令,但随后立即退出。我已经为它加载了错误检查功能,并且没有抛出任何异常,因此机制还可以,但是在循环中的某个地方存在我无法识别的逻辑问题。

这是存储库:https://gitlab.com/IrateSwami/c_pipes/tree/dev

示例运行:

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

这样做会给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);

}

1 个答案:

答案 0 :(得分:1)

首先,我们可以注意到在向其发送第一个命令(添加...)之后,子(从属)应该输出的内容并未出现在屏幕上。

在第二次提示输入命令时,如果停止程序并执行ps,则会注意到孩子已死:

$ ps
  PID TTY          TIME CMD
27069 pts/29   00:00:00 master
27070 pts/29   00:00:00 slave <defunct>

从服务器中的一个主要问题是您正在将未初始化的first_command与“退出”进行比较。该比较要求访问地址first_command上的字节。如果该地址为NULL,或更普遍地说是在程序空间之外,则会出现分段错误,从而杀死该子进程。

这是怎么被父进程忽略的?因为当您从管道read时,您仅检查错误,而不检查EOF(0),这意味着管道为空并关闭(IOW,子项已死)。

解决方法:初始化变量

char *first_command = "";

(或者更好的是,使用do...while循环)

您的代码中还有其他一些小问题,有些已经在注释中报告给您,有些在使用-Wall进行编译时会清楚地显示(始终使用该标志并修复警告),还有一些等待您的明智;-)