coreutils4.5.1 rmdir.c源码分析

刚才忘记了说rmdir这个子程序,其实它也很有意思的。它用到了多进程,fork(),神奇吧。
因为我是在ubuntu下读代码,设置好了tags,但ubuntu下能使用gvim,不方便拷代码,于是在windows下用gvim读代码,但没有设置好tags,于是代码不能跑到lib下,刚才设置好了,我的配置如下:
set tags=C:\\Users\\ubn\\Downloads\\coreutils-4.5.1\\src\\tags
set tags+=C:\\Users\\ubn\\Downloads\\coreutils-4.5.1\\lib\\tags
于是,在gvim下也可以跳了。
int
rmdir (dpath)
     char *dpath;
{
  pid_t cpid;
  int status;
  struct stat statbuf;

  if (stat (dpath, &statbuf) != 0)
    return -1;            /* errno already set */

  if (!S_ISDIR (statbuf.st_mode))
    {
      errno = ENOTDIR;
      return -1;
    }

  cpid = fork ();
  switch (cpid)
    {
    case -1:            /* cannot fork */
      return -1;        /* errno already set */

    case 0:            /* child process */
      execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
      _exit (1);

    default:            /* parent process */

      /* Wait for kid to finish.  */

      while (wait (&status) != cpid)
    /* Do nothing.  */ ;

      if (status)
    {

      /* /bin/rmdir failed.  */

      errno = EIO;
      return -1;
    }
      return 0;
    }
}
这段代码用了fork,新开一进程,返回0时,表示是子进程,在子进程是执行
      execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
      _exit (1);
在父进程中,等子进程删除完成。

      /* Wait for kid to finish.  */

      while (wait (&status) != cpid)
    /* Do nothing.  */ ;

真是神奇得不得了。作者为什么要用多进程呢?也许是怕删除文件夹费时间吧。类似于adjx,哈哈!!!    
 

猜你喜欢

转载自blog.csdn.net/woshiyilitongdouzi/article/details/85795887