execl()사용하면 process가 죽어요

study의 이미지

아래와 같은 code를 만들어 봤습니다.
목적은 main()에서 child process를 fork하고,
parent process와 child process는 loop를 돌면서
/bin/ls 를 5초 마다 출력하는 건데요.

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <error.h>
 
void child(int socket)
{
   int err;
   const char hello[] = "hello parent, I am child";
   write(socket, hello, sizeof(hello));
   while(1)
   {
      printf("in child\n");
      execl("/usr/bin/ls", "/usr/bin/ls", "-l", NULL);
      sleep(5);
   }
}
 
void parent(int socket)
{
   char buf[1024];
 
   int n = read(socket, buf, sizeof(buf));
   printf("parent received '%.*s'\n",n, buf);
   while(1)
   {
      printf("in parent\n");
//    execl("/usr/bin/ls", "ls", "-la", NULL);
      sleep(5);
   }
}
 
int main(void)
{
   int fd[2];
 
   static const int parentsocket = 0;
   static const int childsocket = 1;
   pid_t pid;
 
   socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
 
   pid = fork();
 
   if (pid == 0)
   {
      close(fd[parentsocket]);
      child(fd[childsocket]);
   }
   else
   {
      close(fd[childsocket]);
      parent(fd[parentsocket]);
   }
 
   exit(0);
}

위의 code를 실행하면 출력이 아래와 같습니다.
child process는 execl를 실행하자마자 종료되어 버린것 같아요
종료되지 않고, 계속 loop를 돌면서 ls를 실행하려면
어떻게 해야하는 건지요?

in child
parent received 'hello parent, I am child'
in parent
total 44
-rwxrwxr-x 1 study study 8964 Mar  1 20:14 a.out
-rw-rw-r-- 1 study study 1029 Feb 21 18:08 list.dat
-rw-rw-r-- 1 study study  385 Feb  8 18:51 my_list.c
-rw-rw-r-- 1 study study   44 Feb  8 18:54 phonebook.txt
-rw-rw-r-- 1 study study  953 Mar  1 22:32 process_test.c
-rwxrwxr-x 1 study study 9116 Mar  1 22:32 ptest
-rw-rw-r-- 1 study study  503 Feb  8 20:14 test.c
in parent
in parent
in parent
in parent
in parent
in parent
in parent
in parent
in parent
in parent
in parent

ymir의 이미지

exec 계열 함수들은 프로세스의 이미지를 새로운 프로세스 이미지로 바꿉니다.
즉, exec 이 호출되는 순간, 완전히 새로운 프로세스로 바뀌기 때문에..
exec 이후의 코드들은 exec 자체가 실패했을 때를 제외하고는 아무런 의미가 없습니다.

child 에서 뭔가 주기적으로 작업하기를 바란다면, exec 대신 system 함수로 바꾸는게 편할겁니다.
아니면 다시 child 에서 fork & exec 하게 만들어야 하는데.. 어차피 그게 system 함수와 같습니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

와니의 이미지

system("/usr/bin/ls");
아주 간단하니 좋네요.

study의 이미지

system()을 써도 좋고, fork, exec을 써도 좋은데요.
그런함수들을 이용해서 실행시키는 process의 PID를 읽고 싶은데요.
system()을 쓰면, system()을 이용해서 실행하는 program의 process ID를
어떻게 읽어오는지요?

 의 이미지

그러면 뭐 달리 선택의 여지가 없습니다.
ls를 실행시킬 쪽에서 fork를 한 번 더 하는 수밖에요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.