When you create a child process with fork (), you execute the same program as the parent process (but it is possible to execute a different code branch), and the child process often calls one of the exec functions to execute another program.
Process program substitution: Replace data and code, perform different logic, once replaced, parent, child process code, data Independent.
The main is to create the process, let the child process to execute the command.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/AE/wKioL1cHS6CTj07uAAA-DpdsfeI032.png "title=" Picture 20160408140649.png "alt=" Wkiol1chs6ctj07uaaa-dpdsfei032.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/AE/wKioL1cHS_nyXk9pAAAesB8ptQU482.png "title=" 808. PNG "alt=" Wkiol1chs_nyxk9paaaesb8ptqu482.png "/>
There are 6 main scheduling functions, the function name p indicates the file name (depending on the environment variable to see if there is a path), no p need to pass the file path, to execute a program must first find it.
With e means a custom environment variable, without e representing the environment variable inherited from the parent process.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys /types.h>int main (int argc,char* argv[]) { pid _t id; while (1) { printf ( "[lxj/home]$"); fflush (stdout); char cmd_str[1024]={0}; ssize_t size=read (0,cmd_str,sizeof (CMD_STR)-1); char cmd[32]={0}; char* my_argv[32]; memset (my_argv,0,sizeof (char*) *32); int length=strlen (cmd _STR) -2; // printf ("%s", Cmd_ STR); if (size!= -1) { int index=0; while (length >= 0) { while (length >= 0&&cmd_str[length]== ' ') { cmd_str[length--] = 0; } while (length >= 0 && cmd_str[length] != ' ') length--; my_argv[index++]=cmd_str+length+1; } my_argv[ index]=null; int i=0; index--; for (; i<index;++i,--index) { char* tmp=my_argv[i]; my_argv[i]=my_argv[index]; my_argv[index]=tmp; } if (strcmp (argv[0], "CD") ==0) { chdir (my_argv[1]); continue; } } id=fork (); if (id<0) perror ("fork"); else if (id==0) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECVP (MY_ARGV[0],MY_ARGV); Exit (1); } else if ( id>0) { pid_t waitpid=waitpid (id,null,0); } } return 0;}
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1761714
Write a simple shell command line