A brief introduction to the use of system () function and vfork () function in C language _c language

Source: Internet
Author: User
Tags error code

C language System () function: Execute shell command
header file:

 #include <stdlib.h>

To define a function:

int system (const char * string);

Function Description: System () invokes fork () to produce a child process that invokes the/BIN/SH-C string to execute the command represented by the parameter string string, which then returns the original invoked process. The SIGCHLD signal is temporarily shelved during the call to System () and the SIGINT and sigquit signals are ignored.

return value:
1. If system () fails while calling/bin/sh, it returns 127, and returns 1 for other failure reasons.
2, if the parameter string is a null pointer (null), then return a non-0 value.
3. If the system () call succeeds, the return value after executing the shell command is finally returned, but the return value may also be 127 of the return of the system () call/bin/sh failure, so it is best to check errno again to confirm execution success.

Additional note: Do not use System () when writing programs with Suid/sgid permissions, and System () inherits environment variables, which can cause problems with systems security through environment variables.

Example

#include <stdlib.h>
Main ()
{
 system ("Ls-al/etc/passwd/etc/shadow");
}

Perform:

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

C language Vfork () function: Creating a new process

Header file:

#include <unistd.h>

To define a function:

pid_t vfork (void);

Function Description:
Vfork () produces a new subprocess that replicates the data and stack space of the parent process and inherits the parent process's user code, group code, environment variables, open file code, working directory, and resource limits.

Linux uses Copy-on-write (COW) technology, and only if one of the processes tries to modify the space to replicate does it do a real copy, because the inherited information is replicated and does not mean the same memory space, so the child processes are not synchronized with the changes to these variables and the parent process.

In addition, child processes do not inherit file locks and unhandled signals from the parent process.

Note: Linux does not guarantee that a child process executes or executes later than the parent process, so it is necessary to be aware of deadlocks or competitive conditions when writing programs.

return value: If Vfork () succeeds, the parent process returns the newly established subprocess Code (PID) and returns 0 in the newly established subprocess. If Vfork fails, return 1 directly, and the reason for failure is in errno.

Error code:
1, Eagain: Not enough memory.
2, Enomem: Insufficient memory, unable to configure the core required data structure space.

Example

#include <unistd.h>
Main ()
{
 if (vfork () = = 0)
 {
  printf ("This being the Child process\n");
 }
 Else
 {
  printf ("This is the parent process\n");
 }

Perform:

This is the parent process

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.