c fork,exec,getpid 问题

发布于 2024-11-11 14:27:31 字数 1085 浏览 8 评论 0原文

我是c语言和Linux的新手。我有一个与 fork()、getpid() 和 exec() 函数相关的问题。 我使用 fork() 调用编写了 ac 程序,我的程序代码如下” 代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

void fun()
{
  printf("\n this is trial for child process");
}

int main (int argc, char const *argv[])
{
  int i,status,pid,t;

  if(pid=fork()<0)
  { 
    printf("\nfailed to create the process\n");
  }
  if(pid=fork()==0)
  {
    printf("\n the child process is created");
    fun();
    exit(1);
  }
  while(wait(&status)!=pid);
  return 0;
}

该程序的输出如下:

子进程已创建

这是子进程的试用

子进程已创建

这是子进程的试用

现在我的问题如下:

  1. 为什么程序的输出显示相同的内容两次?输出应该是“子进程已创建,这是子进程的试用”
  2. 为什么输出不符合代码?
  3. 我们可以有一个程序,它有 4 个进程,并且所有进程执行不同的任务,例如一个进程打印“我的名字”。一个进程打印“我的年龄”,另一个进程打印“我的地址”?
  4. 如何在main函数中创建多个进程?如何
  5. 控制多个进程的执行?
  6. exec()函数是做什么的?任何人都可以用源代码向我解释 exec()fork()getpid() 的工作原理吗?

请帮助这个新手吗? 。

I'm new to c language and Linux. I have a problem related to fork(),getpid()and exec()function.
I wrote a c program using fork() call the code of my program is following"
code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

void fun()
{
  printf("\n this is trial for child process");
}

int main (int argc, char const *argv[])
{
  int i,status,pid,t;

  if(pid=fork()<0)
  { 
    printf("\nfailed to create the process\n");
  }
  if(pid=fork()==0)
  {
    printf("\n the child process is created");
    fun();
    exit(1);
  }
  while(wait(&status)!=pid);
  return 0;
}

The out put of this program is following:

the child process is created

this is trial for child process

the child process is created

this is trial for child process

Now my questions are as follows:

  1. Why the output of program showing same thing twice? The output supposed to be "child process is created this is trial for child process"
  2. Why the output is not according to code ?
  3. Can we have a program which has 4 processes and all the processes perform different task for example one process print "my name". One process print "my age", the other process print "my address ?
  4. How to make multiple process in main function ?
  5. How to control the execution of multiple process ?
  6. what does the exec() function do? Can anyone please explain me the working of exec(), fork(), getpid() with a source code?

Please help this novice fellow.

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

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

发布评论

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

评论(3

神魇的王 2024-11-18 14:27:31

您的代码多次调用 fork()

if(pid=fork()<0) /* calls fork() */
{ 
   ...
}
if(pid=fork()==0) /* also calls fork() */
{
   ...
}

每次成功的 fork() 都会创建一个新的子进程。更糟糕的是,第二个 fork() 被父级和第一个子级调用。

如果您尝试创建单个子进程,则应仅调用 fork() 一次:

pid_t pid; /* note the correct return type of fork() */
...
pid = fork();
if (pid < 0)
{ 
   ...
}
else if (pid == 0)
{ 
   ...
}

如果您想创建多个子进程,则可以让父进程调用 fork() 循环中。

对于诸如“exec 做什么?”之类的问题,我的建议是学习如何使用 man,然后如果手册页中还有不清楚的地方,请提出具体问题。

Your code calls fork() multiple times:

if(pid=fork()<0) /* calls fork() */
{ 
   ...
}
if(pid=fork()==0) /* also calls fork() */
{
   ...
}

Each successful fork() creates a new child process. To make matters worse, the second fork() is called by both the parent and the first child.

If you're trying to create a single child process, you should call fork() just once:

pid_t pid; /* note the correct return type of fork() */
...
pid = fork();
if (pid < 0)
{ 
   ...
}
else if (pid == 0)
{ 
   ...
}

If you want to create multiple child processes, you can have the parent process call fork() in a loop.

As to questions like "what does exec do?", my advice is to learn how to use man and then come back with specific questions if there's something in the manpages that remains unclear.

青春有你 2024-11-18 14:27:31

在此代码中,您将创建三个进程,不包括主进程。

pid=fork()

本身就是一条语句,它分叉一个新进程,即使它位于 if 语句条件内。第一次 fork() 调用后,剩余的代码将执行两次。所以下一个 fork 调用将被调用两次。您已经创建了一个新流程。

fork 返回零给它自己和它的
其父进程的进程 ID

这就是考虑进程 A 分叉 B(不是来自您的代码)

 pid = fork();
 printf("pid is : %d",pid);

printf 语句执行两次(一次用于 A,一次用于 B)。对于 A,它打印(A 是父级)

pid is : 512 //某个整数值
进程号

和 B 打印

pid 是:0

所以在你的问题中,

 if(pid=fork()==0)
  {
    printf("\n the child process is created");
    fun();
    exit(1);
  }

这是第二个 fork,它已经执行了两次。因此,每次执行都会创建一个新的子进程。对于两个子进程,pid 值都是 0。因此您的 print 语句将执行,这就是您在输出中看到的内容。但是对于父母双方来说,都会有一个 pid 值,并且您的 if 条件失败,因此它不会打印。这两个子进程是您的第二个和第三个进程。简而言之,您除了主进程之外还创建了 3 个进程

In this code you are creating Three process not including your main process.

pid=fork()

is itself a statement , which forks a new process even though it is inside an if statement condition. After the first fork() call the remaining codes will be executed twice. so next fork call will be called twice. You have already created a new process.

fork returns zero to itself and its
process id to its parent

That is consider a process A forks B (not from your code)

 pid = fork();
 printf("pid is : %d",pid);

printf statement executes twice(one for A and one for B). For A it prints(A is parent)

pid is : 512 //some integer value
process id

and B prints

pid is : 0

So in your question

 if(pid=fork()==0)
  {
    printf("\n the child process is created");
    fun();
    exit(1);
  }

this is the second fork which is already executing twice. So each of this execution creates a new child process. For both childs pid value is 0. So your print statement executes, which is what you see in the output. But for both parents a pid value will be there and your if condition fails, so it wont print. These two childs are your second and third processes..So in short you create 3 processes along with the main process

向地狱狂奔 2024-11-18 14:27:31

输出生成两次,因为您分叉两次:

if(pid=fork()<0)  // Fork #1
{ 
   printf("\nfailed to create the process\n");
}
if(pid=fork()==0) // Fork #2

The output is generated twice because you are forking twice:

if(pid=fork()<0)  // Fork #1
{ 
   printf("\nfailed to create the process\n");
}
if(pid=fork()==0) // Fork #2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文