如何正确使用fork()和EXEC() [英] how to use correctly fork() and exec()

查看:407
本文介绍了如何正确使用fork()和EXEC()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code;

I have this code;

pid_t process;
process = fork();

if (process < 0){
   //fork error
   perror("fork");
   exit(EXIT_FAILURE);
}
if (process == 0){
   //i try here the execl
   execl ("process.c", "process" , n, NULL);
}
else {
   wait(NULL);
}

我不知道这是否使用叉()的EXEC()组合是正确的。当我尝试运行bash的我没有收到任何结果的程序,所以我想它可能是在code,这部分的问题。结果
谢谢你。

I don't know if this use of fork() and exec() combined is correct. When I try to run the program from the bash I do not receive any result, so I thought it could be a problem in this part of code.
Thanks.

推荐答案

一个问题是:

if (process = 0){

应该读

if (process == 0){

否则你的分配的零到过程,只调用 EXECL 如果结果不为零(即从不)。

Otherwise you're assigning zero to process and only calling execl if result is non-zero (i.e. never).

另外,你想给exec一种叫 process.c 。毫无疑问,人们可以有一个名为可执行 process.c 。然而,在 .C 结束常规名称给C源$ C ​​$ C文件。如果 process.c 的确是一个C文件,你需要编译并首先联系起来。

Also, you're trying to exec something called process.c. There's no doubt that one could have an executable called process.c. However, conventionally names ending in .c are given to C source code files. If process.c is indeed a C file, you need to compile and link it first.

一旦你建立可执行文件,你需要或者某个地方将它放在 $ PATH 或指定完整路径execle()。在许多UNIX环境中把它放置在当前目录是不够的。

Once you've built the executable, you need to either place it somewhere on $PATH or specify its full path to execle(). In many Unix environments placing it in the current directory won't be enough.

最后,目前还不清楚是什么 N execle()通话,但名称暗示了一个数字变量。您需要确保它是一个字符串,而不是,例如,一个整数。

Finally, it's unclear what n is in the execle() call, but the name hints at a numeric variable. You need to make sure that it's a string and not, for example, an integer.

这篇关于如何正确使用fork()和EXEC()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