Page 1 of 1
[Multitasking] 2 threads use same address for a variable.
Posted: Mon Sep 26, 2022 12:35 pm
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
Re: [Multitasking] 2 threads use same address for a variable
Posted: Mon Sep 26, 2022 1:12 pm
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.
Re: [Multitasking] 2 threads use same address for a variable
Posted: Mon Sep 26, 2022 1:38 pm
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.
Re: [Multitasking] 2 threads use same address for a variable
Posted: Mon Sep 26, 2022 9:00 pm
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
Re: [Multitasking] 2 threads use same address for a variable
Posted: Mon Sep 26, 2022 9:09 pm
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.