리눅스 상에서 자식에서 파이프를 통해 부모로 데이터를 보내려고 합니다만..

hkp1104의 이미지

지금 문제가 부모에서 자식으로 넘어가는데 걸리는 시간과,
자식에서 부모로 넘어가는데 걸리는 시간을 각각 구하려고 하고 있습니다.

그래서 파이프를 2개 만들어서 하나는 부모->자식, 다른 하나는 자식->부모로 쓰려고 합니다.

그런데 부모에서 자식으로 넘어가는데 걸리는 시간은 문제없이 구해지는데.
자식에서 부모로 넘어가는데 걸리는 시간을 못구하겠습니다.

원래 계획은 자식이 죽기 직전에 자신의 시간을 파이프로 보낸 뒤 죽고,
부모는 자식이 죽은 직후 wait에서 깨어나 현재 시간을 잰 뒤 파이프에서 자식이 보낸 시간을 받아내어,
시간을 측정하려고 하였습니다.

그런데 부모가 자식이 보낸 정보를 파이프에서 못가져 오네요.

아무래도 자식이 죽고 난 뒤 파이프가 닫혀서 그런 것으로 추측됩니다만,
이런 경우 파이프를 못쓴다면 어떤 방법을 써야 할까요?

소스코드는 다음과 같습니다.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
 
int main(void)
{
int time_pipe[2][2] = {0};
	int random_numbers[100] = {0};
	int child_process_ID = 0;
	int child_process_status = 0;
	int index1 = 0;
	int sum = 0;
 
	struct timeval total_time_start;
	struct timeval total_time_end;
	struct timeval part_time_start;
	struct timeval part_time_end;
 
	gettimeofday(&total_time_start,NULL);
 
	srand(time(NULL));
 
if(pipe(time_pipe[0]) < 0)
{
	perror("Pipe 0 error : ");
	exit(0);
}
 
if(pipe(time_pipe[1]) < 0)
{
	perror("Pipe 1 error : ");
	exit(0);
}
 
	for(index1 = 0; index1 < 100; index1++)
	{
		random_numbers[index1] = rand() % 100;
	}
 
	gettimeofday(&part_time_start,NULL);
 
printf("Elapsed time switching Child start : %ld m sec\n", part_time_start.tv_usec);
 
	child_process_ID = fork();
 
	if(child_process_ID > 0)
	{
close(time_pipe[0][1]);
close(time_pipe[1][0]);
write(time_pipe[0][0], (void *)&part_time_start, sizeof(struct timeval));
 
		child_process_ID = wait(&child_process_status);
 
		gettimeofday(&part_time_end,NULL);
read(time_pipe[1][1], (void *)&part_time_start, sizeof(struct timeval));
printf("Elapsed time switching Parent restart : %ld m sec\n", part_time_start.tv_usec);
 
		part_time_end.tv_usec = part_time_end.tv_usec - part_time_start.tv_usec;
		part_time_end.tv_sec = part_time_end.tv_sec - part_time_start.tv_sec;
		if( part_time_end.tv_usec < 0 )
		{
			part_time_end.tv_usec += 1000000;
		}
		printf("Elapsed time switching Child to Parent : %ld m sec\n", part_time_end.tv_usec);///1000
 
		printf("Parent : %d is dead\n",child_process_ID);
	}
 
	else if(child_process_ID == 0)
	{
		gettimeofday(&part_time_end,NULL);
close(time_pipe[0][0]);
close(time_pipe[1][1]);
read(time_pipe[0][1], (void *)&part_time_start, sizeof(struct timeval));
 
		part_time_end.tv_usec = part_time_end.tv_usec - part_time_start.tv_usec;
		part_time_end.tv_sec = part_time_end.tv_sec - part_time_start.tv_sec;
		if( part_time_end.tv_usec < 0 )
		{
			part_time_end.tv_usec += 1000000;
		}
		printf("Elapsed time switching Parent to Child : %ld m sec\n", part_time_end.tv_usec);///1000
 
		for(index1 = 0; index1 < 100; index1++)
		{
			sum += random_numbers[index1];
		}
 
		printf("Child : Sum is %d\n",sum);
 
		gettimeofday(&part_time_start,NULL);
write(time_pipe[1][0], (void *)&part_time_start, sizeof(struct timeval));
printf("Elapsed time switching Child end : %ld m sec\n", part_time_start.tv_usec);
 
		exit(0);
	}
 
		gettimeofday(&total_time_end,NULL);
 
		total_time_end.tv_usec = total_time_end.tv_usec - total_time_start.tv_usec;
		total_time_end.tv_sec = total_time_end.tv_sec - total_time_start.tv_sec;
		if( total_time_end.tv_usec < 0 )
		{
			total_time_end.tv_usec += 1000000;
		}
		printf("recognition time : %ld m sec\n", total_time_end.tv_usec);///1000
 
	return 0;
}