LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 11-12-2022, 12:33 PM   #1
I_Love_Linux_2022
LQ Newbie
 
Registered: Nov 2022
Posts: 1

Rep: Reputation: 0
Stack Location ("/proc/<tpid>/maps" vs. "/proc/thread-self/maps" vs. "pthread_attr_getstack()")


Dear "Linux Users",

Kindly, I am writing an application where I need to know exactly the start of the thread stack after setting it using "posix_memalign()" and "pthread_attr_setstack()".

The problem is that I am getting different values using 3 different methods as follows:

1)
tid = syscall(SYS_gettid);
sprintf(filename, "cat /proc/%d/maps", tid);
system(filename);

2)
system("cat /proc/thread-self/maps");

3)
pthread_attr_getstack();


The following code is my thread:


Code:
void* vidMainThread(void* ptr)
{ 
  int fd;
  pid_t tid;
  char filename[128];

  /* First Method */
  tid = syscall(SYS_gettid);
  printf("Tread ID. SYS_gettid() = %d\r\n", tid);
  sprintf(filename, "cat /proc/%d/maps", tid);
  system(filename);

  printf("\r\n\n", tid);

  /* Second Method */
  system("cat /proc/thread-self/maps");

  printf("\r\n\n", tid);

  /* Third Method */
  vidReserveStack();
}

void vidReserveStack(void)
{
  uint8_t au8Test[1024];

  au8Test[1023] = (uint8_t)0x5A;
  printf("au8Test[1023] = 0x%X, Address = 0x%X\r\n", au8Test[1023], &au8Test[1023]);

  au8Test[0] = (uint8_t)0x5A;
  printf("au8Test[0] = 0x%X, Address = 0x%X\r\n", au8Test[0], &au8Test[0]);
}
The output is as follows:

/* First Method */
fffffacc9000-fffffacea000 rw-p 00000000 00:00 0 [stack]

/* Second Method */
ffffc4a41000-ffffc4a62000 rw-p 00000000 00:00 0 [stack]

/* Third Method */
au8Test[1024] = 0x00, Address = 0x8FEAF90F
au8Test[0] = 0x5A, Address = 0x8FEAF510

Note that:

The output of "pthread_attr_getstack()" is:

StackStart = 0x8FE91000
StackSize = 131072

which is considered logical when we compare it with the output of the "Third Method" as follows:

StackEnd = StackStart + StackSize = 0x8FEB1000 which is higher than and really close to "au8Test[1023]" however, is really far away from the output of the first two methods.


Do you know why I am getting these different values when using different methods?

Thank you very much.
Best regards.

Last edited by I_Love_Linux_2022; 11-12-2022 at 12:41 PM.
 
  


Reply



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
Format for reading /proc/self/status and /proc/meminfo ldmarks Linux - Kernel 1 11-09-2018 05:45 PM
stack limit of Process and stack limit of thread particlereddy Linux - Kernel 7 11-21-2015 01:51 PM
Thread overran stack, or stack corrupted rrlangly Linux - Kernel 1 12-20-2011 11:04 PM
single 8K process stack vs 4K process stack and a seperate 4K interrupt stack charvak Linux - Kernel 1 03-17-2010 06:58 PM
problem on /proc/self/exe and /proc/num/exe snowing Programming 6 08-31-2006 01:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:44 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