The difference between using popen and system blocking processes

Source: Internet
Author: User
Both Popen and system can perform external commands. Popen is equivalent to creating a pipe first, then fork, closing one end of the pipe, executing exec, and returning a standard IO file pointer. System is equivalent to call the fork, exec,waitpid to execute the external command popen itself is not blocked, through the standard IO read (fread, etc.) to make it block;//If you read the file with Fread, it will not block if you use it to read the pipe, Socket connections, etc., may block.   The system itself is blocked. A recently written program that requires the process to proceed down after the calling external command has finished running. The first call to the Popen, and then just use the fgetc, make it blocked, but always blocked. The reason is that if the external command has a lot of output, that fgets in the first character of the output returned, not blocked, call fread, if size and nitems set is not large enough, is the same problem. For example, the external command to output 100 characters, the result size is sizeof (char), Nitems is 10, then when Fread read the ground 10 characters, it has satisfied the condition, and returned. The correct approach is to call system, because system will finally call Waitpid to wait for the child process to complete running.

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.