exe等函式使用

wzm10455發表於2013-01-16
以下標誌這標籤數字的表示每一對執行方案
/*
 ============================================================================
 Name        : twoProcessAndExe.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(void) {
	pid_t pid1,pid2;
	pid1=fork();
	if(pid1>0)
	{
		pid2=fork();
		if(pid2>0)
		{
			wait(NULL);
			wait(NULL);

			puts("I am your father ");
			exit(0);
		}
		else if(pid2==0)
		{
			
		//5	char * argv[]={"ifconfig",NULL};
//			execvp("ls",argv);

			
//		  4  char *argv[]={"ifconfig",NULL};
//		    char *envp[]={"PATH=/bin",0};
//		    execve("/sbin/ifconfig",argv,envp);



		   // 3 .char *argv1[]={"ifconfig",NULL};
			//.execv("/sbin/ifconfig",argv1);

			//2.execlp("ifconfig","ifconfig",NULL);

			//1.execl("/sbin/ifconfig","ifconfig",NULL);//第一個引數必須要找到ifcofig的所在地,其它的就是命令的和null
			exit(0);
		}
		else
		{
			perror("child2 wrong");
		}

	}
	else if(pid1==0)
	{
//	5.	char * argv[] ={ "ls","-l","/usr",NULL};
//		execvp("ls",argv);
		
		//4
//		 char *argv[]={"ls","-al","/usr",NULL};
//		 char *envp[]={"PATH=/bin",0};
//		 execve("/bin/ls",argv,envp);

	  //	3.char *argv[]={"ls","-al","/usr",NULL};
	  // execv("/bin/ls",argv);

		//2.execlp("ls","ls","/usr",NULL);
		//1.execl("/bin/ls","ls","-al",NULL);
		exit(0);
	}
	else
	{
		perror("child1 error");
	}

	exit(0);
}

相關文章