Application-managed pthreads stacks leaking


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Application-managed pthreads stacks leaking
# 1  
Application-managed pthreads stacks leaking

I'm trying to use application-managed stacks, i.e. allocating the stack myself and passing it to the pthread creation attributes through pthread_setstack -function. My problem is that the memory allocated for the stack seems to leak after the thread terminates.

Only information I have been able to find about this is in System Interfaces Chapter 2 section 2.9.8 "Use of Application-Managed Thread Stacks". It states that the application grants to the implementation permanent ownership over the memory region, and that in particular, the region of memory cannot be freed, nor can it be later specified as the stack for another thread.

I've tried to free the stack manually after making sure the thread in question has terminated (by joining to it), which seems to kind of work since the virtual size of the application decreases. However, when I allocate a new stack for a new thread from the heap and the memory previous used for another threads stack is reused, the application crashes at pthread_create.

I'm trying to accomplish (what I'm sure is non-portable) thread stack monitoring by periodically checking the thread stack memory regions for a high water mark. If someone knows another way to do this, I'd be glad to drop the whole application-manager stacks thing.

Has anyone experienced similar problems or alternatively successfully used application-managed stacks?

I'm using NPTL 4.2.

Thanks in advance,
Lasse
# 2  
You could take a look at the codebase for Valgrind as a start for monitoring the threads stack.
You should be able to free the malloc'd stack memory on pthread_exit, set the stack pointer to NULL and reuse.
Code:
/*header defs*/
typedef struct custom_thread {
        int cr_return;
	int thread_stack_size;
	pthread_t tid;
	pthread_attr_t attr;
	void *newstack;
} CTHREAD;
extern CTHREAD *init_custom_thread(int, int (*)(void *));
extern void destroy_custom_thread(CTHREAD *);

/*from shared library*/
extern CTHREAD *init_custom_thread(int sz, int (*teststackalloc)(void *arg)) {
CTHREAD *ret;

               if ( (ret = malloc(sizeof(*ret))) == NULL) {return NULL;}
               ret->thread_stack_size = sz;
               if ( (ret->newstack = malloc(sz)) == NULL) {free(ret); return NULL;}
               if (teststackalloc!= NULL) {
                    if (teststackalloc(ret->newstack) != 0) {free(ret->newstack); free(ret); return NULL;}
               }
               pthread_attr_init(&ret->attr);
               ret->cr_return = pthread_attr_setstack(&ret->attr,ret->newstack,ret->thread_stack_size);
               return ret;
}

extern void destroy_custom_thread(CTHREAD *arg) {

                                  if (arg->newstack != NULL) {free(arg->newstack); arg->newstack = NULL;}
                                  free(arg);
                                  arg = NULL;
}

/*test program*/

#include "custom_stack.h"
#include <limits.h>

void *hello_world(void *arg) {
                             
                  printf("Hello world from thread id %d.\n",pthread_self());
                  pthread_exit(NULL);
}

void dothread_work(CTHREAD *new_id) {
               pthread_create(&new_id->tid,&new_id->attr,hello_world,NULL);
               pthread_join(new_id->tid,NULL);
               destroy_custom_thread(new_id);
}
                
int main(void) {
int i = 0; 
CTHREAD *new_id;

               new_id = init_custom_thread(PTHREAD_STACK_MIN * 2, NULL);
               if (new_id == NULL) {return 1;}
               for (i = 0 ; i < 100; i++) {dothread_work(new_id); new_id = init_custom_thread(PTHREAD_STACK_MIN * 2, NULL);}
               dothread_work(new_id);
               return 0;
}

All works as expected.
# 3  
Thanks for the reply.

I could've sworn that I was doing the exact same thing, but I must have missed something, since the piece of code you provided works perfectly.

Anyways, thanks for you help. It was much appreciated.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #289
Difficulty: Easy
Sun Microsystems was acquired by Oracle Corporation in 2012.
True or False?

6 More Discussions You Might Find Interesting

1. Red Hat

Memory leaking

Hi All Would someone help me to find the detailed report on memory leak. any commands to get detailed report for OS level and applications As we are using REDHAT LINUX 5.9 Thanks Murali Muppa (2 Replies)
Discussion started by: murali969
2 Replies

2. IP Networking

LAN traffic leaking on to WAN

Network map: WAN external interface 192.0.0.0 network | WAN internal interface 192.0.3.0 network | 192.0.3.0 LAN | wireless router 192.0.3.1 | DSL modem 192.0.3.2 The problem I am having is that some traffic from the 192.0.3.0 LAN seems to be "leaking" onto the 192.0.0.0 WAN. I... (0 Replies)
Discussion started by: herot
0 Replies

3. Red Hat

memory leaking

I use linux and unix and run aplication. How I can use command top , vmstat ,free ...etc existing command to find out there is memory leaking ??? Please give me example . It is not allow to use compiler to compile the program /tool. (2 Replies)
Discussion started by: chuikingman
2 Replies

4. UNIX for Advanced & Expert Users

User defined stacks for threads using glibc-2.3.5

Hi, I am currently working with threads using linux 2.6.11 and glibc 2.3.5 on x86. When i am trying to create thread with user defined stacks glibc gives segmentation fault. I have written a wrapper to pthread_create () and generated shared object for the same. When i am trying to create any... (6 Replies)
Discussion started by: mysterious
6 Replies

5. Linux

User defined stacks for threads using glibc-2.3.5

Hi, I am currently working with threads using linux 2.6.11 and glibc 2.3.5 on x86. When i am trying to create thread with user defined stacks glibc gives segmentation fault. I have written a wrapper to pthread_create () and generated shared object for the same. When i am trying to create any... (0 Replies)
Discussion started by: mysterious
0 Replies

6. AIX

Purified binary is not leaking

Hi, I am working on a project which uses 64-bit capability of AIX. This is very recently ported to 64-bit and leaking like anything now. I purified the program on 64-bit and ran it, purify is not showing any leak. Even memory usage has become very normal in purified binary compared to my... (2 Replies)
Discussion started by: jeet_xp
2 Replies

Featured Tech Videos