#include<iostream>
#include<spawn.h>
#include<stdlib.h>
#include<sys/wait.h>
using namespace std;

int main(int argc,char *argv[],char *envp[])
{
pid_t c1,c2;
int ret1,ret2,val;
ret1=posix_spawn(&c1,"t.cpp",NULL,NULL,argv,envp);
ret2=posix_spawn(&c2,"t.cpp",NULL,NULL,argv,envp);
wait(&val);
wait(&val);
cout<<"\n\nChild Process 1: "<<c1;
cout<<"\n\nChild Process 2: "<<c2;
cout<<"\n\n"<<ret1;
cout<<"\n\n"<<ret2<<"\n";
}

Above code creates 2 child processes and each process in supposed to execute t.cpp which is a simple hello world code in c++. But it doesnt execute it. In fact this code works without creating even t.cpp. Can anyone explain why is this happening and how to execute t1.cpp?

有帮助吗?

解决方案

This is the way of creating new process:

posix_spawn(&Pid,"my_exe",NULL,NULL,argv,envp);

where my_exe is the executable file created after I build the program. In your case t will be the executable created after you build the program.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top