Role and meaning analysis of system of 'pause' in c++

  • 2020-06-03 07:42:53
  • OfStack

Simply put, it means to pause, which is typically used in LINUX programming to wait for a signal to be received before it can be rerun.

During C/C++ programming, when the program is running to see the output effect, the window will flash 1 and then close.

1 in the C language by adding getchar();

Add system("pause") before return in C++ 1; So you can see the output, pause will output" press any key to continue ... ".

The system function prototype is int system(char *command) On the windows platform is the DOS command, and on the linux platform is the shell command

Detailed introduction of the system () function under Linux

(Execute the shell command)
Correlation function
fork, execve, waitpid, popen
Header file
# include < stdlib.h >
Define a function
int system(const char * string);
Function description
system() calls fork() to produce a child process that calls /bin/ sh-ES59en string to execute the command represented by the parameter string string, which then returns the original called process. During the call to system(), the SIGCHLD signal is suspended and the SIGINT and SIGQUIT signals are ignored.
The return value
If fork() fails, return -1: An error occurred
If exec() fails, Shell cannot be executed and the return value is equivalent to Shell executing exit(127)
The termination status of the child Shell is returned on success
If system() fails on a call to /bin/sh, 127 is returned, and -1 for other reasons of failure. Returns a non-zero value if the parameter string is a null pointer (NULL) > . If the system() call is successful, the return value after executing the shell command is returned, but it is also possible that this return value is 127 returned on the failure of the system() call /bin/sh call, so it is a good idea to check errno again to confirm that the execution was successful.

Additional instructions

Do not use system() when writing programs with SUID/SGID permissions. system() will inherit environment variables, which may cause system security problems. sample

  #i nclude<stdlib.h>
  system( " ls -al /etc/passwd /etc/shadow " );

Execution Results:

 -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
  -r--------- 1 root root 572 Sep 2 15 :34 /etc/shado

Example 2:

  char tmp[];
  sprintf(tmp,"/bin/mount -t vfat %s /mnt/usb",dev);

Including dev is/dev sda1 System and exec distinction

Both system() and exec() can execute out-of-process commands. system creates a new process on top of the original process, but exec overwrites the original process with the new process (command)

2. Both system() and exec() can generate a return value. The return value of system does not affect the original process, but the return value of exec does

Related articles: