ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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.
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 ...
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).
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?
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;
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.