在 Unix 系统上创建子进程?

发布于 2024-08-26 19:41:36 字数 599 浏览 16 评论 0原文

我正在尝试在另一个进程中创建一个子进程。我正在用 C 语言编写这两个程序。首先,我编写一个虚拟进程,它将作为子进程。它所做的只是在屏幕上写一个字符串。它本身运作良好。然后我编写另一个程序作为父进程。但是,我无法实现它。我尝试同时使用 fork 和 execl 函数,但失败了。我还希望子进程在父进程终止之前不会终止。

父进程应该怎么写?

谢谢。

这是子进程的代码:

#include <stdio.h>

int main(void) {
  while(1) {
    printf("*");
    sleep(1);
  }
}

这是父进程的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void) {
  if (fork()) {
    while(1) {
      printf("-\n");
      sleep(5);
    }
  } else {
    execl("./", "dummy", (char *)0);
  }
}

I'm trying to create a child process in another process. I am writing both the programs in C language. First I write a dummy process which will be the child process. What it is doing is only to write a string on the screen. It works well on its own. Then I write another program which will be the parent process. However, I can't make it happen. I'm trying to use fork and execl functions together, but I fail. I also want the child process does not terminate until the parent process terminates.

How should I write the parent process?

Thanks.

Here is the code for the child process:

#include <stdio.h>

int main(void) {
  while(1) {
    printf("*");
    sleep(1);
  }
}

And here is the parent process:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void) {
  if (fork()) {
    while(1) {
      printf("-\n");
      sleep(5);
    }
  } else {
    execl("./", "dummy", (char *)0);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

征棹 2024-09-02 19:41:36

fork()系统调用可以返回三种不同的状态:失败(<0)、父进程(>0)或子进程(==0)。您必须正确测试返回值。

int pid = fork();

if (pid < 0) {
  /* handle error */
  perror("fork");
  exit(1);
} else if (pid > 0) {
  /* parent code */
} else {
  /* child code */
}

您的 execl() 系统调用是错误的。第一个参数是您要执行的程序的路径,“./”无效,它至少应该类似于“./dummy”。按照惯例,下一个参数是命令名称(执行的程序中的 argv[0]),它可能是第一个参数的重复。所以:

execl("./dummy", "dummy", NULL);

另外,请注意,子程序中的 printf("*") 语句可能会缓冲,并且您不会在终端上看到任何内容。您必须在末尾添加“\n”或调用 fflush(stdout) 来刷新标准输出。

The fork() system call may return three different statuses: failure (<0), parent process (>0) or child process (==0). You must test the return value properly.

int pid = fork();

if (pid < 0) {
  /* handle error */
  perror("fork");
  exit(1);
} else if (pid > 0) {
  /* parent code */
} else {
  /* child code */
}

Your execl() system call is wrong. The first argument is the path to the program you want to execute, "./" is not valid, it should be something like "./dummy" at least. The next argument is by convention the command name (argv[0] in the executed program), which may be a repetition of the first argument. So:

execl("./dummy", "dummy", NULL);

Also, note that the printf("*") statement in the child program will probably buffer and you won't see anything on the terminal. You must either add a "\n" to the end or call fflush(stdout) to flush the standard output.

一袭白衣梦中忆 2024-09-02 19:41:36

C中fork的基本使用

int PID = fork();

if( PID < 0 ) {
    //fail
    return PID;
}
else if( !PID ) {
    //child process
    return exec( prog, args );
} 
else {
    //parent process
    return 0;
}

Basic use of fork in C

int PID = fork();

if( PID < 0 ) {
    //fail
    return PID;
}
else if( !PID ) {
    //child process
    return exec( prog, args );
} 
else {
    //parent process
    return 0;
}
情话难免假 2024-09-02 19:41:36

无法强制子进程在完成后“不终止”(您仍然可以在父进程中等待以获取有关其如何终止的信息,但仅此而已)。除此之外,网络上有许多 fork/exec 示例,例如 这个,应该可以工作——为什么你不尝试一下,看看它是否按照你的意愿执行(在这种情况下,你只需要改变你想要的任何东西)在你自己的尝试中做了不同的事情)。如果它没有按预期工作(除了此 A 中第一句话的不可能;-),请编辑您的代码以添加有关代码的行为方式与您期望的不同的大量详细信息。

There is no way to force the child process to "not terminate" when it's done (you'll still be able in the parent to wait for it to get info on how it terminated, but that's about it). Apart from that, any of the many examples of fork/exec on the web, such as this one, should work -- why don't you try it and see if it performs as you wish (in which case you'll just need to change whatever you were doing differently in your own attempt). If it doesn't work as desired (except for the impossibility per the first sentence in this A;-), please edit your to add copious detail about how the code behaves differently than you expect it to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文