int main(int argc, char* argv[]) { int fd; fd = creat("4_test_file", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); /* rw-rw-rw- */ printf("the new create file fd is %d\n", fd); printf("use my_dup get a new fd is %d\n", my_dup(fd)); printf("use my_dup2 get a new fd(100) is %d\n", my_dup2(fd, 100)); printf("use dup get a new fd is %d\n", dup(fd)); printf("use dup2 get a new fd(100) is %d\n", dup2(fd, 100)); if (close(fd) == -1) errExit("close"); remove("4_test_file"); exit(EXIT_SUCCESS); }
int main (int argc, char *argv[]) { if (argc != 2) { usageErr("%s testfile\n", argv[0]); } int fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR); if (fd == -1) { errExit("open testfile"); } assert(my_dup(-1) == -1); assert(my_dup(42) == -1); int dup = my_dup(fd); assert(dup != -1); assert(dup != fd); assert(get_offset(fd) == 0); assert(get_offset(dup) == 0); char text[] = "abc"; size_t text_len = strlen(text); if (write(dup, text, text_len) != (ssize_t) text_len) { errExit("writing 'abc'"); } assert(get_offset(fd) == (off_t) text_len); assert(get_offset(dup) == (off_t) text_len); assert(my_dup2(42, 42) == -1); assert(my_dup2(STDOUT_FILENO, STDOUT_FILENO) == STDOUT_FILENO); assert(my_dup2(fd, -1) == -1); assert(my_dup2(fd, OPEN_MAX) == -1); assert(my_dup2(42, STDOUT_FILENO) == -1); dup = my_dup2(fd, STDOUT_FILENO); assert(dup == STDOUT_FILENO); char greeting[] = "Hello World!"; size_t combined_len = text_len + strlen(greeting); fprintf(stderr, "this should be the only output of this program\n"); printf(greeting); fflush(stdout); assert(get_offset(dup) == combined_len); assert(get_offset(fd) == combined_len); exit(EXIT_SUCCESS); }
void clp_prover_exit() { int status; char line[MAX_CLPPROVER]; save_normal_fildes(); my_dup(); printf("exit.\n"); fflush(stdout); while(mygetline(line,MAX_CLPPROVER) != -1 ); wait(&status); close(outbound[1]); close(inbound[0]); restore_normal_fildes(); fprintf(stderr, "clpprover closed!\n"); }
int clp_prover_interpolate(char *a, char* b, hash_table_t * varstable) { char line[MAX_CLPPROVER]; int SAT; save_normal_fildes(); my_dup(); printf("interpolate([%s],[%s]).\n", a, b); fflush(stdout); mygetline(line, MAX_CLPPROVER); SAT = (strcmp(line,"satisfiable_input") == 0); if(!SAT) convertCLPProverToCLPR(varstable); else fprintf(stderr, "SAT query\n"); restore_normal_fildes(); return SAT; }
void clp_prover_start() { char line[MAX_CLPPROVER]; // Create the pipe if(pipe(outbound) == -1 || pipe(inbound) == -1) { if(errno == EMFILE) fatal("process has too many files open"); if(errno == ENFILE) fatal("system has too many files open"); fatal("unknown pipe error"); } // fork() requires twice the amount of the parent memory. This can // happen even when fork() is immediately followed by an exec() call that // would release most of that extra memory. // In my machine, if I use fork() i get ENOMEM error. // vfork() solves this problem but it may introduce new problems // with several threads (deadlock). Anyway, this is not a problem for us. switch (vfork()) { case -1: if (errno == EAGAIN) fatal("Fork error. No enough resources to create a new process."); if (errno == ENOMEM) fatal("Fork error. The process requires more space."); case 0: // CHILD if(dup2(outbound[0] , STDIN_FILENO) == -1) fatal("dup2 error"); if(dup2(inbound[1] , STDOUT_FILENO) == -1) fatal("dup2 error"); close(outbound[1]); close(inbound[0]); close(outbound[0]); close(inbound[1]); char *base_path = getenv("TRACER_PATH"); char *path = malloc(strlen(base_path) + 50); sprintf(path, "%s/lib/clp-prover", base_path); if(execl(path, "clp-prover", NULL) == -1){ fprintf(stderr, "is the path to clpprover correct? %s\n", path); fatal("execl Error!"); } // exec call should never return fatal("execl Error!"); default: // PARENT // Set non-buffered output on stdout setvbuf(stdout,(char*)NULL,_IONBF,0); setvbuf(stderr,(char*)NULL,_IONBF,0); close(outbound[0]); close(inbound[1]); save_normal_fildes(); my_dup(); // set option to print ast in clpprover printf("print_interpolant_ast.\n"); fflush(stdout); // read preamble mygetline(line, MAX_CLPPROVER); mygetline(line, MAX_CLPPROVER); mygetline(line, MAX_CLPPROVER); restore_normal_fildes(); fprintf(stderr, "clpprover started!\n"); } }