[英]Why is this execlp() function not executing?
我正在嘗試分叉一些子進程,然后在每個子進程上使用 execlp() 來執行另一組代碼。 execlp() 似乎沒有執行。
我試過 execl() 和 execlp(),但不知道我寫錯了什么。
// create child procs that use execlp()
for (int i = 0; i < num; i++){
if ((pids[i] = fork()) < 0){
perror("fork");
abort();
} else if (pids[i] == 0){
// do child work here
execlp("./fileWriter", "./fileWriter", num_threads, NULL);
printf("got here in child proc\n");
exit(0);
}
}
我期待它執行一個單獨的文件,現在我剛剛寫了一個打印語句,所以我知道其他文件何時實際運行。 而不是那個,我得到“在子進程中到達這里”,我添加了它,所以我知道子進程何時跳過了 execlp() 命令。
聽起來你大部分時間都在那里,但我會給你我正在運行的版本:
char cNum[20];
// create child procs that use execlp()
for (ii = 0; ii < num; ii++)
{
sprintf(cNum,"%d", num_threads);
if ((pids[ii] = fork()) < 0){
perror("fork");
abort();
} else if (pids[ii] == 0){
// do child work here
ret=execlp("./fileWriter", "./fileWriter", cNum, (char *) NULL);
printf("got here in child proc: %d\n",ret);
perror("execlp");
exit(0);
}
}
execlp 必須是一個字符串(你得到了那個部分),如果你檢查錯誤返回以查看錯誤來自哪里,它是“有幫助的”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.