System call EXECVE ()

Source: Internet
Author: User

EXECVE () System call Learning

First, a small program, its function is to print the parent-child process number, all parameters passed in and two environment variables (if any):

Process.c

#include <stdio.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>intMainintargcChar*argv[],Char*env[]) {    inti;
Char *p; printf ("\n=========i am a process image!============\n"); printf ("\nmy pid =%d, Parentpid =%d\n", Getpid (), Getppid ()); for(i=0; i< argc; i++) printf ("\nargv[%d]:%s\n", I, argv[i]); P= Getenv ("ENV1"); if(P! =NULL) printf ("\nenv1 =%s\n", p); P= Getenv ("env2"); if(P! =NULL) printf ("\nenv2 =%s\n\n", p); printf ("\n============= Process =================\n\n"); Sleep (atoi (argv[1])); return About;}

Another piece of code MYEXEC.C is as follows:

#include <stdio.h>#include<errno.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#defineERROR (flag)if(flag) {printf ("%d:", __line__);                Fflush (stdout); Perror ("Error");            Exit (errno); }intMain () {intRET =0; Char*argv[] = {" One"," A", NULL}; Char*env[] = {"env1=1111111111111","env2=2222222222222", NULL}; printf ("\nthis is myexec file, PID was%d, Ppid is%d\n", Getpid (), Getppid ()); //ret = execl ("process", "one", "a", NULL);//ret = EXECLP ("process", "one", "a", NULL);//ret = execle ("process", "one", "a", null,null);//ret = EXECV ("process", argv);//ret = EXECVP ("process", argv);ret= Execve ("Process", argv,env); ERROR (ret== -1); return 0;}

After "Make process myexec" is executed, the process and myexec two executables are generated under the current directory. The results of the implementation are as follows:

The PORCESS.C code prints out the incoming parameters and the environment variable number of the EXECVE () system call when the MYEXEC.C code executes.

--------------------------------------------------------------------------------------------------------------- ----------------------------------

Transform the MYEXEC.C into the following code:

#include <stdio.h>#include<sys/wait.h>#include<errno.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#defineERROR (flag)if(flag) {printf ("%d:", __line__);                Fflush (stdout); Perror ("Error");            Exit (errno); }intMainintargcChar*argv[],Char*env[])    {pid_t pid; PID=Fork (); //pid = Vfork ();ERROR (PID== -1); if(PID = =0) {printf ("\nchild process is running\n"); printf ("\nmy pid =%d, parent PID =%d\n", Getpid (), Getppid ()); Char*arglist[] = {"Sleep","3", NULL}; EXECVP ("./process", arglist); printf ("process never go to here!\n"); _exit (0); }    intStat;    pid_t Child_pid; Child_pid= Wait (&stat); printf ("Child Process has exited, PID =%d\n\n", Child_pid); if(wifexited (STAT)) printf ("Child exited with code%d\n\n", Wexitstatus (stat)); Elseprintf ("Child Exit abnormally\n\n"); return 0;}

After you perform the make myexec process, the results are as follows:

The subprocess executes the process through EXECVP (). The parent process obtains the return value of the child process number and its (normal) exit run time (via Wexitstayus ()) through Wait ().

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------

A way to run terminal commands through C code:

Mysystem,c

#include <stdio.h><stdlib.h><time.h><sys/stat.h>  <unistd.h><sys/types.h><errno.h>int main (intChar *argv[]) {    system ("ls--color=tty");     return 0 ;}

Runs after the link is compiled and performs the same effect as running the "LS--color=tty" command at the terminal.

System call EXECVE ()

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.