I'm trying to use the function execlp() to make a child processes run a specific code that I wrote, but I don't understand how the path works.
NOTE: I use export PATH=$PATH:.
in the terminal so I don't need to type /.ProgName
every time.
The first program is:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
main() {
pid_t childpid;
int i;
int nprocess = 3;
for (i = 0; i < nprocess; ++i) {
if ((childpid = fork()) < 0) {
perror("fork:");
exit(1);
}
if (childpid==0){
printf("I'm the son with ID= %ld\n",(long)getpid());
char *path = "exectest";
if ((execl(path,"0",NULL))<0) printf("\n error exec \n");
}
else
printf("I'm the father with ID= %ld",(long)getpid());
}
}
The second program to be called by the execlp is :
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
void main(){
printf("\n I'm using exec \n");
}
Both programs are in the same directory. The second program is named "exectest" but when I launch the first, I get the error message.
由于第一个程序启动可执行文件exectest
,因此应编译第二个文件exectest.c
并获取名为exectest
可执行文件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.