System() function in c
Do you want to execute a shell command in your program and check the status of the command? Do you want to create a child process and wait for that process to end? Do you want to check if the program has the access to the shell execution? You often have these requirements for your program and we have the solution.
C runtime library has an interesting function called system() to invoke commands in the shell. It takes commands and arguments in a string separated by spaces. We generally call fork() and then execxx() to spawn a new process. It does same and hopefully wraps those calls inside it. It also makes calling interface simpler and easy to use. We have next example to elaborate this.
System() function Description
The C library function int system(const char *command) is used to create a child process. A string containing the command name or program name followed by the arguments are passed to the function. This simple but effective function can be used to execute any command like pause, cls, echo etc.
System() - Declaration
Following is the declaration for system() function.
System() - Parameters
command − This is the C string containing the command.
System() - Return Value
This function can return these following values:
User can pass command string as NULL. This function will return a nonzero value if shell is available else a value of zero will be returned.
If a child process could not be created, or its status could not be retrieved, the return value is -1 and errno is set to indicate the error.
If a shell could not be executed in the child process, then the return value is as though the child shell terminated by calling _exit(2) with the status 127.
If all system calls succeed, then the return value is the termination status of the child shell used to execute command. (The termination status of a shell is the termination status of the last command it executes.)
System function - check if shell exection available or not.
Output:
Shell execution is supported.
System function in c example
Output:
$ ./redo ls -l redo.c -rw-r--r-- 1 user root 771 Jul 15 14:07 redo.c $ $ ./redo uname -a Linux home 2.6.18-164.10.1 #1 SMP Mon May 3 17:50:00 PDT 2010 x86_64 GNU/Linux $ $ ./redo ./redo At least one argument needed. Syntax: redo <args> $ ./redo ./redo abcd sh: abcd: command not found $
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.
Further readings
What is argc, argv in main function in c?[Explain]
Argc and argv are argument count and argument vector. Argc=number of arguments passed. Argv=Aarray of pointers, holds individual arguments. More..
Who calls main() function? What is C startup routine and assembly, explain?
Explains how control comes from C startup routine and assembly code to main routine. Setting up argc argv, environment variables before main
How does shell pass paramaters to my C program? [details of argv and environ]
How shell passes arguments and environment? Argc=number of arguments. Argv=Aarray of pointers, environ=Array of environment srtings. More..
How to install cleanup handler during exit using atexit?
How to install cleanup handler during exit using atexit?