diff options
author | LAN-TW <lantw44@gmail.com> | 2013-11-05 22:39:13 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-11-05 22:39:13 +0800 |
commit | 0750f329918ff6fa2e0169f7687ce6ec5b796efb (patch) | |
tree | 83985ba65844b3e1d09b3c6fab652ddfcb5bea19 /hw2/big_judge.c | |
parent | 259a2c1113be7b4998c76f6ff1298881e54746d7 (diff) | |
download | sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.tar sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.tar.gz sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.tar.bz2 sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.tar.lz sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.tar.xz sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.tar.zst sp2013-0750f329918ff6fa2e0169f7687ce6ec5b796efb.zip |
HW2: 準備好一些基本的東西
Diffstat (limited to 'hw2/big_judge.c')
-rw-r--r-- | hw2/big_judge.c | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/hw2/big_judge.c b/hw2/big_judge.c index fbb21cf..507f2b8 100644 --- a/hw2/big_judge.c +++ b/hw2/big_judge.c @@ -6,10 +6,25 @@ #include "logger.h" #include "xwrap.h" +#include <errno.h> #include <stdbool.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> -bool comb4 (Comp135* comp, int players[], int player_num) { +typedef struct { + pid_t pid; + int read; + int write; +} JudgeData; + +typedef struct { + long score; +} PlayerData; + +bool comb4 (Comp135* comp, int players[], long player_num) { for (int i = 3; i >= 0; i--) { if (players[i] + 1 <= player_num && players[i] + 1 + (3 - i) <= player_num) { @@ -54,7 +69,49 @@ int main (int argc, char* argv[]) { comp135_init (comp, argv[0], false); + JudgeData* jd = xmalloc (sizeof (JudgeData) * judge_num); + PlayerData* pd = xmalloc (sizeof (PlayerData) * player_num); + + for (int i = 0; i < player_num; i++) { + pd[i].score = 0; + } + for (int i = 0; i < judge_num; i++) { + int fdr[2], fdw[2]; + if (pipe (fdr) < 0) { + fprintf (stderr, "%s: pipe: %s\n", argv[0], strerror (errno)); + return 4; + } + if (pipe (fdw) < 0) { + fprintf (stderr, "%s: pipe: %s\n", argv[0], strerror (errno)); + return 4; + } + + jd[i].pid = fork (); + if (jd[i].pid < 0) { + fprintf (stderr, "%s: fork: %s\n", argv[0], strerror (errno)); + return 5; + } else if (jd[i].pid > 0) { + close (fdr[1]); + close (fdw[0]); + } else { + close (fdr[0]); + close (fdw[1]); + char* myjudge = xgetres ("judge"); + execl (myjudge, "judge", xsprintf ("%d", i + 1), NULL); + fprintf (stderr, "%s: execl: %s: %s\n", argv[0], myjudge, strerror (errno)); + _exit (1); + } + + jd[i].read = fdr[0]; + jd[i].write = fdw[1]; + comp135_log (comp, "Judge %d created: PID = %u, read = %d, write = %d", + i + 1, jd[i].pid, jd[i].read, jd[i].write); + } + int players[4] = {1, 2, 3, 4}; + do { + + } while (comb4 (comp, players, player_num)); comp135_destroy (comp); |