Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Some programming languages have system() exec() calls where I can run commands. What exactly happens when I do that? If I run system("ls") Does it run a bash interpreter on the string? Does it make an operating system call? What happens when it's a program like top that modifies the terminal screen instead of constantly dumping?

I'm not really sure how to ask. Where can I learn more about how it works?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

This page describes it pretty well

http://pubs.opengroup.org/onlinepubs/009604499/functions/system.html

If command is a null pointer, the system() function shall determine whether the host environment has a command processor. If command is not a null pointer, the system() function shall pass the string pointed to by command to that command processor to be executed in an implementation-defined manner; this might then cause the program calling system() to behave in a non-conforming manner or to terminate.

So yes it does invoke a bash interpreter, providing that this is the command processor determined by the C standard library implementation on that system. A child process is created that is destroyed when the command has executed.

EDIT: This is specific to C, other languages may function differently but it's usually like this

share|improve this answer
1  
Many languages will have a call that maps directly to this function (since many runtimes/interpreters are written in C), but they may also offer variations that work a little bit differently. –  Jeremy Aug 26 '12 at 22:57

These function calls can be implemented differently depending on what language you're using. However, in general, what they'll do is fork a new process running a shell and then run your command in that. Once the process completes, it terminates and your program will receive whatever it is that the language defines the return value to be (usually it's either a return code, or the standard output of the program that was run).

Since it forks a new process to run the command, its behavior will not affect the runtime environment of your own program.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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