Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12747 Discussions

Has anyone else had problems with POSIX threads?

Altera_Forum
Honored Contributor II
3,757 Views

Hi 

 

I have made an app with 2 POSIX threads for my linux kernel(2.6 microtronix). When I establish my 2 pthread_t varibles the kernel simply reboots. The uClibC has built in support for POSIX threads, so I don't really understand why it has problems with establishing these varibles(I don't even create the threads). 

 

Regards 

GreateWhite.DK
0 Kudos
23 Replies
Altera_Forum
Honored Contributor II
1,496 Views

Think that I might be having some problems with my memory control in the kernel. So it's not a POSIX thing. I have the same problem if I establish a array of 1000 int's. 

 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite.DK

 

I am not sure where you put the 4k array. If it is on the stack, you have to make sure your stack is big enough (you can use flthdr to check the size of the stack for an application).  

 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi wentao 

 

Thx for your answer. I have tried making my stack for my app 12K big. I used the "elf2flt -s 12000" command. I could not find the "flthdr" command that you mentioned in the elf tools. 

The kernel still reboots when I try to start my app. The stacksize I changed was the APP's and not the kernel's. Should it be the kernels(if yes how can this be done)??? 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

I am not sure where you put the 4k array[/b] 

--- Quote End ---  

 

 

Can/should I control this myself? 

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

GreateWhite.DK

 

12k is not actually big (depending how complex your application is). Can you try 32k? 

 

The size of the kernel stack is fixed (2 pages, around 8k), and I dont think it is the cause of your problem. 

 

flthdr is a utility to check the flat header information generated by elf2flt. Since you know how to set the stack size, you don&#39;t have to find it. 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

Can/should I control this myself?[/b] 

--- Quote End ---  

 

if you declare this array as a global or static variable, it is on the heap, not on the stack. but if you declare it as a automatic variable, it is on the stack.  

 

Regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi wentao 

 

I have now tried with 32K. That did not do the trick sadly.  

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

if you declare this array as a global or static variable, it is on the heap, not on the stack. but if you declare it as a automatic variable, it is on the stack.[/b] 

--- Quote End ---  

 

 

I declare it as a variable.  

 

 

Best regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite.DK

 

It sounds like my guess is not true. Let me try to understand the problem: your application restarts the system with the big array somewhere, but will not once the array is removed. Is this true?  

 

Still don&#39;t know where your array is: <div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

I declare it as a variable.[/b] 

--- Quote End ---  

It is global, static or automatic? But this is not important now. 

 

Best regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Here is my code then you can see it all. 

 

#include <stdio.h># include <unistd.h># include <stdlib.h># include <pthread.h> void *print_message_function( void *ptr ); int main() {      printf("NIOS II says hello\n");    int *arrayBuffer;      for(int x=0; x < 10; x++)    {      arrayBuffer = new int;      printf("*\n");        }     //pthread_t tmp;     //pthread_t thread2;         char *message1 = "Thread 1";  printf("2\n");     char *message2 = "Thread 2";       int  iret1, iret2;     printf("#\n");     /* Create independant threads each of which will execute function */     //iret1 = pthread_create( &tmp, NULL, print_message_function, (void*) message1);     //iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);     /* Wait till threads are complete before main continues. Unless we  */     /* wait we run the risk of executing an exit which will terminate   */     /* the process and all threads before the threads have completed.   */     //pthread_join( thread1, NULL);     //pthread_join( thread2, NULL);     printf("Thread 1 returns: %d\n",iret1);     printf("Thread 2 returns: %d\n",iret2);     return 1; } void *print_message_function( void *ptr ) {     printf("***\n");     char *message;     message = (char *) ptr;     while(1)     {       printf("%s \n", message);       usleep(1000000);     } } 

 

When I create the arrayBuffer the kernel panics. The IDE does not allow debugging of this app since it&#39;s a app in the filesystem of the kernel. Normally I would just debug it I find the error this way. 

If I don&#39;t create the array the kernel runs just fine. 

 

Hope that this clarifies your question. 

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite.DK

 

Now I am interested to see how you compiled this program. I don&#39;t think you can use "new" here: 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

for(int x=0; x < 10; x++) 

  arrayBuffer[x] = new int[10]; 

  printf("*\n");     

}[/b] 

