Changing directory in a pthread - pthreads

My question is :
How can i change the current directory in a pthread without changing the current directory in other pthreads, I found a solution which is using openat() function, but i didn't found any example explaining how it works.
Using chdir() changes the current directory in all pthreads in the process.
Thank you for any help.

The openat() method is an alternative to changing the current working directory. Instead of calling:
chdir("/new/working/directory");
open("some/relative/path", flags);
you instead use:
dirfd = open("/new/working/directory", O_RDONLY | O_CLOEXEC);
openat(dirfd, "some/relative/path", flags);
This is the POSIX-standard way to avoid changing the process-wide current working directory in a thread, but still work with relative paths.
There is a also a Linux-specific way to give the current thread its own current working directory, separate from the rest of the process - unshare(CLONE_FS); - but this is not portable.

Related

How to use chdir() in Maxima in a more flexible way?

I often put the following lines at the beginning of my Maxima files
load("operatingsystem")$
chdir("/Users/tilda/Documents/progs/maxima/spm/")$
to make sure that output created by other programs from within Maxima is written into the current working directory as indicated above.
It does its job, yet this method is rather inflexible as the user's home directory, i.e.
/Users/tilda
is hardcoded into the program. So, when I exchange these files, users have to manually edit and adjust the code to their $HOME. To fix this I changed the code to various forms of
load("operatingsystem")$
chdir("$HOME/Documents/progs/maxima/spm/")$
just to get (Lisp) error messages. After consulting the manual and some trial and error I came up with this:
(%i1) load("operatingsystem")$
getenv("HOME")$
chdir(%)$
chdir("Documents/progs/maxima/spm/")$
getcurrentdirectory();
which works fine. For other users, i.e. matthias, the current directory now indeed is in their path.
(%o5) /Users/matthias/Documents/progs/maxima/spm/
However, I wonder if there is a more elegant and shorter way to achieve this.
Any suggestions appreciated.
Cheers
Tilda

CFBundleGetFunctionPointerForName and dlsym return NULL for exported function

I have a fork of the JavaScriptCore framework, where I have added a function of my own, which is exported. The framework compiles just find. Running nm on the framework reveals that the function (JSContextCreateBacktrace_unsafe) is indeed exported:
Leo-Natans-Wix-MPB:JavaScriptCore.framework lnatan$ nm -gU JavaScriptCore.framework/JavaScriptCore | grep JSContextCreateBacktrace
00000000004cb860 T _JSContextCreateBacktrace
00000000004cba10 T _JSContextCreateBacktrace_unsafe
However, I am unable to obtain the pointer of that function using CFBundleGetFunctionPointerForName or dlsym; both return NULL. At first, I used dlopen to open my framework, then tried using CFBundleCreate and then CFBundleGetFunctionPointerForName but that also returns NULL.
What could cause this?
Update
Something fishy is going on. I renamed one of the JSC functions, and nm reflects this. However, dlsym is still able to find the function with the original name, rather than the renamed.
It's hard to track this down since it's highly dependent on your specific environment and circumstances, but it is very likely you're running into this issue because the system image has already been loaded and you haven't changed the name of the framework.
If you look at the source code for dlopen in dyld/dyldAPIS.cpp:1458, you'll notice the context passed to dyld is configured with matchByInstallName = true. This context is then passed to load which executes the various stages necessary for image loading. There are a few phases worth noting:
loadPhase2 in dyld/dyld.cpp:2896 extracts the ending of the framework path and searches for it in the search path
loadPhase5check in dyld/dyld:2712 iterates over all loaded images and determines if any of them have a matching install name, and if one does, it returns that instead of loading a new one.
loadPhase5load in dyld/dyld:2601 finally loads the image if it wasn't loaded/found by any earlier steps. (It's worth noting loadPhase5check is executed first, since image loading is a two pass process.)
Given all of the above, I'd try renaming your framework to something besides JavaScriptCore.framework. Depending on the install name of both the system framework and your framework, I'd also recommend changing the install name. (There are plenty of blog articles and StackOverflow posts that document how to do this using install_name_tool -id.)

How to change the current folder on Delphi?

How to change process current folder on Delphi?
Both ways work: ChDir or SetCurrentDir. They both make the same Windows API call: SetCurrentDirectory.
If you use ChDir, an exception will be raised (in $I+ mode_) if the directory doesn't exist. So you'd want to handle the exception in that case (using try and except).
If you use SetCurrentDir, it will return false if the directory doesn't exist, and true if it was successful.
Given that SetCurrentDir is newer and ChDir is quite old, legacy (from DOS days), the former is preferred and easy to use as well.
Call the SetCurrentDir function from the SysUtils unit.
Note that the current directory is not a system property, it is a property of the process. Each process has its own current directory.

directory path question

What is the difference between:
include("./somepath/class.php");
and
include("somepath/class.php");
There shouldn't be any difference, directory wise, since the former assumes relative directory structure. The only potential pitfall could be if "somepath" were actually a command to be run - some users expect to type a command for a local script file and assume it should run, when you actually have to run "./somepath" to invoke it. This, of course, only pertains to commands not on your $PATH.
I don't see any difference. "." means current directory.
. refers to the current working directory.
Sometimes you want to specify explicitly that what you want is in the current working directory, and that it is not from something in your path variable for example.
For example in PHP "somepath/somefile" is appended after paths specified in include_dir (for example: /home/var/; /home/bin/ ....) directive.
The second variant is more specific it says: search in current directory!

Change directory using os:cmd/1

I am trying to change the directory in the command line from a gen_server using
os:cmd("cd d:\temp").
but nothing happense, the return is just an empty list and I remain in the same directory.
any ideas?
Try using file:set_cwd(Dir) to change your current dir.
cmd() runs a sub-shell, which you're telling to change directory, then the sub-shell exits, having changed nothing about its parent process's environment.
You want to use cd() instead, if you're in the shell, or file:set_cwd() at runtime within an Erlang program.
Another option, if you want to run another program and have its working directory be different from the one Erlang is using is to pass the {cd, Dir} tuple to open_port().

Resources