exec downloads the executable file and replaces it with the current sample program. As you rightly pointed out, cd not an executable file, but rather an embedded shell. Thus, the executable you want to run is the shell itself. This, of course, is what system() does for you, but if you want to be explicit, you can use exec :
execl("/bin/sh", "-c", "cd", (const char *)0);
Since this replaces your current process image, you should do this after fork() from the new process.
However, this entire procedure has absolutely no effect. If you want to change the directory in the current process, use chdir() .
source share