LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2010, 03:54 AM   #1
subham
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
problem with thread stack using pthread_attr_setstackaddr()


When I set the stack base address of the child thread using the POSIX library function "pthread_attr_setstackaddr()", I am unable to access the memory contents of its parent. The data-structures that are created on the HEAP of its parent using malloc() are either getting destroyed or unaccessible when moving to the context of the child thread. These data-structures are being passed as an argument to the child thread.
Even if I make these variables global then also it is not working.
pthread_attr_setstacksize(tattr, ...);
stackbase = (void *) malloc(...);
pthread_attr_setstackaddr(tattr, stackbase);

But when I create the child thread without setting its stack base address using that pthread_attr_setstackaddr(), then it is able to access the parent's memory contents.

Why is that so ? Can anybody explain this ?
 
Old 10-18-2010, 04:20 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by subham View Post
When I set the stack base address of the child thread using the POSIX library function "pthread_attr_setstackaddr()", I am unable to access the memory contents of its parent. The data-structures that are created on the HEAP of its parent using malloc() are either getting destroyed or unaccessible when moving to the context of the child thread.
You should be using pthread_attr_setstack, since pthread_attr_setstacksize and pthread_attr_setstackaddr are now deprecated.

The most likely reason for your error is because the pthread_attr_setstackaddr does not set the base of the stack, but the initial stack pointer. On an x86 architecture and many others, this is the end of the allocated stack space (which is why it is non-portable and is now deprecated).

If you set it to the start of the allocated space, the stack will grow down into other memory and cause heap corruptions. Depending on your architecture, you could have used pthread_attr_setstackaddr(tattr, stackbase+stacksize) to fix your memory corruption.

The example below illustrates a child accessing the heap of the parent, with no issues.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
void *threadFn(void *arg)
{
// print string
	puts((char *) arg);
}
int main()
{
// create a string on the heap
	char *arg = malloc(3);
	arg[0] = '4'; arg[1] = '2'; arg[2] = 0;
// create a stack
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setstack(&attr, malloc(4096), 4096);
// start child thread
	pthread_t thread;
	pthread_create(&thread, &attr, threadFn, arg);
	pthread_attr_destroy(&attr);
	pthread_join(thread, 0);
}

Last edited by neonsignal; 10-18-2010 at 06:48 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing call stack of user thread biswarup Linux - Kernel 1 05-14-2010 11:21 AM
MySql - Thread stack overrun spettroelica Linux - Server 2 09-17-2008 03:23 AM
Command to list stack size of each thread in an application mannw72 Linux - Newbie 1 03-14-2007 06:31 PM
Thread stack size failure jdhedden Programming 0 03-27-2006 08:53 AM
Thread call stack StephenG Linux - General 3 08-24-2005 10:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration