1 명의 부모와 2 명의 자녀가있는 코드를 만들려고합니다. 상기 방법은 3 개 개의 매개 변수를 받아 봐 : 라인이 쌍은누군가 내 코드를 C로 도울 수 있습니까?
- 경우, 방법 proccess_pair 및 단어 1에 광고를 보내 original_file의 단어 1 상위는 파일을 줄을 판독
word2. 행에 word1이 포함되어 있으면 file_1.txt에 행을 저장하십시오.
- 행이 홀수 인 경우 process_odd 및 word2 행을 보냅니다. 될 것입니다 (chil1 및 chil2에 대한)
int p_h1[2] // pipe from parent to child1 int p_h2[2];// pipe from parent to child2 int main(int argc, char **argv){ pid_t pdi1, pdi2; FILE *fd; // for original file FILE *p_h1f, *p_h2f; //file create for child1 and child2 respectively char buffer[1024];//buffer if (pid1<0){ fprintf(stderr,"Error fork \n %s \n",strerror(errno)); exit(EXIT_FAILURE); } else if (pid1==0){//Im the child1 //proccess for child 1 proccess_pair(arg[2]); exit(EXIT_SUCCESS); } pid2 = fork(); if (pid2<0){ fprintf(stderr,"Error fork \n %s \n",strerror(errno)); exit(EXIT_FAILURE); } else if (pid2==0){//Im the child2 //proccess for child 2 proccess_odd(arg[2]); exit(EXIT_SUCCESS); } //Parent dont read from pipe close(p_h1[0]); close(p_h2[0]); fd = fopen(argv[1],"r"); //I openthe file for read it; p_h1f = fdopen(p_h1[1],"w") p_h2f = fdopen(p_h2[1],"w") int i = 1; while(fgets(buffer,1024,fd) != NULL){ if (i % 2 ==0){ //check if the lines is pairs fputs(buffer,p_h1f); fflush(p_h1f); }else{ fputs(buffer,p_h2f); fflush(p_h2f); } i++; } close(p_h1[1]); close(p_h2[1]); fclose(fd); wait(NULL); wait(NULL); }
두 가지 방법 : 선이 단어 1을 포함 경우, file_2.txt에서 C에서
임 초보자 라인을 저장하고, 나는이 함께 노력
void proccess_pair(char *word1){
FILE *fd;
fd = fopen("file_1.txt","w");
//closing the not used
close(p_h1[1]);
close(p_h2[1]);
close(p_h2[0]);
int nsto = dup(1)//duplicate the stdout
dup2(fd,1);//changing stdout->file_1.txt
execlp("grep","grep",word1,NULL);//execution of grep
exit(EXIT_SUCCESS);
}
임 학습과 내가 내가 도움을 필요로 이런 이유로, 많은 많은 오류가 있음을 알고, 이런 이유로 나는 단지 그들 중 하나를 구현 같은 (그러나 파이프의 올바른 측면을 폐쇄).
감사
전 C에서 배열에 많은 파이프를 만드는 방법을
어린이 한 명과 함께 파이프를 사용하는 방법을 알고 있다면 어린이 2 명을 위해 뭔가 쓰고 코드를 게시하십시오. –
@ Jean-BaptisteYunès 편집! 감사 ejeje – randall