diff options
author | LAN-TW <lantw44@gmail.com> | 2013-11-16 17:06:29 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-11-16 17:06:29 +0800 |
commit | 0a2f61bc3b40a26fb192555f404a32f206d112a4 (patch) | |
tree | 00484198e3b3a6078a4f80af4682fe5ba23cb162 | |
parent | 69ac4b3704e5360c7e67903dbfa9e0780bffa040 (diff) | |
download | sp2013-0a2f61bc3b40a26fb192555f404a32f206d112a4.tar.gz sp2013-0a2f61bc3b40a26fb192555f404a32f206d112a4.tar.zst sp2013-0a2f61bc3b40a26fb192555f404a32f206d112a4.zip |
HW2: big_judge 成績排序、judge 砍掉未結束的 player
-rw-r--r-- | hw2/big_judge.c | 47 | ||||
-rw-r--r-- | hw2/judge.c | 18 |
2 files changed, 55 insertions, 10 deletions
diff --git a/hw2/big_judge.c b/hw2/big_judge.c index 05077a7..7c029c0 100644 --- a/hw2/big_judge.c +++ b/hw2/big_judge.c @@ -25,9 +25,27 @@ typedef struct { } JudgeData; typedef struct { + int id; long score; } PlayerData; +static int player_sort_cb (const void* aa, const void* bb) { + const PlayerData* a = aa; + const PlayerData* b = bb; + if (a->score > b->score) { + return -1; + } else if (a->score < b->score) { + return 1; + } else { + if (a->id > b->id) { + return 1; + } else if (a->id < b->id) { + return -1; + } + } + return 0; +} + static bool comb4 (int players[], long player_num) { for (int i = 3; i >= 0; i--) { if (players[i] + 1 <= player_num && @@ -148,6 +166,7 @@ int main (int argc, char* argv[]) { struct pollfd* jpoll = xmalloc (sizeof (struct pollfd) * judge_num); for (int i = 0; i < player_num; i++) { + pd[i].id = i + 1; pd[i].score = 0; } for (int i = 0; i < judge_num; i++) { @@ -194,6 +213,7 @@ int main (int argc, char* argv[]) { int players[4] = {1, 2, 3, 4}; long completed_jobs = 0; + long sent_jobs = 0; do { char msg[20]; int msglen; @@ -208,14 +228,37 @@ int main (int argc, char* argv[]) { completed_jobs++; } - comp135_log (comp, "Send %d %d %d %d to judge %d", + comp135_log (comp, "Sending %d %d %d %d to judge %d", players[0], players[1], players[2], players[3], jnext + 1); write (jd[jnext].write, msg, msglen); comp135_log (comp, "Judge %d is working now", jnext + 1); jd[jnext].working = true; - + sent_jobs++; } while (comb4 (players, player_num)); + for (; + completed_jobs < sent_jobs && + nextscore (comp, jd, judge_num, pd, player_num, jpoll) >= 0; + completed_jobs++); + + for (int i = 0; i < judge_num; i++) { + const char msg[] = "-1 -1 -1 -1\n"; + const int msglen = STATIC_STRLEN (msg); + write (jd[i].write, msg, msglen); + } + + qsort (pd, player_num, sizeof (PlayerData), player_sort_cb); + + printf ("%d", pd[0].id); + for (int i = 1; i < player_num; i++) { + printf (" %d", pd[i].id); + } + + for (int i = 0; i < player_num; i++) { + comp135_log (comp, "Rank %d = Player %d with %ld points", + i + 1, pd[i].id, pd[i].score); + } + comp135_destroy (comp); return 0; diff --git a/hw2/judge.c b/hw2/judge.c index 9c707a0..51c195b 100644 --- a/hw2/judge.c +++ b/hw2/judge.c @@ -40,10 +40,6 @@ static void player_tle_setter (int signo) { player_tle = 1; } -static void cleanup_child (int signo) { - waitpid (-1, NULL, WUNTRACED | WNOHANG); -} - static void fdata_clear (FData* d) { d->last = 0; d->score = 0; @@ -142,8 +138,6 @@ int main (int argc, char* argv[]) { }; sigemptyset (&sa.sa_mask); sigaction (SIGALRM, &sa, NULL); - sa.sa_handler = cleanup_child; - sigaction (SIGCHLD, &sa, NULL); sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, NULL); @@ -203,6 +197,7 @@ int main (int argc, char* argv[]) { fprintf (stderr, "Cannot fork: %s\n", strerror (errno)); for (int j = 1; j < i; j++) { kill (ffd[i].pid, SIGKILL); + waitpid (ffd[i].pid, NULL, 0); } goto new_loop_end; } else if (ffd[i].pid == 0) { @@ -357,12 +352,19 @@ int main (int argc, char* argv[]) { } fflush (stdout); + /* clean up all child process */ + for (i = 1; i <= 4; i++) { + comp135_log (comp, "Waiting for PID %u to exit ...", ffd[i].pid); + kill (ffd[i].pid, SIGKILL); + waitpid (ffd[i].pid, NULL, 0); + } + + comp135_log (comp, "Waiting for the next request from big_judge"); + new_loop_end: free (linestr); linestr = NULL; linelen = 0; - - comp135_log (comp, "Waiting for the next request from big_judge"); } free (linestr); |