#include
#include
#include
#include
int main(int argc,char* argv[])
{
if (argc != 8)
{
fprintf(stderr, "usage: a.out input_data, t1, t2, ? t6");
return -1;
}
if (atoi(argv[2]) < 0 || atoi(argv[3]) < 0 || atoi(argv[4]) < 0 ||
atoi(argv[5]) < 0 || atoi(argv[6]) < 0 || atoi(argv[7]) < 0)
{
fprintf(stderr, "t1-t6 should be positive integer!");
return -1;
}
int idata = atoi(argv[1]);
int pid_0 = getpid();
time_t thetime;
thetime = time(NULL);
printf("Process 0 (pid %d): Current time is %s", pid_0, ctime(thetime));
printf("Process 0 (pid %d): Current idata is %d.\n", pid_0, idata);
printf("Process 0: Time counting start.\n");
int t1 = atoi(argv[2]);
sleep(t1);
printf("Process 0 (PID %d): Waited %d seconds, now fork process 1.\n",pid_0, t1);
pid_t fork_id_1 = fork();
if (fork_id_1 == -1)
{
perror("Process 0 to Process 1: fork error!");
exit(EXIT_FAILURE);
}
else if (fork_id_1 != 0)
{
int t2 = atoi(argv[3]);
printf("Process 0 (pid %d): Before fork process 2, wait %d seconds.\n",pid_0, t2);
sleep(t2);
printf("Process 0 (pid %d): Waited %d seconds, now fork process 2.\n",pid_0, t2);
pid_t fork_id_2 = fork();
if (fork_id_2 == -1)
{
perror("Process 0 to Process 2: fork error!");
exit(EXIT_FAILURE);
}
else if (fork_id_2 == 0)
{
pid_t pid_2 = getpid();
int t4 = atoi(argv[5]);
int t5 = atoi(argv[6]);
int t6 = atoi(argv[7]);
int t = t4 - t5;
if (t <= 0)
{
thetime = time(NULL);
printf("Process 2 (pid %d): This is process 2, the second child process of Process 0 (pid %d), current time is %s", pid_2, pid_0, ctime(thetime));
sleep(t4);
printf("Process 2 (pid %d): Waited %d seconds.\n", pid_2, t4);
idata = idata + 1;
printf("Process 2 (pid %d): Added 1 to idata. Current idata is %d.\n", pid_2, idata);
printf("Process 2 (pid %d): Because t4(%d) <= t5(%d), process 2 will terminate before fork a child process.\n", pid_2, t4, t5);
thetime = time(NULL);
printf("Process 2 (pid %d): This is process 2, the second child process of Process 0 (pid %d), current time is %s", pid_2, pid_0, ctime(thetime));
printf("Process 2 (pid %d): Terminate.\n", pid_2);
exit(0);
}
else
{
thetime = time(NULL);
printf("Process 2 (pid %d): This is process 2, the second child process of Process 0 (pid %d), current time is %s", pid_2, pid_0, ctime(thetime));
sleep(t5);
printf("Process 2 (pid %d): Waited t5 = %d seconds before fork a child process.\n", pid_2, t5);
pid_t fork_id_3 = fork();
if (fork_id_3 == -1)
{
perror("Process 2 to Process 3: fork error!");
exit(EXIT_FAILURE);
}
else if (fork_id_3 == 0)
{
pid_t pid_3 = getpid();
thetime = time(NULL);
printf("Process 3 (pid %d): This is process 3, the only child process of Process 2 (pid %d), current time is %s", pid_3, pid_2, ctime(thetime));
sleep(t6);
printf("Process 3 (pid %d): Waited %d seconds.\n", pid_3, t6);
idata = idata + 1;
printf("Process 3 (pod %d): Added 1 to idata. Current idata is %d.\n", pid_3, idata);
thetime = time(NULL);
printf("Process 3 (pid %d): This is process 3, the only child process of Process 2 (pid %d), current time is %s", pid_3, pid_2, ctime(thetime));
printf("Process 3 (pid %d): Terminate.\n", pid_3);
}
else if (fork_id_3 != 0)
{
sleep(t);
printf("Process 2 (pid %d): Waited t4 - t5 = %d seconds.\n", pid_2, t);
idata = idata + 1;
printf("Process 2 (pid %d): Added 1 to idata. Current idata is %d.\n", pid_2, idata);
thetime = time(NULL);
printf("Process 2 (pid %d): This is process 2, the second child process of Process 0 (pid %d), current time is %s", pid_2, pid_0, ctime(thetime));
printf("Process 2 (pid %d): Terminate.\n", pid_2);
exit(0);
}
}
}
else if (fork_id_2 != 0)
{
idata = idata + 1;
printf("Process 0 (pid %d): Added 1 to idata. Current idata is %d.\n", pid_0, idata);
thetime = time(NULL);
printf("Process 0 (pid %d): This is process 0, current time is %s", pid_0, ctime(thetime));
printf("Process 0 (pid %d): Waiting for child processes to terminate, if any.\n", pid_0);
// wait(NULL);
waitpid(fork_id_1, NULL, 0);
waitpid(fork_id_2, NULL, 0);
printf("Process 0 (pid %d): Terminate.\n", pid_0);
exit(0);
}
}
else if (fork_id_1 == 0)
{
int pid_1 = getpid();
thetime = time(NULL);
int t3 = atoi(argv[4]);
printf("Process 1 (pid %d): This is process 1, the first child process of Process 0 (pid %d), current time is %s", pid_1, pid_0, ctime(thetime));
printf("Process 1 (pid %d): Wait %d seconds before terminate.\n", pid_1, t3);
sleep(t3);
printf("Process 1 (pid %d): Waited %d seconds.\n", pid_1, t3);
idata = idata + 1;
printf("Process 1 (pid %d): Added 1 to idata. Current idata is %d.\n", pid_1, idata);
thetime = time(NULL);
printf("Process 1 (pid %d): This is process 1, the first child process of Process 0 (pid %d), current time is %s", pid_1, pid_0, ctime(thetime));
printf("Pricess 1 (pid %d): Terminate.\n", pid_1);
exit(0);
}
}