Linux Process Creation

Source: Internet
Author: User

One:system systems Call  <stdlib.h>int system (constchar *string ); The system function is passed to /bin/sh-C to execute the command specified by string. The string can contain options and parameters if/bin/sh is not found . The function returns 127 if another error is returned-1, a successful return of 0, but if string is null, returns a non-0 value system calls other processes through the shell to invoke other processes, the program process is not related to the process being called. 

#include <stdio.h><stdlib.h><string.h><errno.h>  <unistd.h><sys/types.h>int main (intChar * args[]) {    System ("ls-l");     return 0 ;}

Two:fork system call #include<unistd.h>pid_t fork (void), fork execution succeeds, return the PID of child process to parent process, and returns 0 to the child process, which means that the fork returns two times even if it is called only once. The new process that fork creates is the same copy as the parent process (except PID and Ppid). There is a difference between the parent and child processes that the child process does not inherit the time-out setting of the parent process (using the alarm call), the file lock created by the parent process, or the pending signal. However, the child process inherits the file descriptor of the parent process, the size of the memory space, the code you cannot expect the parent process to run before or after his child process, and his execution is unordered and asynchronous. The asynchronous behavior of fork means that you should not execute code that relies on the parent process in the child process, and vice versa. The fork call may fail because too many processes have been run on the system and have exceeded the maximum number of processes allowed to execute. Fork execution fails, returns 1 to the parent process , and does not create a child process.
The fork executes successfully, returns the PID of the child process to the parent process, and returns 0 to the child process.

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>intMainintArgChar*args[]) {pid_t child=Fork (); if(child==-1) {printf ("Operating system Error! \ n"); return-1; }    if(child==0)    {        //at this point in the child processprintf"Child is begining!\n"); Sleep ( $); printf ("Child is end!\n"); }Else    {        //at this time, in the parent process, child is greater than 0, and the value of children is the PID of the subprocessprintf"Parent is begining!\n"); Sleep ( $); printf ("Parent is end!\n"); }    /*See the parent-child process sharing a piece of code by observing the parent-child process under "/proc"*/    return 0;}

Linux Process Creation

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.