LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-15-2011, 01:21 AM   #1
psmurthy
LQ Newbie
 
Registered: Mar 2010
Posts: 11

Rep: Reputation: 0
Thread stack printing


Hi here is my program ...

#include <stdio.h>
#include <pthread.h>

pthread_attr_t attr;

void *threadRoutine(void *threadMsg)
{
size_t myStackSize;
void *stackAddr;

pthread_attr_getstack(&attr,&stackAddr,&myStackSize);

printf("thread %d\n",(int)threadMsg);
printf("stack address %lx %lx\n",(long)stackAddr,(long)(stackAddr + 1));
printf("my stack size is %li\n",myStackSize);
printf("%d\n",*((int *)stackAddr)); <--------------------Causing segmentation fault..

pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
size_t stacksize;
int rc;
size_t i;
pthread_t thread;

pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr,&stacksize);
printf("Default stack size = %ld\n",stacksize);
stacksize = 12582912;
pthread_attr_setstacksize(&attr,stacksize);

rc = pthread_create(&thread,&attr,threadRoutine,(void *)i);
if(rc) {
printf("ERROR:return code from pthread_create %d\n",rc);
exit(-1);
}

pthread_exit(NULL);

}

The place i pointed is giving segmentation fault.Ideally it should not as i am accessing my own stack area and that address does not fall in kernel space.
Can you help me in identifying the root cause..or give me solution to access my own stack area...
 
Old 08-16-2011, 08:35 AM   #2
mayankc
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
Hi Murthy,

The address that you get by calling pthread_attr_getstack() is not what you are seeing it when printing. This is because you haven't included an important header file stdlib.h . So first and formost, include this header file.

Also, please remove all the compilation warnings from the code. Use %p or %08x instead of %lu or %li for printing the addresses.

After doing all these improvements, you will realize that the stack base address is 0. This is because by default pthread assigns NULL to the base address. So, If you try to print a value to a NULL address, it will give segmentation fault.

May be you want to give some different base address (non NULL) to the stack and the get the address and print the value. Use pthread attribute functions to set a different base address.

Thanks,
Mayank
 
Old 08-17-2011, 01:00 AM   #3
psmurthy
LQ Newbie
 
Registered: Mar 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Thread stack printing

Hi Mayank,

Thanks a lot for your reply ...

I modified my program little bit as you suggested .. but there is no improvement in output ...

###########################################################################################

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

pthread_attr_t attr;

void *threadRoutine(void *threadMsg)
{
size_t myStackSize;
void *stackAddr;
pthread_attr_getstack(&attr,&stackAddr,&myStackSize);
printf("threadRoutine:stack address %p\n",(long)stackAddr);
printf("threadRoutine:stack size is %li\n",myStackSize);
printf("%d\n",*((int *)stackAddr)); <--------------------Causing segmentation fault..
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
size_t stacksize;
void *stackAddr;
int rc;
size_t i;
pthread_t thread;

pthread_attr_init(&attr);
pthread_attr_getstack(&attr,&stackAddr,&stacksize);

stacksize = 12582912;
pthread_attr_setstacksize(&attr,stacksize);
printf("Main:stack address %p\n",(long)stackAddr);
printf("Main:stack size is %li\n",myStackSize);
rc = pthread_create(&thread,&attr,threadRoutine,(void *)i);
if(rc) {
printf("ERROR:return code from pthread_create %d\n",rc);
exit(-1);
}
pthread_exit(NULL);
}
######################################################################################
output:
Main:stack address (nil)
Main:stack size is 0
threadRoutine:stack address 0xffffffffff400000
threadRoutine:stack size is 12582912
Segmentation fault
################################

1. Stack address in main program is 0...which is expected as thread is not yet created ...
2. in thread routing ...seems it is giving correct stack address...(i included stdlib.h also ...) ... But still accessing that address causing segmentation fault ...
 
Old 08-17-2011, 01:43 AM   #4
mayankc
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
Hi,
Your code above did not work as it is. I request you to post at least working code so that others do not have to consume their time in making it working. Also, start learning coding guidelines. It will help you a lot in programming.