--- Quote End ---  

 

 

I would do it this way: 

int x; 

for(x=0; x < 10; x++) 

arrayBuffer[x] = malloc (sizeof(int) * 10); 

printf("*\n");  

 

Since the memory are malloced from the heap, the problem is not a stack size issue. I think "new" caused your problem.  

 

Regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hey GreatWhite, 

 

Actually, the IDE does provide rudimentary debugging. Take a peek at the Reference Guide included with the Nios II Linux Distribution. It wasn&#39;t easy coaxing the IDE debugger to work with Linux apps so the process isn&#39;t perfect but it&#39;s a start. 

 

If the Nios II IDE starts behaving oddly while debugging Linux applications, let me know, it is still possible to debug using Insight or the GDB commandline as well.
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi Wentao, ken and others 

 

Wentao : 

 

Have tried with malloc same old problem still. 

 

Something new I have noticed is when I terminate my nios2-terminal app the kernel boots just fine(I think). I say this because the LEDs begins the binary count. The second I connect to the board again the kernel reboots!!! The kernel just keeps on rebooting while I&#39;m connected. The second I then disconnect the binary count on the LEDs begins again. Very strange I think. 

 

Some ideas??? 

 

How do I change the microtronix linux kernel so that it uses the serial IF as printf instead of the JTAG. 

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite.DK

 

I compiled your app (with my change) and ran it on one of Altera&#39;s board. It worked fine. 

 

For the JTAG problem, it is new to me. If you take jtag as the console, you have to start the nios2-terminal on your PC to read from the jtag, so that the kernel won&#39;t get stuck during the booting. I don&#39;t know what you changed to make the kernel continue without nios2-terminal reading from the jtag. 

 

Anyway, for the time being, you can switch to uart as the console: 

 

Device drivers --> Character devices --> Serial drivers --> 

enable Nios serial support, and enable support for console on Nios UART. 

disbale altera jtag uart support. 

 

Regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi Wentao 

 

Thx for all your effort. When you ran the program did you do it on a Linux kernel like me??? I use the prebuilt linux project that ships with the Quartus II(linux_1c20.ptf) 

I use the default setup of the kernel(have only changed it from stratix to cyclone support). 

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite.DK

 

I was using a kernel locally built, but from the same source code. The problem you had with the jtag is still confusing for me. I am quite sure the kernel wont continue during the booting if nios2-terminal is not reading from it. Can you give me more information regarding this issue? maybe we can find some clue from it. 

 

BTW, when you are linking your app, which libraries did you use?  

 

Here is the Makefile I was using for your app: 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

include ../../Rules.mak 

 

all: test.flt test.gdb 

clean: 

rm -f *.[iods] core *.flt *.bin *.elf *.gdb[/b] 

--- Quote End ---  

 

 

Regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi again 

 

Do you have an Email adr I can use??? Perhaps you have messenger (under my profile my MSN name can be found). 

Then I can send you my hole project. That would perhaps be a more fast and easy way to do it? It is not easy for me to figure out what could be some important fact that you could use. Keep in mind I am quit new at this http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif  

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi Wentao and others 

 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

BTW, when you are linking your app, which libraries did you use?[/b] 

--- Quote End ---  

 

 

I use the ones that the microtronix tool include when the project is created(I guess the uClibc). It is a kernel built entirely from what the quickstart guide from microtronix describe. 

 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

I was using a kernel locally built, but from the same source code. The problem you had with the jtag is still confusing for me. I am quite sure the kernel wont continue during the booting if nios2-terminal is not reading from it. Can you give me more information regarding this issue? maybe we can find some clue from it.[/b] 

--- Quote End ---  

 

 

If I don&#39;t connect to the dev board at all during boot, the LEDs will not begin there binary count. BUT if I am connected for a second or so and then disconnect(the boot is not done), the kernel boots just fine and will begin the binary count. I think there must be some sort of write buffer in the JTAG interface. As long as this buffer is not full the program can keep running. But once the buffer is full it locks up the intire kernel.  

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite, 

 

Can you post your app Makefile here? Just want to make sure you are using uClibc. 

The JTAG problem you observed is normal. You can switch to UART as the console. 

 

Regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi GreateWhite.DK

 

