[Multitasking] 2 threads use same address for a variable.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
zungnguyen
Posts: 10
Joined: Wed Sep 14, 2022 12:42 pm

[Multitasking] 2 threads use same address for a variable.

Post by zungnguyen »

Hi all,

I am testing for multitasking. I am facing with a problem. I create 2 tasks for 2 functions below
int TaskTest::count(int argc, char** argv)
{
int i, j, k;
uint64 rsp;
READ_CPU(RSP, rsp);
printf(" count %d %s %p %p", argc, argv[0], &i, rsp);
for (i = 0; i < 6; i++)
{
for (j = 0; j < 3000; j++)
for (k = 0; k < 5000; k++)
{}
READ_CPU(RSP, rsp);
printf("[count %d %p %p]", i, &i, rsp);

}

printf("\n[End count]\n");
return 0;
}

int TaskTest::ask(int argc, char** argv)
{
int i, j, k;
uint64 rsp;
READ_CPU(RSP, rsp);
printf(" ask %d %s %p %p", argc, argv[0], &i, rsp);

for (i = 0; i < 6; i++)
{
for (j = 0; j < 3000; j++)
for (k = 0; k < 5000; k++)
{}
READ_CPU(RSP, rsp);
printf(" [ask %d %p %p] ", i, &i, rsp);
}
printf("\n [End ask] \n");
return 1;
}
When i printed the address of variable i in 2 functions (run in 2 threads), it is pointed to same address when the os run into loops. If function "count" run first, the address of i in function "count" is also use for the address of i in function "ask".The same situation if the function "ask" run first. I also checked that rsp and other registers are saved and loaded correctly.

Does anyone know about that? Thanks
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: [Multitasking] 2 threads use same address for a variable

Post by Octocontrabass »

Did you try using a debugger to check the parameters being passed to printf()? Perhaps the actual problem is that printf() isn't reentrant.
thewrongchristian
Member
Member
Posts: 426
Joined: Tue Apr 03, 2018 2:44 am

Re: [Multitasking] 2 threads use same address for a variable

Post by thewrongchristian »

zungnguyen wrote:Hi all,

I am testing for multitasking. I am facing with a problem. I create 2 tasks for 2 functions below

When i printed the address of variable i in 2 functions (run in 2 threads), it is pointed to same address when the os run into loops. If function "count" run first, the address of i in function "count" is also use for the address of i in function "ask".The same situation if the function "ask" run first. I also checked that rsp and other registers are saved and loaded correctly.

Does anyone know about that? Thanks
How are you starting your tasks? It sounds like they're both using the same stack, as if called sequentially and run to completion, but without indicating how the tasks are called, it's impossible to say.

It sounds like you need to read up on threading and what constitutes a thread context. At the minimum, you'll need a per thread stack (which is what it sounds like you don't have yet) and a structure containing the platform callee saved registers.

The GNU Pth implementation paper is as good an introduction as any to the subject, and while it is for a user space threading library, it is an excellent place to start, user code being easier to develop and step in a debugger.

My kernel threads are very similar, in that I use setjmp/longjmp primitives to switch tasks. The hard work, and what is highlighted in the paper, is how to bootstrap a new thread with an arbitrary stack.
zungnguyen
Posts: 10
Joined: Wed Sep 14, 2022 12:42 pm

Re: [Multitasking] 2 threads use same address for a variable

Post by zungnguyen »

thewrongchristian wrote:
zungnguyen wrote:Hi all,

I am testing for multitasking. I am facing with a problem. I create 2 tasks for 2 functions below

When i printed the address of variable i in 2 functions (run in 2 threads), it is pointed to same address when the os run into loops. If function "count" run first, the address of i in function "count" is also use for the address of i in function "ask".The same situation if the function "ask" run first. I also checked that rsp and other registers are saved and loaded correctly.

Does anyone know about that? Thanks
How are you starting your tasks? It sounds like they're both using the same stack, as if called sequentially and run to completion, but without indicating how the tasks are called, it's impossible to say.

It sounds like you need to read up on threading and what constitutes a thread context. At the minimum, you'll need a per thread stack (which is what it sounds like you don't have yet) and a structure containing the platform callee saved registers.

The GNU Pth implementation paper is as good an introduction as any to the subject, and while it is for a user space threading library, it is an excellent place to start, user code being easier to develop and step in a debugger.

My kernel threads are very similar, in that I use setjmp/longjmp primitives to switch tasks. The hard work, and what is highlighted in the paper, is how to bootstrap a new thread with an arbitrary stack.
The stack addresses are different. The magic thing happen when the program run into the loop :( , before that, every address has correct value.
I make context switching in Timer interrupt handler. I update rsp, rip, in the Stack and reload the registers value
zungnguyen
Posts: 10
Joined: Wed Sep 14, 2022 12:42 pm

Re: [Multitasking] 2 threads use same address for a variable

Post by zungnguyen »

zungnguyen wrote:Hi all,

I am testing for multitasking. I am facing with a problem. I create 2 tasks for 2 functions below
int TaskTest::count(int argc, char** argv)
{
int i, j, k;
uint64 rsp;
READ_CPU(RSP, rsp);
printf(" count %d %s %p %p", argc, argv[0], &i, rsp);
for (i = 0; i < 6; i++)
{
for (j = 0; j < 3000; j++)
for (k = 0; k < 5000; k++)
{}
READ_CPU(RSP, rsp);
printf("[count %d %p %p]", i, &i, rsp);

}

printf("\n[End count]\n");
return 0;
}

int TaskTest::ask(int argc, char** argv)
{
int i, j, k;
uint64 rsp;
READ_CPU(RSP, rsp);
printf(" ask %d %s %p %p", argc, argv[0], &i, rsp);

for (i = 0; i < 6; i++)
{
for (j = 0; j < 3000; j++)
for (k = 0; k < 5000; k++)
{}
READ_CPU(RSP, rsp);
printf(" [ask %d %p %p] ", i, &i, rsp);
}
printf("\n [End ask] \n");
return 1;
}
When i printed the address of variable i in 2 functions (run in 2 threads), it is pointed to same address when the os run into loops. If function "count" run first, the address of i in function "count" is also use for the address of i in function "ask".The same situation if the function "ask" run first. I also checked that rsp and other registers are saved and loaded correctly.

Does anyone know about that? Thanks
I found the bug. I forgot to save rbp.
Thank you guys for your suggestions.
Have a good time everyone.
Post Reply