The role and meaning of system (& quot; pause & quot;) in c ++, systempause

Source: Internet
Author: User

The role and meaning of system ("pause") in c ++, systempause

In short, the pause function is generally used in LINUX programming. It will run again only after receiving signals.

During C/C ++ programming, when you run the program to view the output result, a window will pop up and close.

In C, getchar () is usually added ();

In C ++, system ("pause") is usually added before return in the main function. In this way, you can see the output result clearly, pause will output "press any key to continue... ".

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

Introduction to the system () function in Linux (execute shell commands) related functions fork, execve, waitpid, and popen header file # include <stdlib. h> define the int system (const char * string) function. The function indicates that system () calls fork () to generate sub-processes, the sub-process calls/bin/sh-c string to execute the command represented by the string parameter. After the command is executed, the original called process is returned. The SIGCHLD signal is temporarily shelved during system () calls, while the SIGINT and SIGQUIT signals are ignored. Returned value: If fork () fails, return-1: an error occurs. If exec () fails, Shell cannot be executed. The returned value is equivalent to Shell executing exit (127) if the execution is successful, the termination status of the sub-Shell is returned. If system () fails to be called/bin/sh, 127 is returned. For other causes of failure,-1 is returned. If the string parameter is a NULL pointer, a non-zero value is returned. If system () is successfully called, the return value after the shell command is executed is returned. However, the returned value may also be 127 returned when system () fails to call/bin/sh, therefore, it is best to check errno again to confirm the execution is successful. Note: Do not use system () when writing programs with SUID/SGID permissions. system () inherits environment variables, which may cause system security problems. Example # I nclude <stdlib. h> main () {system ("ls-al/etc/passwd/etc/shadow");} execution result: -rw-r -- 1 root 705 Sep 3 13: 52/etc/passwd-r --------- 1 root 572 Sep 2 15: 34/etc/shado Example 2: char tmp []; sprintf (tmp, "/bin/mount-t vfat % s/mnt/usb", dev); system (tmp ); among them, dev is the difference between/dev/sda1.System and exec. 1. Both system () and exec () can execute commands outside the process. system opens up a new process on the original process, however, exec uses a new process (command) to overwrite the original process 2, system (), and exec (). The return value of system does not affect the original process, but the exec return value affects the original process reference blog:

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: 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.