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

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;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.