Help needed with Task-04 in riot-os tutorial

Hello, I needed help understanding a problem that I am facing when trying to complete task-04 in the riot-os tutorial posted on github(https://github.com/RIOT-OS/Tutorials/tree/master/task-04) If I just create a thread that print time every 2 seconds everything works as expected. see picture. 1

When I run the shell(the riot os shell module), the time is not printed by the thread and I only see the shell(riot shell) marker. I am using Virtual Box with pepermint linux, and building the app(riot app) for native. (Sorry I dont know how to post code in the forms)

#include <stdio.h>

#include "shell.h" // comes with riot
#include "thread.h"
#include "xtimer.h"

static char stack[THREAD_STACKSIZE_MAIN];
void *print_time(void *arg)
{
  uint32_t micros=0;
  (void)arg;
  while(1)
  {
    micros = xtimer_now_usec();
    printf("Current Time (us) = %d\r\n",micros);
    xtimer_sleep(2);
  }
  return NULL;
}
// unable to print if the shell is running..
int main(void)
{
   //char line_buf[SHELL_DEFAULT_BUFSIZE];
   puts("This is Task-04\r\n");
   printf("Thread Stack Size main=%d\r\n",THREAD_STACKSIZE_MAIN);
   printf("Thread priority main=%d\r\n",THREAD_PRIORITY_MAIN);
   printf("Thread priority min=%d\r\n",THREAD_PRIORITY_MIN);
   printf("Thread priority idle=%d\r\n",THREAD_PRIORITY_IDLE);
   //shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
   // create thread
   thread_create(stack, sizeof(stack),
                       THREAD_PRIORITY_MAIN-2,
                       THREAD_CREATE_STACKTEST,
                       print_time,
                       NULL, "print_time");
   return 0;
}
# This is our app name
APPLICATION = task04

# define our default board
BOARD ?= native # arduino-uno

# RIOT base diractory absolute path
RIOTBASE ?= $(CURDIR)/../RIOT

# commit it is final
CFLAGS += -DDEVELHELP
# change to 0 in final
QUIET += 1
# Modules to includes

USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
# use timer
USEMODULE += xtimer
include $(RIOTBASE)/Makefile.include

:neutral_face:

Welcome @Magamanny!

I reformated your original post to be properly formated. It works the same as in Github Flavored Markdown.

shell_run() only returns when the shell is exited, so your thread effectively never is run, as shell_run() did not return yet in your case. If you call shell_run() after thread_create() it should work.

Yes, It works now, Thank you. You said that ‘shell only returns when shell is exited’, how can one exit the shell(riot shell)?. I tried ‘exit and quit’ command it gives command not found. Also, can I run the shell as a thread?

You can exit a shell with Ctrl+D, however, shell_run() is an alias for shell_run_forever() so exiting the shell will just start a new instance. If you want to be able to exit the shell for good using Ctrl+D, use shell_run_once().

Yes, the thread should be run with a lower priority (read “niceness”), otherwise it might never get to read anything. However, just to use the main thread then to start another thread after you started your shell in a different thread, seems a bit of a waste of the main thread’s stack memory (which will still be allocated and which you can’t free, once the main function and thus the main thread exits). Better use the main for the stack and move the thread creation before that.