I was meaning the application makefile. But it is too small to bear any information. Can you give me the source code of hello_world.c, I mean the one that can pass compiling without modification? The last one you gave me needed several modifications before it could be compiled. 

 

Regards, 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi Greatwhite 

 

After performing the modifications that Wentao provided to your application, I was able to compile and run your app under Nios II Linux for a Cyclone 1c12 device. 

 

I&#39;d like to give your pthread code a run for its money as well but your code seems to be missing some declarations for the the tmp, thread1, and thread2 variables. Since my knowledge of pthreads is limited, could you post an example that will compile? 

 

WRT to the JTAG console issues, are you running into similar problems using a UART console as Wentao suggested?
0 Kudos
Altera_Forum
Honored Contributor II
1,496 Views

Hi Ken/Wentao 

 

Here below is the code I use. 

I have noticed that the kernel also reboots if I call the func "pthread_attr_getstacksize(&attr, &stacksize);" 

The "pthread_attr_init(&attr);" called before the "pthread_attr_getstacksize" call apperently gets called correct.  

 

I have modified the "pthread_create" function a bit(have made alot of printf&#39;s). But all these printf&#39;s don&#39;t get printed(have usleep(1000000) after each printf so they have time to get printed) 

 

I am thinking this might be a memory problem. Perhaps something is not allocated as it should be, but I am not sure. 

 

 

#include <stdio.h># include <stdlib.h># include <pthread.h># include <unistd.h> void *print_message_function( void *ptr ) {     char *message;     message = (char *) ptr;     printf("%s \n", message);     return (NULL); } int main() {     pthread_t thread1, thread2;     pthread_attr_t attr;     size_t stacksize;     char *message1 = "Thread 1";     char *message2 = "Thread 2";     int  iret1, iret2;     //Will try to allocate some memory to see if this is possible     void **newstack;     printf("\n##################################\n");     printf("Will now allocate 8096 bytes\n");     newstack = (void **) malloc(8096);     printf("\n**********************************\n");     printf("8096 bytes allocated\n");     //Just give some time to the UART to printf the messages.     usleep(1000000);    /* Create independant threads each of which will execute function */         printf("Will now create threads!!!\n");     //Just give some time to the UART to printf the messages.     usleep(1000000);         //Attributes for the thread.     pthread_attr_init(&attr);     printf("1!\n");     //Just give some time to the UART to printf the messages.     usleep(1000000);     //Get the stacksize of the thread.    //pthread_attr_getstacksize(&attr,  &stacksize);    //printf("Stacksize = %d\n", stacksize);     //Just give some time to the UART to printf the messages.     usleep(1000000);     //Setting the stack size    //pthread_attr_setstacksize(&attr, 1000);     //Creating the thread.     pthread_create(&thread1, &attr, print_message_function, (void *)message1);         //iret1 = pthread_create( &thread1, NULL, print_message_function, NULL);     printf("Thread One created!!!\n");     //Just give some time to the UART to printf the messages.     usleep(1000000);     //Creating a thread with standard attributes "NULL"     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);             /* Wait till threads are complete before main continues. Unless we  */     /* wait we run the risk of executing an exit which will terminate   */     /* the process and all threads before the threads have completed.   */     pthread_join( thread1, NULL);     pthread_join( thread2, NULL);     printf("Thread 1 returns: %d\n",iret1);     printf("Thread 2 returns: %d\n",iret2);     exit(0);     return 1; } 

 

 

Regards 

GreateWhite.DK
0 Kudos
Altera_Forum
Honored Contributor II
1,435 Views

Hey Greatwhite 

 

I&#39;ve noticed the same problem on my end... it&#39;ll print the number 1 but won&#39;t go any further than that. I don&#39;t know how much more time I can sink into this at the moment but my current suggestion would be to try building a filesystem project with the sample-threads package. Alternatively, just upload the 7 executables (ex1..ex7) to your uClinux system. 

 

The sample-threads package contains 7 executables to test the pthread capabilities of uClibc (Wentao can correct me on this if I&#39;m wrong). If they run properly, you can take a peek at the source code for them in 

 

...altera/kits/nios2/examples/software/linux/apps/samples/thread 

 

If they don&#39;t run properly then there&#39;s a potential problem in your hardware or kernel... hopefully we can narrow this down a bit... 

 

Let me know how it goes
0 Kudos
Reply