Summary of usage of system () functions in C language (RPM)

Source: Internet
Author: User

The system () function is powerful, and many people use it to understand the principle of the Linux version of the system function of the source code:

1#include <sys/types.h>2#include <sys/wait.h>3#include <errno.h>4#include <unistd.h>5  6 intSystemConst Char*cmdstring)7 {8 pid_t pid;9     intstatus;Ten   One  A     if(Cmdstring = =NULL) {       -          return(1); -     } the   -  -     if(PID = fork ()) <0){ -Status =-1; +     } -   +     Else if(PID =0){ AEXECL ("/bin/sh","SH","- C", Cmdstring, (Char*)0); atExit127);//This statement is not executed if the child process is performing normally -     } -   -     Else{ -             while(Waitpid (PID, &status,0) <0){ -                 if(errno! =einter) { inStatus =-1; -                      Break; to                 } +             } -         } the   *         returnstatus; $}

Analysis of the principle of estimation can be read:

When the command accepted by system is NULL, it is returned directly, otherwise fork out a subprocess, because fork is returned in two processes: parent and child processes, here to check that the returned Pid,fork returns 0 in the child process, the PID of the child process is returned in the parent process, The parent process uses WAITPID to wait for the child process to end, the child process is called Execl to start a program instead of itself, execl ("/bin/sh", "sh", "-C", Cmdstring, (char*) 0) is the calling shell, the path of the shell is/ Bin/sh, the subsequent strings are arguments, and then the subprocess becomes a shell process, and the shell parameter is cmdstring, which is the parameter that the system accepts. The shell in Windows is the command, and presumably everyone is familiar with what the shell has done after accepting the command.

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

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

1 hinstance   ShellExecute (2               hwnd   hwnd,3               lpctstr   lpverb,4                 lpctstr   lpfile,5               lpctstr   lpparameters,6                lpctstr   7               INT   8    );   

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

You might wonder if there's a parameter in ShellExecute that passes the parent process environment variable Lpdirectory,linux EXECL is not, because EXECL is the compiler's function (to some extent, hides the specific system implementation), In Linux it will then generate a call to the Linux system Execve, the prototype is shown below:
int Execve (const char * file,const char **argv,const char **envp);

See here to understand why system () accepts the environment variables of the parent process, but after changing the environment variable with system, the system returns the main function or not. The reason from the implementation of the system can be seen, it is achieved by generating a new process, from my analysis can see that there is no process communication between the parent process and the child process, the child process naturally cannot change the parent process environment variables.

The system function is used to execute DOS instructions.

1#include <stdio.h>2#include <stdlib.h>3 4 Xiaoyu ()5 {6     Char*A;7     intn=0;8FILE *F;9F=fopen ("File.bat","w+");/*Create a new batch*/Ten     if(f==NULL) { OneExit1); A     } -  -A="Echo";/*DOS command*/ the      for(n= $; n<= -; n++) {/*uppercase A-Z*/ -fprintf (F,"%s%c\n", a,n);/*output A-Z with ASCII code to write batch processing*/ -     } - fclose (f); +System"File.bat");/*Run Batch processing*/ - } +  A intMainintargcCharargv[]) at { -     Char*string; -      - Xiaoyu (); -     string="the system function of the Echo C language \ n";/*Output Chinese*/ -Systemstring); inSystem"Pause");/*Program paused*/ -     return 0; to}

A DOS command can be used in C, and it is much easier to program later by calling DOS commands.

Go to: Summary of usage of system () functions in C language

Summary of usage of system () functions in C language (RPM)

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.