gpt4 book ai didi

C程序中的chdir系统调用

转载 作者:太空宇宙 更新时间:2023-11-04 10:45:51 26 4
gpt4 key购买 nike

<分区>

程序:

    #include<stdio.h>
#include<unistd.h>
int main()
{
char s[100];
printf("%s\n",getcwd(s,100));
chdir("..");
printf("%s\n",getcwd(s,100));
return 0;
}

输出:

    $ ./a.out 
/home/guest
/home
$

上面的程序改变了一个进程的工作目录。但是,它不会改变当前 shell 的工作目录。因为程序在shell中执行时,shell遵循fork on exec机制。因此,它不会影响当前的 shell。

有没有有什么方法可以通过这些程序更改 shell 的当前工作目录,例如 shell 使用的内置(cd、echo)命令?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com