Today, I want to learn how to write a program using the multi-process!
now, I will show a very easy Ex for you to study!
here are the codes!
//parent process "process.c"
#include
#include
void main(int argc, char *argv[]){
int pid;
pid = fork();
printf("pid =%d\n", pid);
if(pid < 0){
printf("Error:Cant creat a new process!\n");
}
else if(pid == 0){
execlp("/home/daniel/Desktop/hw1/compare",NULL,NULL);
}
else{
wait(NULL);
printf("Succeed:Child Complete!\n");
}
return ;
}
//child process "compare.c"
#include
void main(void){
/*compare 3 and 5, which one is bigger*/
printf("You are running the child process!\n");
int i=3,j=5;
printf("i=%d ,j=%d.\n",i,j);
if(i > j){
printf("OK, %d > %d!\n",i,j);
}
else{
printf("OK, %d < %d!\n",i,j);
}
return ;
}
When you type the code into your vim, save it and using the gcc to compile it !
for Ex: gcc -W compare.c -o compare
after you compile the program , you will find you can run the process ,and it can call the campare program to run.
转载于:https://www.cnblogs.com/lzhiq/archive/2010/10/31/1865845.html