打印

请教关于SIGSEGV的问题。。。

请教关于SIGSEGV的问题。。。

[LuoXinsheng@localhost process]$cat clone.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
int VAR = 11;
int func()
{
        VAR = 100;
        exit(EXIT_SUCCESS);
}
int main(void)
{
        int retval;
        void *child_stack;

        child_stack = (void *)malloc(5000);

        printf("The variable was %d\n", VAR);

        retval = clone(func, child_stack, CLONE_VM | CLONE_FILES, NULL);
        if(retval == -1)
        {
                perror("clone");
                exit(EXIT_FAILURE);
        }
        sleep(2);
        printf("The variable is %d\n", VAR);
        exit(EXIT_SUCCESS);
}
[LuoXinsheng@localhost process]$gcc -g clone.c -o clone
[LuoXinsheng@localhost process]$clone
The variable was 11
段错误



(gdb) run
Starting program: /root/lxs/linux/process/clone
The variable was 11

Program received signal SIGSEGV, Segmentation fault.
0x44c288fb in clone () from /lib/libc.so.6
不明白怎么回事。。。为什么会出现无效内存引用呢???

TOP

int func() should be int func(void* arg) i guess?
#include <sched.h>int clone(int (*fn)(void *), void *child_stack,
int flags, void *arg, ...           /* pid_t *pid, struct user_desc *tlspid_t
*" ctid " */ );"

TOP

ex:
复制内容到剪贴板
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>

int variable;

int do_something(void*) {
   variable = 42;
   _exit(0);
}

int main(int argc, char *argv[]) {
   void *child_stack;
   char tempch;

   variable = 9;
   child_stack = (void *) malloc(16384);
   printf("The variable was %d\n", variable);

   clone(do_something, child_stack, CLONE_VM|CLONE_FILES, NULL);
   sleep(1);

   printf("The variable is now %d\n", variable);
   return 0;
}
taoewang@baryon:/home/taoewang/temp> ./a.out
The variable was 9
The variable is now 42

[ 本帖最后由 wangzhonnew 于 2008-4-8 02:24 编辑 ]

TOP


感谢一直以来您对我们的支持!
当前时区 GMT+8, 现在时间是 2008-10-8 09:06 京ICP证060528 号

Designed By 17DST