C/C++ code#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
int status,i;
pid_t apid;
for (i = 0; i < 10; i++)
{
status = fork();
if (status == 0 || status == -1) break;
}
if (status == -1)
{
//error
cout << "error" << endl;
}
else if (status == 0)
{
//sub process
pid_t cpid;
cpid = getpid();
cout << "in child " << i <
int msec;
// Add the following three lines
time_t tick;
tick = time(0);
srand((tick << 16) | (getpid() & 0xffff));
msec = rand()%1000000;
// Add the following line to check the msec
cout << "msec = " << msec << endl;
usleep(msec);
exit(0);
}
else
{
//parent process
apid=wait(&status);
cout << "apid= " << apid << endl;
}
return 0;
}