簡體   English   中英

系統函數不輸出任何東西

[英]System function doesn't output anything

我一直在修補管道和父子通信,直到現在我才使用system函數調用將某些東西從子進程傳遞給父進程。 問題是我相信system在我的例子中沒有任何輸出。

父級讀取輸入並將其發送給子級以檢查字符串是否為“已排序的字母”。 字符必須按字典順序排列。 我使用systemechogrep結合起來,並將結果發送回管道。 我能夠將其縮小到管道的父母讀取端; 它阻止。 我猜這是因為管道中沒有任何東西可供讀取,並且寫入端沒有關閉。 我知道這將是問題的原因,為此, system不必輸出任何內容。

  /*
$ ./grep
AeIoU
Yes.
blah
No.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>


#define     STRING_SIZE 128

static void     fatalError(char* message);          
int     checkIfAlpha(char   *string, int length);   //f-ja koja proverava da li se string sastroji samo od slova

int main(int argc, char const *argv[])
{

    pid_t   pid;
    char    input[STRING_SIZE];     //cuvamo input string ovde
    char    command[100 + STRING_SIZE];
    int     pipeToParent[2], pipeToChild[2];
    int     n;
    char    tmp[50];
    int     count;


    if (pipe(pipeToParent) < 0)
        fatalError("pipe error");

    if (pipe(pipeToChild) < 0)
        fatalError("pipe error");

    if ((pid = fork()) < 0)
        fatalError("fork error");
    else if (pid == 0) {
            //child
        if (close(pipeToChild[1]) < 0)
            fatalError("close error");

        if (close(pipeToParent[0]) < 0)
            fatalError("close error");

        if (dup2(pipeToParent[1], STDOUT_FILENO) < 0)
            fatalError("dup2 error");

        while (1) {

        if ((count = read(pipeToChild[0], tmp, 50)) < 0)
            fatalError("read error");
        fprintf(stderr, "2\n");
        if (count == 0)
            break;
        sprintf(command, "echo '%s' | grep -i ^a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*$", tmp);

        if (system(command) < 0) 
            fatalError("system error");

        fprintf(stderr, "3\n"); 

        }

        exit(EXIT_SUCCESS);


    }

    //parent

    if (close(pipeToParent[1]) < 0)
        fatalError("close rror");


    if (close(pipeToChild[0]) < 0)
        fatalError("close error");

    while(fgets(input, STRING_SIZE, stdin) != NULL) {

        input[strlen(input)-1] = '\0';


            if (checkIfAlpha(input, strlen(input)) == -1) {
                fprintf(stderr, "Samo slova\n");
                continue;
            }
            if (write(pipeToChild[1], input, strlen(input)) != strlen(input))
                fatalError("write error");
            printf("1\n");

            if ((n = read(pipeToParent[0], input, STRING_SIZE)) < 0)
                fatalError("fatal error");
            printf("4\n");

            if (n == 0)
                printf("No\n");
            else if (n > 0)
                printf("Yes\n");

    }

    exit(EXIT_SUCCESS);

}


static void     fatalError(char *message) {
    perror(message);
    exit(EXIT_FAILURE);
}

int     checkIfAlpha(char   *string , int   length) {

    int c = 1;

    for (int i = 0; i < length; i++)
        if (!isalpha(string[i])) {
            c = -1;
            break;
        }

    return c;
}

編輯:如果我認為它們會那么重要,我會發布輸出,但無論如何。

這是輸出(具有正確的輸入):

./grep 
abcde
1
2
3

這是 GDB:

Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe058) at grep.c:23
23  {
(gdb) next
34      if (pipe(pipeToParent) < 0)
(gdb) 
37      if (pipe(pipeToChild) < 0)
(gdb) 
40      if ((pid = fork()) < 0)
(gdb) 
42      else if (pid == 0) {
(gdb) 
76      if (close(pipeToParent[1]) < 0)
(gdb) 
80      if (close(pipeToChild[0]) < 0)
(gdb) 
83      while(fgets(input, STRING_SIZE, stdin) != NULL) {
(gdb) 

85          input[strlen(input)-1] = '\0';
(gdb) 
88              if (checkIfAlpha(input, strlen(input)) == -1) {
(gdb) 
92              if (write(pipeToChild[1], input, strlen(input)) != strlen(input))
(gdb) 
94              printf("1\n");
(gdb) 
1
96              if ((n = read(pipeToParent[0], input, STRING_SIZE)) < 0)
(gdb) 

read不寫入終止空字符。 if ((count = read(pipeToChild[0], tmp, 50)) < 0) ,程序將tmp傳遞給sprintf ,而不向其中寫入空字符。

這導致echo命令從tmp傳遞額外的垃圾數據,超出了從管道讀取的內容,進而導致grep找不到匹配的字符串。

一種解決方法是將read更改為read(pipeToChild[0], tmp, sizeof tmp - 1)並插入tmp[count] = 0; .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM