int main(void) { int tmp = 1026; int* p; for (tmp = 1026; tmp < 1027; tmp++) { p = (int *) palloc(); *p = 0; share_page(p); } int pid = fork(); if(pid == 0){ *p += 3; }else{ *p += 32; } exit(); return 0; }
int main(int argc, char *argv[]) { pid_t pid; int fd; int writer2encrypt[2]; int encrypt2reader[2]; int reader2writer[2]; char *buf_writer, *buf_reader; int *r; if (argc < 2) { write(STDOUT_FILENO, ERR_ARGC, sizeof(ERR_ARGC) - 1); _exit(-1); } /* create 2 pipes */ if (pipe(encrypt2reader) < 0 || pipe(writer2encrypt) < 0 || pipe(reader2writer) < 0) { write(STDOUT_FILENO, ERR_PIPE, sizeof(ERR_PIPE) - 1); return -1; } buf_reader = (char *) palloc(); buf_writer = (char *) palloc(); r = (int *) palloc(); share_page((void *) buf_reader); share_page((void *) buf_writer); share_page((void *) r); pid = fork(); if (pid < (pid_t) 0) { write(STDOUT_FILENO, ERR_FORK1, sizeof(ERR_FORK1) - 1); return -1; } else if (pid == (pid_t) 0) { close(writer2encrypt[0]); close(writer2encrypt[1]); close(encrypt2reader[1]); close(reader2writer[0]); if (argc > 2) { if ( (fd = open(argv[2], O_CREAT | O_RDWR, 0)) < 0) { write(STDOUT_FILENO, ERR_OPEN, sizeof(ERR_OPEN) - 1); return -1; } } else { fd = STDOUT_FILENO; } /* READER process */ return reader(fd, encrypt2reader[0], reader2writer[1], buf_reader, r+1); } else { pid = fork(); if (pid < (pid_t) 0) { write(STDOUT_FILENO, ERR_FORK2, sizeof(ERR_FORK2) - 1); return -1; } else if (pid == (pid_t) 0) { close(encrypt2reader[0]); close(writer2encrypt[1]); /* ENCRYPT process */ return encrypt(writer2encrypt[0], buf_writer, r, encrypt2reader[1], buf_reader, r+1); } else { close(encrypt2reader[0]); close(encrypt2reader[1]); close(writer2encrypt[0]); close(reader2writer[1]); /* open the file to encrypt */ if ( (fd = open(argv[1], O_RDONLY, 0)) < 0) { write(STDOUT_FILENO, ERR_OPEN, sizeof(ERR_OPEN) - 1); return -1; } /* WRITER process */ return writer(fd, writer2encrypt[1], reader2writer[0], buf_writer, r); } } return 0; }