0

I'm trying to implement a subshell in C. I already have a normal shell, which looks like this:

int status;
pid = fork();
if (pid < 0) {
    exit(1);
}
else if (pid == 0) {
    if (execvp(node->command.program, node->command.argv) < 0) {
        exit(1);
    }
}
else {
    waitpid(pid, &status, 0);
}

I'm trying to figure out how a subshell works, and how it differs from the code above.

Thanks in advance.

Steven
  • 1,123
  • 5
  • 14
  • 31
  • 3
    What is the question? Subshell is the same shell when invoked from another shell. It's not any different from any other executable. – Eugene Sh. Apr 08 '15 at 19:23
  • ``fork()`` is pretty much not in use anymore these days.... ... ... – BitTickler Apr 08 '15 at 19:25
  • @user2225104 What do you mean? The `fork` system call is not in use, but the library function is still there, but implemented differently. – Eugene Sh. Apr 08 '15 at 19:30
  • @EugeneSh. fork() was mainly useful in times when Unix V had no notion of threads. Using it today for new code is a bit on the anachronistic side. (1970 retro style). – BitTickler Apr 08 '15 at 19:37
  • @user2225104 How would you spawn a child process? – Eugene Sh. Apr 08 '15 at 19:41
  • @EugeneSh. I wouldn't. – BitTickler Apr 08 '15 at 20:42
  • @user2225104 So you are claiming that multiprocessing is outdated? Really? – Eugene Sh. Apr 08 '15 at 20:44
  • @EugeneSh. Yup, as in "same process NCores times". Other forms of multiprocessing, not but then you would use ``exec()`` and not ``fork()``. – BitTickler Apr 08 '15 at 20:58
  • @user2225104 I afraid I don't understand. Are you considering single (or limited) core platforms? – Eugene Sh. Apr 08 '15 at 21:03
  • @EugeneSh. The simple fact, that you can achieve the same in a cheaper fashion with N threads as you can with N-1 child processes + main process does it for me. Along with all the memory footprint involved (copy on write of global data, ...) and other implications coming with child processes. If you want parallel processing, threads will do. If you want a chain of responsibities (pipelined actors or however one wants to call it), using threads also produces better design. – BitTickler Apr 08 '15 at 21:07
  • @user2225104 That's all good if you are designing a fully independent system. But once you need to run some external executables requiring a private virtual memory space, how would you do this with threads? – Eugene Sh. Apr 08 '15 at 21:14

0 Answers0