Linux Learning-1 process

Source: Internet
Author: User

In Linux, another program is launched inside a program to create a new process.1.This work can be done through the library function system.

#include <stdlib.h>

int system (const char *string);

The function of the system function is to run the command as a string parameter and wait for the command to complete. The execution of the command is like executing the following command in the shell:

$ sh-c String

If the shell cannot be started to run this command, the system function returns error code 127, and the other error returns-1.

System.c

#include <stdlib.h>#include<stdio.h>int  main () {    printf ("  Running Upper with system\n");    System ("./upper");    printf ("done.\n");    Exit (0);}

Compiling GCC system.c-o system

At execution time./system

You will find that the upper program is executed.

2. Create the process bottom interface exec.

The EXEC series function consists of a set of related functions that differ in how the process is started and how the program parameters are expressed. The EXEC function can replace the current process with a new process. You can use the EXEC function to switch the execution of a program from one program to another.

The EXEC function is more efficient than the system function because the original program is no longer running after the new program starts.

#include <unistd.h>

int execl (XXX);

int EXECLP (XXX);

int execle (XXX);

int execv (XXX);

int EXECVP (XXX);

int execve (XXX);

Or take the upper program as an example:

Exe_up.c

 #include <stdio.h> #include  <unistd.h>  #include  <stdlib.h>int   main () {printf (  " runing    With execlp\n   " ); EXECLP (  " ./upper  " ,  " /upper   "   " down   "     0    

GCC Exe_up.c-o exe_up

./exe_up

Ps:

Upper.c

#include <stdio.h>#include<stdlib.h>#include<ctype.h>int  main () {    int  ch;      while (EOF! = (ch=getchar ())    ) {        Putchar (toupper (CH));    }    Exit (0);    }

GCC Upper.c-o Upper

All of the above is from the fourth edition of Linux programming.

Linux Learning-1 process

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.