闫刚 nshterm实现重定向原理

资源

nuttx_wqueue.md
nshterm实现重定向原理

代码分析

后台运行,肯定直接启动1个线程执行
$ nshterm /dev/ttyACM0 &

 重要代码分析
 nshterm_main(int argc, char *argv[])
      请问当前这里的fd是多少? 答案是3
	 fd = open(argv[1], O_RDWR);
	关闭标准输入,标准输入,标准输入
       close(0);
	close(1);
	close(2);
	
	关闭0,1,2, 通过dup2决定把fd2复制给0,1,2,同时0,1,2可以正常使用了
	dup2(fd, 0);
	dup2(fd, 1);
	dup2(fd, 2);
       
       进入nsh解析,创建了1个新的nsh控制台
	nsh_consolemain(0, NULL);
       
       关闭打开的当前描述符
	close(fd);

通过代码分析,可以有多个控制台.

nsh_initscript

#ifdef CONFIG_NSH_ROMFSETC
int nsh_initscript(FAR struct nsh_vtbl_s *vtbl)
{
  static bool initialized;
  bool already;
  int ret = OK;

  /* Atomic test and set of the initialized flag */

  sched_lock();
  already     = initialized;
  initialized = true;
  sched_unlock();

  /* If we have not already executed the init script, then do so now */
  //
  if (!already)
    {
      ret = nsh_script(vtbl, "init", NSH_INITPATH);
    }

  return ret;
}

通过node分配files

//分配1个文件,通过node进行分配
  fd = files_allocate(inode, oflags, 0, 0);
     -if (!list->fl_files[i].f_inode) //通过这个指针是否是空确定?
  if (fd < 0)
    {
      ret = EMFILE;
      goto errout_with_inode;
    }

//节点释放后,就不能访问了。


也就是dup2, 只有先的文件句柄了,而且

ret = _files_close(filep2);
  if (ret < 0)
    {
      /* An error occurred while closing the driver */

      goto errout_with_ret;
    }

  /* Increment the reference count on the contained inode */
//节点
  inode = filep1->f_inode;
  inode_addref(inode);

  /* Then clone the file structure */

  filep2->f_oflags = filep1->f_oflags;
  filep2->f_pos    = filep1->f_pos;
  filep2->f_inode  = inode;

//通过看出,dup(new, old)
相当于,new替代了old , 但是执行1个close操作, 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值