부모 프로세스는 인수를 main()에 전달하고 파이프를 통해 자식 프로세스에 한 번에 하나씩 문자를 보냅니다. 쓰기). 자식 프로세스는 부모 프로세스가 보낸 문자 수를 계산하고 부모로부터받은 문자 수를 출력합니다. 자식 프로세스는 어떤 식 으로든 main()에 대한 인수를 사용하면 안됩니다. 아이는 정상적으로 돌아가고 부모는 아이를 죽이지 않아야합니다.부모 프로세스 및 자식 프로세스
나는 논증을 오른쪽으로 계산합니까? 나는 한 번에 하나씩 논쟁을 보내고, 나는 그 아이를 거두고 있습니까?
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define size = 100;
int main(int argc, char *argv[])
{
int i, count =0;
int c;
int fdest[2]; // for pipe
pid_t pid; //process IDs
char buffer[BUFSIZ];
if (pipe(fdest) < 0) /* attempt to create pipe */
perror("pipe");
if ((pid = fork()) < 0) /* attempt to create child/parent process */
{
perror("fork");
}
/* parent process */
else if (pid > 0) {
close(fdest[0]);
for (i=1; i < argc; ++i)
{
for (c=0; c < strlen(argv[i]); ++c) {
write(fdest[1], &argv[i][c], 1);
}
}
close(fdest[1]);
wait(NULL);
exit(0);
} else {
/* child Process */
close(fdest[1]);
while (read(fdest[0], &buffer, 1) > 0)
{
count++;
}
printf("\nchild: counted %d characters\n", count);
}
wait(NULL);
exit(0);
}
이전에이 질문을 게시 한 것으로 보입니다. 아마도 당신은 이전 질문과 다른 점을 설명 할 수 있습니다 : http://stackoverflow.com/questions/2390868/passing-command-line-arguments-to-a-child-process-and-count-them –
메인 차이점은 한 번에 한 글자 씩 파이프에 쓰고 한 번에 한 글자 씩 파이프에서 읽는 것에 대한 엄격한 것 같다. –
숙제 태그가 필요합니까? –