An example of waitpid()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
I want to modify this program using waitpid() only to output
121
#include <iostream>
#include <unistd.h>
using namespace std;
int main ()
{
pid_t child_pid;
int i=1;
child_pid = fork ();
if (child_pid != 0)
{
cout << i++<<endl;
cout << i++<<endl;
}
else
cout << i++<<endl;
return 0;
}
/////////////////////////////MY VERSION///////////////////////////////////
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main ()
{
pid_t child_pid;
int i=1;
child_pid = fork ();
if (child_pid != 0)
{
cout << i++<<endl;
cout << i++<<endl;
}
else{
waitpid(child_pid, NULL,WNOHANG);
cout << i++<<endl;}
return 0;
}
|
Last edited on
Topic archived. No new replies allowed.