Below is the code that is a lot cleaner and working one. Please note the changes i have made by comparing it with the above code (posted in the previous post by you).

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

pthread_attr_t attr;

void *threadRoutine(void *threadMsg)
{
size_t myStackSize;
void *stackAddr;
pthread_attr_getstack(&attr,&stackAddr,&myStackSize);
printf("threadRoutine:stack address %p\n",stackAddr);
printf("threadRoutine:stack size is %x\n",myStackSize);
printf("%d\n",*((int *)stackAddr)); // <--------------------Causing segmentation fault..
pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
size_t stacksize;
void *stackAddr;
int rc;
size_t i;
pthread_t thread;

pthread_attr_init(&attr);

stacksize = 0xC00000; //12582912;
stackAddr = malloc(stacksize);
pthread_attr_setstack(&attr,stackAddr, stacksize);
pthread_attr_getstack(&attr,&stackAddr, &stacksize);
printf("Main:stack address %p\n",stackAddr);
printf("Main:stack size is %x\n",stacksize);
rc = pthread_create(&thread,&attr,threadRoutine,(void *)i);
if(rc) {
printf("ERROR:return code from pthread_create %d\n",rc);
exit(-1);
}
pthread_exit(NULL);
}

Output:
#############################################
Main:stack address 0xb6abb008
Main:stack size is c00000
threadRoutine:stack address 0xb6abb008
threadRoutine:stack size is c00000
0
#############################################
 
Old 08-17-2011, 11:56 PM   #5
psmurthy
LQ Newbie
 
Registered: Mar 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Thread stack printing

Hi Mayank,
Extremely sorry for pasting not compiled code ...I have few more queries (hope fully last)...
1.What is purpose of adding stdlib.h file in this program?
2.You said , by default thread assigns NULL base stack address, but i see thread routing like below are working fine without assigning explicit stack address..then in that case where the local variables of that thread are stored?
void *threadRoutine(void *threadMsg)
{
int i;
int j;

for(i = 0; i < 10; i++) {
for(j=o; j < 10; j++) {
printf("(i+j) = %d\n",i+j);
}
}
}
3.In my previous code output, why stack address in main program and thread routine are different? both should be NULL as i didn't set the stack address explicitly?
 
Old 08-18-2011, 01:14 AM   #6
mayankc
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
Please find my answers inline

1.What is purpose of adding stdlib.h file in this program?

To remove compilation warnings generated by exit(). For more information see http://en.wikipedia.org/wiki/Stdlib.h

2.You said , by default thread assigns NULL base stack address, but i see thread routing like below are working fine without assigning explicit stack address..then in that case where the local variables of that thread are stored?
void *threadRoutine(void *threadMsg)
{
int i;
int j;

for(i = 0; i < 10; i++) {
for(j=o; j < 10; j++) {
printf("(i+j) = %d\n",i+j);
}
}
}

The local variable of the above code will be on stack only. What i meant is by default the stack start address is NULL, as soon as there is a need to store a local variable, the stack pointer gets incremented and the value is stored. Now, If we want the stack to have some Non-NULL starting address then we use pthread_attr_setstack(). This will do nothing but ask the stack pointer to point to the assigned address and start from there.

3.In my previous code output, why stack address in main program and thread routine are different? both should be NULL as i didn't set the stack address explicitly?

You have set the size of the stack in your previous code. I would suggest you to only use pthread_attr_setstack / pthread_attr_getstack for changing size or address. If you do not set the size of stack in your previous code [commenting stacksize = 12582912; and /or pthread_attr_setstacksize(&attr,stacksize);], you will get consistent result (i.e both the main and thread stack address will be set to NULL).

Thanks,
Mayank
 
  


Reply

Tags
linuxapplications



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
problem with thread stack using pthread_attr_setstackaddr() subham Programming 1 10-18-2010 04:20 AM
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
printing call stack of user thread biswarup Linux - Kernel 1 05-14-2010 11:21 AM
thread's stack lqu Linux - Kernel 5 01-12-2010 11:57 PM
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:42 AM.

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