为什么$$返回与父进程相同的ID? [英] Why is $$ returning the same id as the parent process?

查看:117
本文介绍了为什么$$返回与父进程相同的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Bash时遇到问题,我也不知道为什么.
在shell下,我输入:

I have problem with Bash, and I don't know why.
Under shell, I enter:

echo $$    ## print 2433
(echo $$)  ## also print 2433
(./getpid) ## print 2602

"getpid"是用于获取当前pid的C程序,例如:

"getpid" is a C program to get current pid, like:

   int main() {
    printf("%d", (int)getpid());
    return 0;
   }

让我感到困惑的是:

  1. 我认为(命令)"是一个子进程(对吗?),我认为其pid与它的父pid应该不同,但是它们是相同的,为什么...
  2. 当我使用程序在括号之间显示pid时,显示的pid不同,对吗?
  3. '$$'是否类似于宏?

你能帮我吗?

推荐答案

$$定义为在子shell中返回父级的进程ID;从手册页的特殊参数"下:

$$ is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":

$扩展为Shell的进程ID.在()子外壳程序中,它扩展为当前外壳程序的进程ID,而不是子外壳程序.

$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.

bash 4中,您可以使用BASHPID获取子进程的ID.

In bash 4, you can get the process ID of the child with BASHPID.

~ $ echo $$
17601
~ $ ( echo $$; echo $BASHPID )
17601
17634

这篇关于为什么$$返回与父进程相同的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