Summarizing the usage of system () function in C language _c language

Source: Internet
Author: User

The system () function is powerful, and many people use it with little knowledge of the Linux version of the system function first:

Copy Code code as follows:

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>

int system (const char * cmdstring)
{
pid_t pid;
int status;


if (cmdstring = = NULL) {
return (1);
}


if ((PID = fork ()) <0) {
status =-1;
}

    else if (PID = 0) {
        execl ("/bin/sh", "sh", "-C", Cmdstring, (char *) 0);
       -exit (127);//child process Normal execution does not execute this statement
        }

else{
while (Waitpid (PID, &status, 0) < 0) {
if (errno!= einter) {
status =-1;
Break
}
}
}

return status;
}


Analysis of the principle of the estimate will be able to read:

Returns directly when the command accepted by system is null, otherwise fork a subprocess because fork is returned in two processes: both parent and child, to check that the returned Pid,fork returns 0 in the subprocess and returns the PID of the child process in the parent process. The parent process uses WAITPID to wait for the child process to end, and the subprocess calls Execl to start a program instead of itself, execl ("/bin/sh", "sh", "-C", Cmdstring, (char*) 0) is the calling shell, and the path to the shell is/ Bin/sh, the subsequent strings are arguments, and then the subprocess becomes a shell process, and the shell's arguments are cmdstring, which is the parameters accepted by system. The shell in Windows is the command, and presumably everyone is familiar with the shell's acceptance of the command.

Then explain the principle of fork: When a process a calls fork, the system kernel creates a new process B and copies the memory image of A to B's process space, because A and B are the same, so how do they know if they are a parent or a subprocess, and look at the return value of fork to know that It also says that fork returns 0 in the subprocess and returns the PID of the child process in the parent process.

The situation in Windows is similar, that is, EXECL changed a smelly long name, the parameter name also changed to see the people dizzy, I found in the MSDN prototype, to show you:

Copy Code code as follows:

HInstance ShellExecute (
HWND hwnd,
LPCTSTR Lpverb,
LPCTSTR Lpfile,
LPCTSTR Lpparameters,
LPCTSTR Lpdirectory,
INT nShowCmd
);

usage See below:
ShellExecute (NULL, "open", "C:\\a.reg", NULL, NULL, SW_SHOWNORMAL);

You might be surprised. There is a shellexecute in the lpdirectory,linux that passes the parameters of the parent process environment variable EXECL is not, because EXECL is the compiler's function (to some extent hides the specific system implementation), In Linux it will then produce a Linux system called EXECVE, prototype see below:
int Execve (const char * file,const char **argv,const char **envp);

When you see this, you will understand why system () accepts the environment variables of the parent process, but when you change the environment variable with system, the system returns the main function or not. The reason for this is that the implementation of the system can be seen through the creation of a new process, from my analysis you can see that there is no process communication between the parent process and the child process, and the child process cannot naturally change the environment variables of the parent process.

DOS commands can be executed using the system function.

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
Xiaoyu ()
{
Char *a;
int n=0;
FILE *f;
F=fopen ("File.bat", "w+"); * New Batch/*
if (f==null)
Exit (1);
A= "echo"; /*dos Command *
for (n=65;n<=90;n++)/* Uppercase a-z*/
fprintf (F, "%s%c\n", a,n);//* Using ASCII code output A-Z, write batch/
Fclose (f);
System ("File.bat");/* Run Batch/*
}
Main ()
{
Char *string;
Xiaoyu ();
string= "The system function of the Echo C language \ n";/* Output Chinese * *
System (string);
System ("pause");/* Program paused * *
}

C can use DOS command, later programming by calling DOS commands a lot of operation is much simpler.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.