简体   繁体   English

使用C语言更改终端目录

[英]change terminal directory using c language

i am trying to change the directory of my linux terminal using c language. 我正在尝试使用C语言更改Linux终端的目录。 the thing is i am using threads. 事情是我正在使用线程。 i have tried to use chdir() but its not working, n yes chdir() is also a process function. 我试图使用chdir(),但它不起作用,是的chdir()也是一个过程函数。 Actually the thing i am trying to implement is, i am trying to make a multi threaded program which compiles Linux kernel, and that is not possible without specifying directories( i have tried to do it without specifying directories but failed :) ) 实际上我要实现的是,我正在尝试制作一个多线程程序,该程序可以编译Linux内核,并且如果不指定目录就无法实现(我已经尝试了不指定目录但失败了:))

can anybody help me out with this issue? 有人可以帮我解决这个问题吗?

Thanks in advance :) 提前致谢 :)

You can't! 你不能! No process can change the working directory of another. 没有进程可以更改另一个的工作目录。 A process can only change its own WD. 一个进程只能更改自己的WD。 When you launch an external command such like your C program, then a new process is launched. 当您启动外部命令(例如C程序)时,将启动一个新进程。 The cd command of your shell is an internal one. Shell的cd命令是内部命令。

It isn't very clear what you are trying to achieve. 尚不清楚您要实现的目标。 It appears that somehow you wish to emulate the behaviour of a script (or the make tool) using a C program, that is to say having the C program you wrote launch new processes (using system or perhaps a combination of fork and exec* ). 看来您似乎希望以某种方式使用C程序来模拟脚本(或make工具)的行为,也就是说,使编写的C程序启动新进程(使用system或使用forkexec*的组合) 。 If that is the case, then what you actually want is to modify the environment variables of these processes for them to find the files they need. 如果真是这样,那么您真正想要的是修改这些进程的环境变量,以使它们找到所需的文件。 A forked process will inherit the environment of its parent, so all you need is to use getenv , putenv , setenv and unsetenv to retrieve and set the environment variables you want to add, update or remove; 分叉的进程将继承其父进程的环境,因此您需要使用getenvputenvsetenvunsetenv来检索和设置要添加,更新或删除的环境变量。 or you may use the specific execve , execvpe which let you specify exactly what environment should be available to the new program. 或者您可以使用特定的execveexecvpe ,它们使您可以确切指定新程序应使用的环境。

references: 引用:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM