FedoraLinux中的进程 我是FEDORA新手,用C++代码在FEDORA中创建过程。我想从父进程生成2个进程。我是在我的代码中这样做的,但是当进程2被创建并且我检查它的父id时,它与原始的父id不同,一些人可以告诉我为什么这个代码会显示这种行为谢谢 #include <iostream> #include <string.h> #include <stdio.h> #include <spawn.h> #include <unistd.h> #include <sys/wait.h> using namespace std; int main() { cout<<"Begning of the program"<<endl; int counter=0; pid_t child1=fork(); if(child1==0) { cout<<"Child1 Process"<<endl; cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; } else if(child1>0) { pid_t child2=fork(); if(child2>0) { cout<<"Parrent of Child1 and Child2"<<endl; cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; } else if(child2==0) { cout<<"Child2 Creadted"<<endl; cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; } else { cout<<"Process Failed"<<endl; } } else { cout<<"Process fail"<<endl; } cout<<"End "<<endl; return 0; }
在分叉子进程输出其父进程id之前,真正的父进程(调用了FedoraLinux中的进程 我是FEDORA新手,用C++代码在FEDORA中创建过程。我想从父进程生成2个进程。我是在我的代码中这样做的,但是当进程2被创建并且我检查它的父id时,它与原始的父id不同,一些人可以告诉我为什么这个代码会显示这种行为谢谢 #include <iostream> #include <string.h> #include <stdio.h> #include <spawn.h> #include <unistd.h> #include <sys/wait.h> using namespace std; int main() { cout<<"Begning of the program"<<endl; int counter=0; pid_t child1=fork(); if(child1==0) { cout<<"Child1 Process"<<endl; cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; } else if(child1>0) { pid_t child2=fork(); if(child2>0) { cout<<"Parrent of Child1 and Child2"<<endl; cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; } else if(child2==0) { cout<<"Child2 Creadted"<<endl; cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; } else { cout<<"Process Failed"<<endl; } } else { cout<<"Process fail"<<endl; } cout<<"End "<<endl; return 0; },c++,linux,process,C++,Linux,Process,在分叉子进程输出其父进程id之前,真正的父进程(调用了fork())已经退出。子进程被重新附加到组父进程,子进程输出该pid 您可以调用pstree-p查看哪个进程是1303 我建议更换电话线 cout<<"Process ID: "<<getpid()<<endl; cout<<"Parrent ID: "<<getppid()<<endl; 在分叉子进程输出其父进程id之前,真正的父进程(调用了fork())已经退出。
fork()
)已经退出。子进程被重新附加到组父进程,子进程输出该pid
您可以调用pstree-p
查看哪个进程是1303
我建议更换电话线
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
在分叉子进程输出其父进程id之前,真正的父进程(调用了
fork()
)已经退出。子进程被重新附加到组父进程,子进程输出该pid
您可以调用pstree-p
查看哪个进程是1303
我建议更换电话线
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
您应该像我在代码中所做的那样,在创建流程2之后应用wait。这样父对象在子对象2执行之前保持活动状态。在您的r代码中,父级在创建child2后死亡,这使child2成为一个僵尸进程
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <spawn.h>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
int main()
{
cout<<"Begning of the program"<<endl;
int counter=0;
pid_t child1=fork();
if(child1==0)
{
cout<<"Child1 Process"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else if(child1>0)
{
pid_t child2=fork();
wait(NULL);
if(child2>0)
{
cout<<"Parrent of Child1 and Child2"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else if(child2==0)
{
cout<<"Child2 Creadted"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else
{
cout<<"Process Failed"<<endl;
}
}
else
{
cout<<"Process fail"<<endl;
}
cout<<"End "<<endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout您应该像我在代码中所做的那样,在创建进程2之后应用wait。这样,父进程在子进程2执行之前保持活动状态。在您的代码中,父进程在创建子进程2之后死亡,这使子进程2成为一个僵尸进程
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <spawn.h>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
int main()
{
cout<<"Begning of the program"<<endl;
int counter=0;
pid_t child1=fork();
if(child1==0)
{
cout<<"Child1 Process"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else if(child1>0)
{
pid_t child2=fork();
wait(NULL);
if(child2>0)
{
cout<<"Parrent of Child1 and Child2"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else if(child2==0)
{
cout<<"Child2 Creadted"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else
{
cout<<"Process Failed"<<endl;
}
}
else
{
cout<<"Process fail"<<endl;
}
cout<<"End "<<endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
c未定义三个进程之间的输出顺序。您需要将的输出标记为“进程ID”
和的“进程ID”
这样你就知道它们真正来自哪个进程了。与你的问题更相关的是,你没有地方让子进程完成。1287?00:00:00系统D这是1287进程一些程序员,我应该把wait命令放在哪里?三个进程之间的输出顺序没有定义。你需要标记“进程ID”
和相关ID"
这样你就知道它们真正来自哪个进程了。与你的问题更相关的是,你没有地方让子进程完成。1287?00:00:00 systemd这是1287进程一些程序员伙计,我应该把wait命令放在哪里?1287?00:00:00 systemd这是1287进程现在我该怎么做才能获得正确的进程id子进程2?systemd
正常。它是所有用户进程的起始根。您可以在父进程中调用waitpid
以等待子进程,这样父进程将一直处于空闲状态,直到子进程退出。1287?00:00:00 systemd这是1287进程现在我可以做什么来获取子进程2的正确parrent id?systemd
可以。它是所有用户进程的起始根。您可以在父进程中调用waitpid
,等待子进程,这样父进程将处于空闲状态,直到子进程退出。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <spawn.h>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
int main()
{
cout<<"Begning of the program"<<endl;
int counter=0;
pid_t child1=fork();
if(child1==0)
{
cout<<"Child1 Process"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else if(child1>0)
{
pid_t child2=fork();
wait(NULL);
if(child2>0)
{
cout<<"Parrent of Child1 and Child2"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else if(child2==0)
{
cout<<"Child2 Creadted"<<endl;
cout<<"Process ID: "<<getpid()<<endl;
cout<<"Parrent ID: "<<getppid()<<endl;
}
else
{
cout<<"Process Failed"<<endl;
}
}
else
{
cout<<"Process fail"<<endl;
}
cout<<"End "<<endl;
return 0;
}