[英]System function doesn't output anything
我一直在修補管道和父子通信,直到現在我才使用system
函數調用將某些東西從子進程傳遞給父進程。 問題是我相信system
在我的例子中沒有任何輸出。
父級讀取輸入並將其發送給子級以檢查字符串是否為“已排序的字母”。 字符必須按字典順序排列。 我使用system
將echo
和grep
結合起來,並將結果發送回管道。 我能夠將其縮小到管道的父母讀取端; 它阻止。 我猜這是因為管道中沒有任何東西可供讀取,並且寫入端沒有關閉。 我知道這將是問題的原因,為此, 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.