static inline void sched_dupfiles(FAR struct task_tcb_s *tcb)
{
  /* The parent task is the one at the head of the ready-to-run list */

  FAR struct tcb_s *rtcb = (FAR struct tcb_s *)g_readytorun.head;
  FAR struct file *parent;
  FAR struct file *child;
  int i;

  DEBUGASSERT(tcb && tcb->cmn.group && rtcb->group);

  /* Duplicate the file descriptors.  This will be either all of the
   * file descriptors or just the first three (stdin, stdout, and stderr)
   * if CONFIG_FDCLONE_STDIO is defined.  NFSDS_TOCLONE is set
   * accordingly above.
   */

  /* Get pointers to the parent and child task file lists */

  parent = rtcb->group->tg_filelist.fl_files;
  child  = tcb->cmn.group->tg_filelist.fl_files;

  /* Check each file in the parent file list */

  for (i = 0; i < NFDS_TOCLONE; i++)
    {
      /* Check if this file is opened by the parent.  We can tell if
       * if the file is open because it contain a reference to a non-NULL
       * i-node structure.
       */

      if (parent[i].f_inode)
        {
          /* Yes... duplicate it for the child */

          (void)file_dup2(&parent[i], &child[i]);
        }
    }
}
int group_setupidlefiles(FAR struct task_tcb_s *tcb)
{
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
  FAR struct task_group_s *group = tcb->cmn.group;
#endif
#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
  int fd;
#endif

#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
  DEBUGASSERT(group);
#endif

#if CONFIG_NFILE_DESCRIPTORS > 0
  /* Initialize file descriptors for the TCB */

  files_initlist(&group->tg_filelist);
#endif

#if CONFIG_NSOCKET_DESCRIPTORS > 0
  /* Allocate socket descriptors for the TCB */

  net_initlist(&group->tg_socketlist);
#endif

  /* Open stdin, dup to get stdout and stderr. This should always
   * be the first file opened and, hence, should always get file
   * descriptor 0.
   */

#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
  fd = open("/dev/console", O_RDWR);
  if (fd == 0)
    {
      /* Successfully opened /dev/console as stdin (fd == 0) */

      (void)file_dup2(0, 1);
      (void)file_dup2(0, 2);
    }
  else
    {
      /* We failed to open /dev/console OR for some reason, we opened
       * it and got some file descriptor other than 0.
       */

      if (fd > 0)
        {
          slldbg("Open /dev/console fd: %d\n", fd);
          (void)close(fd);
        }
      else
        {
          slldbg("Failed to open /dev/console: %d\n", errno);
        }
      return -ENFILE;
    }
#endif

  /* Allocate file/socket streams for the TCB */

#if CONFIG_NFILE_STREAMS > 0
  return group_setupstreams(tcb);
#else
  return OK;
#endif
}
int sched_setupidlefiles(FAR _TCB *tcb)
{
#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
  int fd;
#endif

#if CONFIG_NFILE_DESCRIPTORS > 0
  /* Allocate file descriptors for the TCB */

  tcb->filelist = files_alloclist();
  if (!tcb->filelist)
    {
      return -ENOMEM;
    }
#endif /* CONFIG_NFILE_DESCRIPTORS */

#if CONFIG_NSOCKET_DESCRIPTORS > 0
  /* Allocate socket descriptors for the TCB */

  tcb->sockets = net_alloclist();
  if (!tcb->sockets)
    {
      return -ENOMEM;
    }
#endif /* CONFIG_NSOCKET_DESCRIPTORS */

#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE)
  /* Open stdin, dup to get stdout and stderr. This should always
   * be the first file opened and, hence, should always get file
   * descriptor 0.
   */

  fd = open("/dev/console", O_RDWR);
  if (fd == 0)
    {
      /* Successfully opened /dev/console as stdin (fd == 0) */

      (void)file_dup2(0, 1);
      (void)file_dup2(0, 2);
    }
  else
    {
      /* We failed to open /dev/console OR for some reason, we opened
       * it and got some file descriptor other than 0.
       */
  
      if (fd >- 0)
        {
          slldbg("Open /dev/console fd: %d\n", fd);
          (void)close(fd);
        }
      else
        {
          slldbg("Failed to open /dev/console: %d\n", errno);
        }
      return -ENFILE;
    }

#if CONFIG_NFILE_STREAMS > 0
  /* Allocate file strems for the TCB */

  return sched_setupstreams(tcb);
#else
  return OK;
#endif /* CONFIG_NFILE_STREAMS */
#else
  return OK;
#endif /* CONFIG_NFILE_DESCRIPTORS && CONFIG_DEV_CONSOLE */
}