2012-04-22 2 views
0

이것은 파이프와 프로세스에 관한 작은 C 프로그램입니다. 아버지 프로세스는 2 개의 하위 프로세스를 생성하고 첫 번째 프로세스는 체인에서 숫자를 읽고 두 번째 프로세스는 문자를 읽습니다. 나는 단어를 묻는 것으로 시작했다. 나는 보호를 추가하지 않았다. 이것은 단지 시험 일 뿐이므로 약 20자를 말하면, 아버지 과정은 첫 번째 파이프에 숫자를 쓰고 두 번째 파이프에 글자를 쓰고, fork()를 사용하는 자식이 자식이라면 첫 번째 파이프에서 숫자를 읽습니다. 아버지 인 경우 다른 자식을 만들어 문자를 읽습니다. 컴파일 후WRITE 및 READ의 실수는 무엇입니까?

# include <stdio.h> 
# include <unistd.h> 
# include <string.h> 
# include <fcntl.h> 

main() 
{ 
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n"); 
    char * word; 
    printf("please type the word: \n"); 
    scanf("%s",word); 
    printf("Now 2 pipes will be created\n"); 
    int fd1[2]; 
    int fd2[2]; 
    pipe(fd1); pipe(fd2); 
    printf("Now the father will write numbers in the first pipe, and letters in the second\n"); 
    int i; 
    char numbers[20]; int j=0; 
    char caracters[20]; int k=0; 
    for (i=0;i<20;i++) 
    { 
     if(word[i]>='0' && word[i]<='9') //if number 
     { 
      close(fd1[0]); //closing reading 
      write(fd1[1],word[i],2); 

     } 
     else 
     { 
      close(fd2[0]); 
      write(fd2[1],word[i],2); 
     } 

    } 
    printf("The father has wrote in the 2 pipes, now its time for the sons\n"); 
    int f=fork(); 
    if(f==0) //first son 
    { 
     for(i=0;i<20;i++) {   
      close(fd1[1]); //closing writing 
      read(fd1[0],numbers[j],strlen(numbers[j])+1); 
      j++; 

     } 
     printf("first son read everything, he got %d Numbers\n", j); 
    } 
    else 
    { 
     f=fork(); 
     if(f==0) 
     { 
      for(i=0;i<20;i++) {   
      close(fd2[1]); //closing writing 
      read(fd2[0],caracters[k],strlen(caracters[k])+1); 
      k++; 

     } 
     printf("second son read everything, he got %d caracters\n", j); 
    } 
} 

는 :

In function 'main': 
Line 25: warning: passing argument 2 of 'write' makes pointer from integer without a cast 
Line 31: warning: passing argument 2 of 'write' makes pointer from integer without a cast 
Line 41: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast 
Line 41: warning: passing argument 2 of 'read' makes pointer from integer without a cast 
Line 54: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast 
Line 54: warning: passing argument 2 of 'read' makes pointer from integer without a cast 
Line 60: error: expected declaration or statement at end of input 

답변

2

writeread의 프로토 타입은 포인터해야 write/read

ssize_t write(int fd, const void *buf, size_t count); 

ssize_t read(int fd, void *buf, size_t count); 

인수이 있습니다. 그러나 당신이 보내는 심지어 대신 포인터의 배열로서 또한 당신의 strlen

선언 word으로 (사실 정수) 문자 word[i]numbers[i]

같은 문제가 있습니다. 그렇지 않으면 포인터가 가리키는 임의의 위치에 글을 쓸 수 있습니다. 또는 포인터로 유지하려는 경우 malloc 일부 메모리가 필요합니다.

편집을 불평하는

이 모든 후, 단지 word, numbers 대신 numbers[j] 또는 기능에 words[i]의 통과 : 또한 마지막 forfor(i=0;i<20;i++)는 닫는 중괄호 따라서

+0

나는 read (fd2 [0], & caracters [k], 1)를 사용했다. <- 나는 다른 사람들과 동일하게 오류를 더하고 더 이상 오류가 없다. 그러나 나는 단지 1. 오류가있다. : 입력 끝의 예상 선언이나 문장 –

+0

@AliBassam 내 편집 –

+0

을 확인한다. 하지만 실행 후, 그것은 단어를 묻는다, 나는 그것을 타이프한다. 그러면 2 개의 printfs를 보여준다. (이제 아버지는 .... 쓸 것이다.) 그 밖의 것은 없다 ..... –

0

대신 Line 60: error: expected declaration or statement at end of input 오류 부족 of :

수행 방법 :

write(fd1[1],(void*)&word[i],2); 

... 즉 데이터 자체의 값이 아니라 데이터 위치에 POINTER를 전달합니다.

+0

read (fd2 [0], & caracters [k], 1); <- 나는 다른 사람들에게도 똑같은 짓을했는데, 더 이상 오류는 없지만 나는 단지 1 개 밖에 없었습니다. 오류 : 입력이 끝날 때 예상되는 선언이나 문장 –

+0

줄 번호는 무엇입니까? 어떤 라인에 해당합니까? –

+0

닫는 브래킷이 필요했습니다. 하지만 실행 후, 그것은 단어를 묻습니다, 내가 그것을 입력 한 다음 2 printfs (지금은 아버지 .... 쓸 것입니다) 다음 다른 아무것도 ..... Logged –

관련 문제