#include #include #include #include #include #include #include #define BUFFSIZE 135 /******************************************************************************* * The following two functions are not part of the assignment. They are needed * * to generate simulated observatory data. * ******************************************************************************/ char bin2hex ( char *s ) { int h, i; h = (*s) - '0'; for (i=1; i<4; ++i) { ++s; h *= 2; if (*s == '1') ++h; } if (h < 10) return '0'+h; else return 'a' + h - 10; } void geninput ( int n ) { FILE *ifp, *ofp; char I[64][128], J[64][128], ofname[16]; int i, j, k, p, nline, ncorrupt, opflag[128]; ifp = (FILE *)fopen("../img/sample01.txt", "r"); for (i=0; i<64; ++i) { for (j=0; j<128; ++j) { fscanf(ifp, "%c", &I[i][j]); } getc(ifp); } fclose(ifp); for (p=1; p<=n; ++p) { nline = 0; for (i=0; i<64; ++i) { for (j=0; j<128; ++j) J[i][j] = I[i][j]; if ((opflag[i] = rand() % 2)) { ++nline; ncorrupt = rand() % 8; for (k=0; k= 64) i -= 64; } opflag[i] = 0; fprintf(ofp, "%2d ", i); for (j=0; j<128; j+=4) fprintf(ofp, "%c", bin2hex(J[i]+j)); fprintf(ofp, "\n"); } fclose(ofp); } } /*********** The solution of the assignment starts below this line ************/ void hex2bin ( char d, char *s ) { int digit, i; if ((d >= '0') && (d <= '9')) digit = d - '0'; else digit = 10 + d - 'a'; for (i=3; i>=0; --i,++s) *s = (digit & (1 << i)) ? '1' : '0'; } void senddata ( int i, int wfd ) { FILE *ifp; char ifname[20], line[128], buffer[BUFFSIZE];; int lineno, j, k; sprintf(ifname, "C2/A%02d.txt", i); ifp = (FILE *)fopen(ifname, "r"); while (1) { fscanf(ifp, "%d%s", &lineno, line); if (feof(ifp)) break; sprintf(buffer, "%2d %2d ", i, lineno); for (j=0,k=6; j<32; ++j,k+=4) hex2bin(line[j],buffer+k); buffer[BUFFSIZE-1] = '\0'; write(wfd,buffer,BUFFSIZE); } fclose(ifp); sprintf(buffer, "%d -1", i); write(wfd,buffer,BUFFSIZE); } char **receivedata ( int n, int rfd, int nline[] ) { char **T, buffer[BUFFSIZE]; int i, r, ndone; T = (char **)malloc(64 * n * sizeof(char *)); for (i=0; i<64*n; ++i) T[i] = NULL; for (i=0; i<64; ++i) nline[i] = 0; ndone = 0; while (ndone < n) { read(rfd, buffer, BUFFSIZE); sscanf(buffer, "%d%d", &r, &i); if (i == -1) ++ndone; else { T[n*i+nline[i]] = (char *)malloc(129*sizeof(char)); sscanf(buffer+6, "%s", T[n*i+nline[i]]); ++(nline[i]); } } return T; } void stitchdata ( int n, char I[][129], char **T, int nline[] ) { int i, j, k, n0, n1; for (i=0; i<64; ++i) { for (j=0; j<128; ++j) { n0 = n1 = 0; for (k=0; k n1) I[i][j] = '0'; else if (n1 > n0) I[i][j] = '1'; else I[i][j] = '?'; } I[i][128] = '\0'; } } void sendimage ( char I[][129] ) { int i; for (i=0; i<64; ++i) printf("%s\n", I[i]); } void receiveimage ( ) { int i, j, nqm; char I[64][129], pixel; for (i=0; i<64; ++i) { for (j=0; j<128; ++j) scanf("%c",I[i]+j); I[i][128] = '\0'; getchar(); } printf("\n+++ The unprocessed image\n\n"); for (i=0; i<64; ++i) printf(" %s\n", I[i]); nqm = 0; for (i=1; i<63; ++i) { for (j=1; j<127; ++j) { if (I[i][j] == '?') { pixel = I[i-1][j-1]; if (pixel != '?') { if (I[i-1][j] != pixel) continue; if (I[i-1][j+1] != pixel) continue; if (I[i][j-1] != pixel) continue; if (I[i][j+1] != pixel) continue; if (I[i+1][j-1] != pixel) continue; if (I[i+1][j] != pixel) continue; if (I[i+1][j+1] != pixel) continue; I[i][j] = pixel; ++nqm; } } } } printf("\n%d question marks replaced\n", nqm); printf("\n+++ Printing for light terminals\n\n"); for (i=0; i<64; ++i) { printf(" "); for (j=0; j<128; ++j) printf("%c", (I[i][j]=='0')?' ':I[i][j]); printf("\n"); } printf("\n"); printf("\n+++ Printing for dark terminals\n\n"); for (i=0; i<64; ++i) { printf(" "); for (j=0; j<128; ++j) printf("%c", (I[i][j]=='1')?' ':I[i][j]); printf("\n"); } printf("\n"); } int main ( int argc, char *argv[] ) { int n, i; pid_t *sndpid, stcpid, cpid; int fd[2], sfd[2]; n = (argc > 1) ? atoi(argv[1]) : 10; if ((n <= 0) || (n > 16)) n = 10; srand((unsigned int)time(NULL)); #ifdef _CREATE_INPUT geninput(n); #endif if (pipe(fd) == -1) { fprintf(stderr, "*** Unable to set up pipe\n"); exit(1); } if (pipe(sfd) == -1) { fprintf(stderr, "*** Unable to set up pipe\n"); exit(1); } sndpid = (pid_t *)malloc(n * sizeof(pid_t)); for (i=0; i