[완료] Linux System Call clone() 에 관하여.

mach337의 이미지

아래와 같이 소스를 컴파일 해서 실행하면 커널이 프로세스를 강제로 종료하는 것 처럼 보이는데 어느곳에 문제가 있는지 모르겠습니다. 혹시 아시는분 계신가요?

시스템 : RedHat 9 i686
컴파일러 : gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
커널 : 2.4.20-31.9

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
 
int variable, fd;
 
int do_something() {
        variable = 42;
        close(fd);
        _exit(0);
}
 
int main(int argc, char *argv[]) {
        void **child_stack;
        char tempch; 
        int ret = 0;
 
        variable = 9;
        fd = open("test.file", O_RDONLY);
        child_stack = (void **) malloc(16384);
        printf("The variable was %d\n", variable);
 
        ret = clone(do_something, child_stack, CLONE_VM|CLONE_FILES, NULL);
        if (ret < 0) {
                perror("clone");
        }       
        sleep(1);
 
        printf("The variable is now %d\n", variable);
        if (read(fd, &tempch, 1) < 1) {
                perror("File Read Error");
                exit(1);
        }       
        printf("We could read from the file\n");
        return 0;
}

결과
[root@mach2 tmp]# gcc -o test_clone test_clone.c -Wall
[root@mach2 tmp]# ./test_clone                        
The variable was 9
Killed
bushi의 이미지

The child_stack argument specifies the location of the stack used by
the child process. Since the child and calling process may share mem-
ory, it is not possible for the child process to execute in the same
stack as the calling process. The calling process must therefore set
up memory space for the child stack and pass a pointer to this space to
clone. Stacks grow downwards on all processors that run Linux (except
the HP PA processors), so child_stack usually points to the topmost
address
of the memory space set up for the child stack.

출처: man://clone

[bushi@rose net]$
[bushi@rose net]$ diff -uN clone_orig.c clone.c
--- clone_orig.c        2007-03-06 18:23:05.000000000 +0900
+++ clone.c     2007-03-06 18:22:27.000000000 +0900
@@ -24,7 +24,7 @@
        child_stack = (void**) malloc(16384);
        printf("The variable was %d\n", variable);
 
-       ret = clone(do_something, child_stack, CLONE_VM|CLONE_FILES, NULL);
+       ret = clone(do_something, child_stack+16384, CLONE_VM|CLONE_FILES, NULL);
        if (ret < 0) {
                perror("clone");
        }
[bushi@rose net]$
[bushi@rose net]$ gcc -s -o clone clone.c
[bushi@rose net]$
[bushi@rose net]$ ./clone
The variable was 9
The variable is now 42
File Read Error: Bad file descriptor
[bushi@rose net]$

mach337의 이미지

너무 감사합니다. man page 도 제대로 안보고..ㅋㅋ... ;p
i686 RedHat 에서는 Segment Fault 가 발생하네요.
다른 시스템에서는 안해봐서 모르겠구요.
MMU 가 없는 ARM 에서는 uClibc 0.9.26, gcc 2.95.3 에서는 잘 됩니다.
감사합니다.

댓글 달기

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