OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:23 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [Multitasking] 2 threads use same address for a variable.
PostPosted: Mon Sep 26, 2022 12:35 pm 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
Hi all,

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

Quote:
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


Top
 Profile  
 
 Post subject: Re: [Multitasking] 2 threads use same address for a variable
PostPosted: Mon Sep 26, 2022 1:12 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
Did you try using a debugger to check the parameters being passed to printf()? Perhaps the actual problem is that printf() isn't reentrant.


Top
 Profile  
 
 Post subject: Re: [Multitasking] 2 threads use same address for a variable
PostPosted: Mon Sep 26, 2022 1:38 pm 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 402
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.


Top
 Profile  
 
 Post subject: Re: [Multitasking] 2 threads use same address for a variable
PostPosted: Mon Sep 26, 2022 9:00 pm 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
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


Top
 Profile  
 
 Post subject: Re: [Multitasking] 2 threads use same address for a variable
PostPosted: Mon Sep 26, 2022 9:09 pm 
Offline

Joined: Wed Sep 14, 2022 12:42 pm
Posts: 10
zungnguyen wrote:
Hi all,

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

Quote:
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 73 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group