11

If I compile a program using gcc, and try to execute it from the bash shell, what is the exact sequence of steps followed by bash to execute it ?

I know fork(), execve(), loader, dynamic linker (and other things) are involved, but can someone give an exact sequence of steps and some suitable reading reference ?

Edit:

From the answers, it seems the question could imply many possibilities. I want to narrow down to a simple case:

(test.c just prints hello world)

$ gcc test.c -o test
$ ./test

What will be the steps in the above case (./test), specifically relating bash starting program in some child process, doing loading, linking etc. ?

  • 4
    I invite you to read lwn.net/Articles/630727 – cuonglm Aug 27 '15 at 1:45
  • 3
    Why not try `strace bash -c 'test' ? – Sergiy Kolodyazhnyy Aug 27 '15 at 7:30
  • 2
    It seems like a decent Operating Systems textbook would be a good resource for the OP. Trying to learn how operating systems work by asking individual questions like this is not likely to be a productive process. – Barmar Sep 2 '15 at 19:36
  • It would be useful to see a minimal example of a shell: brennan.io/2015/01/16/write-a-shell-in-c – jinawee Dec 13 '18 at 12:31
5

Well, the exact sequence may vary, as there might be a shell alias or function that first gets expanded/interpreted before the actual program gets executed, and then differences for a qualified filename (/usr/libexec/foo) versus something that will be looked for through all the directories of the PATH environment variable (just foo). Also, the details of the execution may complicate matters, as foo | bar | zot requires more work for the shell (some number of fork(2), dup(2), and, of course, pipe(2), among other system calls), while something like exec foo is much less work as the shell merely replaces itself with the new program (i.e., it doesn't fork). Also important are process groups (especially the foreground process group, all PIDs of which eat SIGINT when someone starts mashing on Ctrl+C, sessions, and whether the job is going to be run in the background, monitored (foo &) or background, ignored (foo & disown). I/O redirection details will also change things, e.g., if standard input is closed by the shell (foo <&-), or whether a file is opened as stdin (foo < blah).

strace or similar will be informative about the specific system calls made along this process, and there should be man pages for each of those calls. Suitable system level reading would be any number of chapters from Stevens's "Advanced Programming in the UNIX Environment" while a shell book (e.g., "From Bash to Z Shell") will cover the shell side of things in more detail.

  • I edited the question to narrow down to a simple case – Jake Aug 27 '15 at 1:17
1

Assuming a textbook example shell (for code clarity) that is already running (so the dynamic linker is done), the commands you mention will require the shell to make the following system calls:

  • read: gets the next command in this case gcc
  • fork: two process are needed, we assume the parent has pid 500 and the child for illustration.
  • the parent will call wait(501), meanwhile the child will call exec. At this point the shell is no longer running on pid 501. gcc makes lots of system calls including at a minimum open, close, read, write, chmod, fork, exec, wait and exit.
  • when gcc calls exit, wait will return, write is called to display the prompt and the process will repeat.

More complicated commands of course add more complication to this basic sequence. Two simpler examples of basic complications are basic io redirection where a open, close, dup sequence is inserted between the fork and the exec and background processes where the wait is skipped (and another wait is added to a sigchld handler).

  • Small addition: the question asks about loading and dynamic linking. All the code that is statically linked, i.e. actually included in the program file, is done by the kernel before the program is started. Dynamically loaded libraries, i.e. separate files, are handled by the program itself before starting main(). The code for this is automatically added by gcc. – Stig Hemmer Aug 27 '15 at 8:55
0

I suggest reading Section 8.4.6 Using fork and execve to Run Programs

on http://www.groupes.polymtl.ca/inf2610/documentation/ComputerSystemBook.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.