Segmentation in thread created by clone() in linux...

Segmentation in thread created by clone() in linux...

Post by Ken » Thu, 19 Sep 2002 21:25:44



Hi, All

    I use clone() to create thread in LINUX. the call of clone is as
follows:
        for(i = 0; i < n; i++) {
            if((stack[i] = (char*)malloc(CHILD_STACK * sizeof(char)))
== NULL) {
                       fprintf(stderr, "<main>malloc stack error.");
                       exit(-1);
            }
            parm[i] = i;
            if((pid[i] = clone(child_send_process, stack[i] +
CHILD_STACK, CLONE_VM|CLONE_FILES, &parm[i])) < 0) {
                       fprintf(stderr, "<main>clone send_child_process
error.");
                       exit(-1);
            }
        }
    so n threads are created. But some of the thread will encounter
segmentation fault after runing some time. I am sure the pointer
allocated by my code is released correctly and the stack size here is
3M bytes.
    Can anybody tell me some possibilities which will case segfault in
thread made by clone()??

Thanks.

----Kent

 
 
 

Segmentation in thread created by clone() in linux...

Post by Eric Worral » Thu, 19 Sep 2002 21:58:18



> Hi, All

>     I use clone() to create thread in LINUX. the call of clone is as
> follows:
>         for(i = 0; i < n; i++) {
>             if((stack[i] = (char*)malloc(CHILD_STACK * sizeof(char)))
> == NULL) {
>                        fprintf(stderr, "<main>malloc stack error.");
>                        exit(-1);
>             }
>             parm[i] = i;
>             if((pid[i] = clone(child_send_process, stack[i] +
> CHILD_STACK, CLONE_VM|CLONE_FILES, &parm[i])) < 0) {
>                        fprintf(stderr, "<main>clone send_child_process
> error.");
>                        exit(-1);
>             }
>         }
>     so n threads are created. But some of the thread will encounter
> segmentation fault after runing some time. I am sure the pointer
> allocated by my code is released correctly and the stack size here is
> 3M bytes.
>     Can anybody tell me some possibilities which will case segfault in
> thread made by clone()??

> Thanks.

> ----Kent

Same reasons as any other code + race conditions. How does your code
work - do you create a thread pool or do you create a new thread to
handle each new request? Is all access to shared variables protected by
mutexes or semaphores?

Have you considered using fork instead of clone? Using fork can solve a
lot of problems, it is usually worth the extra effort of communicating
shared information via anonymous pipes rather than via direct access to
shared memory.

You could also try installing Electric Fence or a similar memory
debugging tool, this will help you isolate faulty memory management
code.

http://www.rpmfind.net/linux/rpm2html/search.php?query=electricfence&...

Eric Worrall

--
You have just received an Etech Solution
For all your Linux requirements contact


 
 
 

Segmentation in thread created by clone() in linux...

Post by Nils O. Sel?sd » Thu, 19 Sep 2002 23:38:59




>> Hi, All

>>     I use clone() to create thread in LINUX. the call of clone is as
>> follows:
>>         for(i = 0; i < n; i++) {
>>             if((stack[i] = (char*)malloc(CHILD_STACK * sizeof(char)))
>> == NULL) {
>>                        fprintf(stderr, "<main>malloc stack error.");
>>                        exit(-1);
>>             }
>>             parm[i] = i;
>>             if((pid[i] = clone(child_send_process, stack[i] +
>> CHILD_STACK, CLONE_VM|CLONE_FILES, &parm[i])) < 0) {
>>                        fprintf(stderr, "<main>clone send_child_process
>> error.");
>>                        exit(-1);
>>             }
>>         }
>>     so n threads are created. But some of the thread will encounter
>> segmentation fault after runing some time. I am sure the pointer
>> allocated by my code is released correctly and the stack size here is
>> 3M bytes.
>>     Can anybody tell me some possibilities which will case segfault in
>> thread made by clone()??

>> Thanks.

>> ----Kent

> Same reasons as any other code + race conditions. How does your code
> work - do you create a thread pool or do you create a new thread to
> handle each new request? Is all access to shared variables protected by
> mutexes or semaphores?

> Have you considered using fork instead of clone? Using fork can solve a
> lot of problems, it is usually worth the extra effort of communicating
> shared information via anonymous pipes rather than via direct access to
> shared memory.

> You could also try installing Electric Fence or a similar memory
> debugging tool, this will help you isolate faulty memory management
> code.

And last, is there any reason to use clone directly and not pthreads
(if threads are what one want ?)
 
 
 

Segmentation in thread created by clone() in linux...

Post by Ken » Fri, 20 Sep 2002 09:16:42


I have heard that pthread in linux is not so stable for High
Availability Program which needs 7x24 peformance. So I use clone()
instead of pthread.

I have a share memory and fd between these two processes. so thread is
suitable for me instead of using fork().

How to use eletric fence? If segfault appeared, How can i trace back
to find where the error is?

thanks



> >> Hi, All

> >>     I use clone() to create thread in LINUX. the call of clone is as
> >> follows:
> >>         for(i = 0; i < n; i++) {
> >>             if((stack[i] = (char*)malloc(CHILD_STACK * sizeof(char)))
> >> == NULL) {
> >>                        fprintf(stderr, "<main>malloc stack error.");
> >>                        exit(-1);
> >>             }
> >>             parm[i] = i;
> >>             if((pid[i] = clone(child_send_process, stack[i] +
> >> CHILD_STACK, CLONE_VM|CLONE_FILES, &parm[i])) < 0) {
> >>                        fprintf(stderr, "<main>clone send_child_process
> >> error.");
> >>                        exit(-1);
> >>             }
> >>         }
> >>     so n threads are created. But some of the thread will encounter
> >> segmentation fault after runing some time. I am sure the pointer
> >> allocated by my code is released correctly and the stack size here is
> >> 3M bytes.
> >>     Can anybody tell me some possibilities which will case segfault in
> >> thread made by clone()??

> >> Thanks.

> >> ----Kent

> > Same reasons as any other code + race conditions. How does your code
> > work - do you create a thread pool or do you create a new thread to
> > handle each new request? Is all access to shared variables protected by
> > mutexes or semaphores?

> > Have you considered using fork instead of clone? Using fork can solve a
> > lot of problems, it is usually worth the extra effort of communicating
> > shared information via anonymous pipes rather than via direct access to
> > shared memory.

> > You could also try installing Electric Fence or a similar memory
> > debugging tool, this will help you isolate faulty memory management
> > code.
> And last, is there any reason to use clone directly and not pthreads
> (if threads are what one want ?)

 
 
 

Segmentation in thread created by clone() in linux...

Post by Nils O. Sel?sd » Fri, 20 Sep 2002 18:07:57



> I have heard that pthread in linux is not so stable for High
> Availability Program which needs 7x24 peformance. So I use clone()
> instead of pthread.

Never heard that. If it makes you feel any better we have a threaded
app that runs 24x7, and have for about a year now. And Apache2 is
threaded.
pthread support is pretty subtle thought, when it comes to the pthreads
standard compliance.

> I have a share memory and fd between these two processes. so thread is
> suitable for me instead of using fork().

> How to use eletric fence? If segfault appeared, How can i trace back
> to find where the error is?

> thanks



>> >> Hi, All

>> >>     I use clone() to create thread in LINUX. the call of clone is as
>> >> follows:
>> >>         for(i = 0; i < n; i++) {
>> >>             if((stack[i] = (char*)malloc(CHILD_STACK * sizeof(char)))
>> >> == NULL) {
>> >>                        fprintf(stderr, "<main>malloc stack error.");
>> >>                        exit(-1);
>> >>             }
>> >>             parm[i] = i;
>> >>             if((pid[i] = clone(child_send_process, stack[i] +
>> >> CHILD_STACK, CLONE_VM|CLONE_FILES, &parm[i])) < 0) {
>> >>                        fprintf(stderr, "<main>clone send_child_process
>> >> error.");
>> >>                        exit(-1);
>> >>             }
>> >>         }
>> >>     so n threads are created. But some of the thread will encounter
>> >> segmentation fault after runing some time. I am sure the pointer
>> >> allocated by my code is released correctly and the stack size here is
>> >> 3M bytes.
>> >>     Can anybody tell me some possibilities which will case segfault in
>> >> thread made by clone()??

>> >> Thanks.

>> >> ----Kent

>> > Same reasons as any other code + race conditions. How does your code
>> > work - do you create a thread pool or do you create a new thread to
>> > handle each new request? Is all access to shared variables protected by
>> > mutexes or semaphores?

>> > Have you considered using fork instead of clone? Using fork can solve a
>> > lot of problems, it is usually worth the extra effort of communicating
>> > shared information via anonymous pipes rather than via direct access to
>> > shared memory.

>> > You could also try installing Electric Fence or a similar memory
>> > debugging tool, this will help you isolate faulty memory management
>> > code.
>> And last, is there any reason to use clone directly and not pthreads
>> (if threads are what one want ?)

 
 
 

Segmentation in thread created by clone() in linux...

Post by Eric Worral » Fri, 20 Sep 2002 18:24:12



> I have heard that pthread in linux is not so stable for High
> Availability Program which needs 7x24 peformance. So I use clone()
> instead of pthread.

AFAIK pthread and clone are both stable.

Quote:> I have a share memory and fd between these two processes. so thread is
> suitable for me instead of using fork().

fork is used in most high availability Linux systems. Process pools and
request -> fork architectures are not as efficient as thread pools but
they are definitely more stable - fork eliminates most of the
opportunities for race conditions.

Quote:> How to use eletric fence? If segfault appeared, How can i trace back
> to find where the error is?

The goal with electric fence is to detect the instruction which corrupts
the memory. Memory corruption is often difficult to trace because the
instruction which corrupts the memory is often not the instruction which
causes the SEGFAULT. Electric fence increases the likelyhood an invalid
memory access will cause an immediate failure.

http://perens.com/FreeSoftware/

or http://www.rpmfind.net to find an Electric Fence RPM which matches
your system.

Eric Worrall

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

Segmentation in thread created by clone() in linux...

Post by Bob Robillar » Thu, 26 Sep 2002 03:25:43




>>I have heard that pthread in linux is not so stable for High
>>Availability Program which needs 7x24 peformance. So I use clone()
>>instead of pthread.

> AFAIK pthread and clone are both stable.

While you might call LinuxThreads (the pthreads library
shipped with glibc and used on most GNU/Linux boxes)
"stable," I dunno that you could call it "good."  :-)

It's not really the LinuxThreads' author's fault though.
I think that fiven the state of the Kernel when it was
written, he did an amazing job--it's a very impressive
hack (in the good sense of that word, the old MIT AI Lab
sense).  But if I wanted to write threading code on Linux,
I think I'd seriously consider using clone(), since it's
the native mechanism.

There's a new pthreads library coming for the GNU C
library on Linux soon:

http://kerneltrap.org/node.php?id=422

The whitepaper describing it is at

http://people.redhat.com/drepper/nptl-design.pdf

It has a great summary of the woes of current LinuxThreads
library:

   Perhaps the biggest problem was the absence of usable synchronization
   primitives in the kernel which forced the implementation to resort to
   using signals.  This, together with the concept of kernel threads
   missing in the kernel versions of the time, lead to non-compliant and
   fragile signal handling in the thread library.

Duke