Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chdir() & multithreading in C

Is it possible to use chdir() or some other command to change the directory in a thread without affecting the cwd of the other threads ? I'm using pthread.h.

*I'm trying to write a server program that handles multiple client connections and requests. One of the available commands to the client is the 'cd' command.

like image 529
IrishDog Avatar asked Oct 27 '25 06:10

IrishDog


1 Answers

No, as mentioned by others the current working directory is a per-process property, not per-thread. You can "emulate" a per-thread (or per client, or however you structure your application) current working directory by storing a file descriptor for the "per-thread CWD" and the using the various *at() syscalls specified in POSIX 2008 (openat() etc.) to manipulate paths relative to that directory fd.

like image 191
janneb Avatar answered Oct 28 '25 20:10

janneb