/* * Adapted from the original rbt_test.c. */ static int t1_add(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) { int nprobs; dns_name_t *dns_name; nprobs = 0; if (name && dns_result) { if (create_name(name, mctx, &dns_name) == 0) { if (T_debug) t_info("dns_rbt_addname succeeded\n"); *dns_result = dns_rbt_addname(rbt, dns_name, dns_name); if (*dns_result != ISC_R_SUCCESS) { delete_name(dns_name, mctx); t_info("dns_rbt_addname failed %s\n", dns_result_totext(*dns_result)); ++nprobs; } } else { ++nprobs; } } else { ++nprobs; } return(nprobs); }
static int t1_search(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) { int nprobs; dns_name_t *dns_searchname; dns_name_t *dns_foundname; dns_fixedname_t dns_fixedname; void *data; nprobs = 0; if (name && dns_result) { if (create_name(name, mctx, &dns_searchname) == 0) { dns_fixedname_init(&dns_fixedname); dns_foundname = dns_fixedname_name(&dns_fixedname); data = NULL; *dns_result = dns_rbt_findname(rbt, dns_searchname, 0, dns_foundname, &data); delete_name(dns_searchname, mctx); } else { ++nprobs; } } else { ++nprobs; } return(nprobs); }
void delete_way(int way,int cont_temp) { char name[256]; int id_cont; stu_t *p,*head; FILE *fp1; switch(way) { case 1: printf("Please input delete the ID :1~%d:",cont_temp); scanf("%d",&id_cont); getchar(); fp1 = fopen("dong.txt","r"); p = head_1(fp1); fclose(fp1); head = delete_id(p,id_cont); write(head); break; case 2: printf("Please input delete the Name :"); scanf("%s",name); getchar(); fp1 = fopen("dong.txt","r"); p = head_1(fp1); fclose(fp1); head = delete_name(p,name); write(head); case 3: break; } }
static int t1_delete(char *name, dns_rbt_t *rbt, isc_mem_t *mctx, isc_result_t *dns_result) { int nprobs; dns_name_t *dns_name; nprobs = 0; if (name && dns_result) { if (create_name(name, mctx, &dns_name) == 0) { *dns_result = dns_rbt_deletename(rbt, dns_name, ISC_FALSE); delete_name(dns_name, mctx); } else { ++nprobs; } } else { ++nprobs; } return(nprobs); }
static int test_rbt_gen(char *filename, char *command, char *testname, isc_result_t exp_result) { int rval; int result; dns_rbt_t *rbt; isc_result_t isc_result; isc_result_t dns_result; isc_mem_t *mctx; isc_entropy_t *ectx; dns_name_t *dns_name; result = T_UNRESOLVED; if (strcmp(command, "create") != 0) t_info("testing using name %s\n", testname); mctx = NULL; ectx = NULL; isc_result = isc_mem_create(0, 0, &mctx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mem_create: %s: exiting\n", dns_result_totext(isc_result)); return(T_UNRESOLVED); } isc_result = isc_entropy_create(mctx, &ectx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_entropy_create: %s: exiting\n", dns_result_totext(isc_result)); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } isc_result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE); if (isc_result != ISC_R_SUCCESS) { t_info("isc_hash_create: %s: exiting\n", dns_result_totext(isc_result)); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(T_UNRESOLVED); } rbt = NULL; if (rbt_init(filename, &rbt, mctx) != 0) { if (strcmp(command, "create") == 0) result = T_FAIL; isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); } /* * Now try the database command. */ if (strcmp(command, "create") == 0) { result = T_PASS; } else if (strcmp(command, "add") == 0) { if (create_name(testname, mctx, &dns_name) == 0) { dns_result = dns_rbt_addname(rbt, dns_name, dns_name); if (dns_result != ISC_R_SUCCESS) delete_name(dns_name, mctx); if (dns_result == exp_result) { if (dns_result == ISC_R_SUCCESS) { rval = t1_search(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == ISC_R_SUCCESS) { result = T_PASS; } else { result = T_FAIL; } } else { t_info("t1_search failed\n"); result = T_UNRESOLVED; } } else { result = T_PASS; } } else { t_info("dns_rbt_addname returned %s, " "expected %s\n", dns_result_totext(dns_result), dns_result_totext(exp_result)); result = T_FAIL; } } else { t_info("create_name failed\n"); result = T_UNRESOLVED; } } else if ((strcmp(command, "delete") == 0) || (strcmp(command, "nuke") == 0)) { rval = t1_delete(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == exp_result) { rval = t1_search(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == ISC_R_SUCCESS) { t_info("dns_rbt_deletename " "didn't delete " "the name"); result = T_FAIL; } else { result = T_PASS; } } } else { t_info("delete returned %s, expected %s\n", dns_result_totext(dns_result), dns_result_totext(exp_result)); result = T_FAIL; } } } else if (strcmp(command, "search") == 0) { rval = t1_search(testname, rbt, mctx, &dns_result); if (rval == 0) { if (dns_result == exp_result) { result = T_PASS; } else { t_info("find returned %s, expected %s\n", dns_result_totext(dns_result), dns_result_totext(exp_result)); result = T_FAIL; } } } dns_rbt_destroy(&rbt); isc_hash_destroy(); isc_entropy_detach(&ectx); isc_mem_destroy(&mctx); return(result); }
int main(int argc, char **argv) { char *command, *arg, buffer[1024]; const char *whitespace; dns_name_t *name, *foundname; dns_fixedname_t fixedname; dns_rbt_t *rbt = NULL; int length, ch; isc_boolean_t show_final_mem = ISC_FALSE; isc_result_t result; void *data; progname = strrchr(*argv, '/'); if (progname != NULL) progname++; else progname = *argv; while ((ch = isc_commandline_parse(argc, argv, "m")) != -1) { switch (ch) { case 'm': show_final_mem = ISC_TRUE; break; } } argc -= isc_commandline_index; argv += isc_commandline_index; POST(argv); if (argc > 1) { printf("Usage: %s [-m]\n", progname); exit(1); } setbuf(stdout, NULL); /* * So isc_mem_stats() can report any allocation leaks. */ isc_mem_debugging = ISC_MEM_DEBUGRECORD; result = isc_mem_create(0, 0, &mctx); if (result != ISC_R_SUCCESS) { printf("isc_mem_create: %s: exiting\n", dns_result_totext(result)); exit(1); } result = dns_rbt_create(mctx, delete_name, NULL, &rbt); if (result != ISC_R_SUCCESS) { printf("dns_rbt_create: %s: exiting\n", dns_result_totext(result)); exit(1); } whitespace = " \t"; while (fgets(buffer, sizeof(buffer), stdin) != NULL) { length = strlen(buffer); if (buffer[length - 1] != '\n') { printf("line to long (%lu max), ignored\n", (unsigned long)sizeof(buffer) - 2); continue; } buffer[length - 1] = '\0'; command = buffer + strspn(buffer, whitespace); if (*command == '#') continue; arg = strpbrk(command, whitespace); if (arg != NULL) { *arg++ = '\0'; arg += strspn(arg, whitespace); } length = strlen(command); if (*command != '\0') { if (CMDCHECK("add")) { name = create_name(arg); if (name != NULL) { printf("adding name %s\n", arg); result = dns_rbt_addname(rbt, name, name); PRINTERR(result); } } else if (CMDCHECK("delete")) { name = create_name(arg); if (name != NULL) { printf("deleting name %s\n", arg); result = dns_rbt_deletename(rbt, name, ISC_FALSE); PRINTERR(result); delete_name(name, NULL); } } else if (CMDCHECK("nuke")) { name = create_name(arg); if (name != NULL) { printf("nuking name %s " "and its descendants\n", arg); result = dns_rbt_deletename(rbt, name, ISC_TRUE); PRINTERR(result); delete_name(name, NULL); } } else if (CMDCHECK("search")) { name = create_name(arg); if (name != NULL) { printf("searching for name %s ... ", arg); dns_fixedname_init(&fixedname); foundname = dns_fixedname_name(&fixedname); data = NULL; result = dns_rbt_findname(rbt, name, 0, foundname, &data); switch (result) { case ISC_R_SUCCESS: printf("found exact: "); print_name(data); putchar('\n'); break; case DNS_R_PARTIALMATCH: printf("found parent: "); print_name(data); printf("\n\t(foundname: "); print_name(foundname); printf(")\n"); break; case ISC_R_NOTFOUND: printf("NOT FOUND!\n"); break; case ISC_R_NOMEMORY: printf("OUT OF MEMORY!\n"); break; default: printf("UNEXPECTED RESULT\n"); } delete_name(name, NULL); } } else if (CMDCHECK("check")) { /* * Or "chain". I know, I know. Lame name. * I was having a hard time thinking of a * name (especially one that did not have * a conflicting first letter with another * command) that would differentiate this * from the search command. * * But it is just a test program, eh? */ name = create_name(arg); if (name != NULL) { detail(rbt, name); delete_name(name, NULL); } } else if (CMDCHECK("forward")) { iterate(rbt, ISC_TRUE); } else if (CMDCHECK("backward")) { iterate(rbt, ISC_FALSE); } else if (CMDCHECK("print")) { if (arg == NULL || *arg == '\0') dns_rbt_printall(rbt, NULL); else printf("usage: print\n"); } else if (CMDCHECK("quit")) { if (arg == NULL || *arg == '\0') break; else printf("usage: quit\n"); } else { printf("a(dd) NAME, d(elete) NAME, " "s(earch) NAME, p(rint), or q(uit)\n"); } } } dns_rbt_destroy(&rbt); if (show_final_mem) isc_mem_stats(mctx, stderr); return (0); }
int main(void){ // fill queue readFile(); //execute the processes with priority 0 queue *temp = first; while(temp!=NULL){ if (temp->data.priority == 0){ pid_t pid = fork(); if (pid == 0){ //child process puts("child:"); execlp("./process",NULL); exit(0); }else if (pid > 0){ //parent process temp->data.pid = pid; //set the right pid printf("[parent] waiting %d seconds...:\n",temp->data.runtime); sleep(temp->data.runtime); //sleep for the needed runtime puts("[parent] Sending SIGINT..."); kill(pid,SIGINT); waitpid(pid,0,0); //print process to be deleted printf("[parent] Deleting process: %s, priority: %d, pid: %d, runtime: %d\n", temp->data.name,temp->data.priority,temp->data.pid,temp->data.runtime); //delete process from queue queue *deleted = temp; temp = temp->next; delete_name(deleted->data.name); }else { //fork failed } }else{ temp = temp->next; } } //running the remaining processes temp = first; while(temp!=NULL){ pid_t pid = fork(); if (pid == 0){ //child process puts("child:"); execlp("./process",NULL); exit(0); }else if (pid > 0){ //parent process temp->data.pid = pid; //set the right pid printf("[parent] waiting %d seconds...:\n",temp->data.runtime); sleep(temp->data.runtime); //sleep for the needed runtime puts("[parent] Sending SIGINT..."); kill(pid,SIGINT); waitpid(pid,0,0); //print process to be deleted printf("[parent] Deleting process: %s, priority: %d, pid: %d, runtime: %d\n", temp->data.name,temp->data.priority,temp->data.pid,temp->data.runtime); pop(); //pop process from queue temp = first; }else { //fork failed } } /* //print all temp = first; while(temp!=NULL){ printf("name: %s, priority: %d, pid: %d, runtime: %d\n", temp->data.name,temp->data.priority,temp->data.pid,temp->data.runtime); temp = temp->next; } */ free(temp); return 0; }
/* Usage: follow the protocol to handle a request, and call right func Return: none */ void handle_request(struct name_prtl *name_request, int itemfd, int dbfd, int rservfd, int sockfd, int logfd, ssize_t n, int port, char *data) { int flag, result, m; char hostipaddr[16]; // chech which server should be responsible for the request flag = is_local(itemfd, name_request->name); if (flag == -1) { /* not have this kind of names */ // ask route server printf("[Info] ask routing server for the name\n"); result = route(rservfd, logfd, name_request->name[0], hostipaddr); if (result == -1) { /* fail */ fprintf(stderr, "[Error] route check failed\n"); write_log(logfd, "[Error] handle_request -- route check failed"); pkt_write(sockfd, 8, name_request->name, "fail to check route table"); } else if (result == 0) { /* new kind of names, add it locally */ // add new mapping first printf("[Info] find the corresponding server\n"); m = add_nameitem(itemfd, name_request->name[0]); if (m == -1) { fprintf(stderr, "[Error] fail to update indextable\n"); write_log(logfd, "[Error] handle_request -- update indextable error"); pkt_write(sockfd, 8, name_request->name, "fail to updata indextable"); } else { if (name_request->type == 2) { /* add new name */ printf("[Info] adding new name: %s\n", name_request->name); add_name(sockfd, dbfd, logfd, name_request); } else if (name_request->type == 1) { /* lookup */ printf("[Info] lookup name: %s\n", name_request->name); lookup_name(sockfd, dbfd, logfd, name_request); } else if (name_request->type == 3) { /* delete */ printf("[Info] delete name: %s\n", name_request->name); delete_name(sockfd, dbfd, logfd, name_request); } else if (name_request->type == 4) { /* update */ printf("[Info] update name: %s\n", name_request->name); update_name(sockfd, dbfd, logfd, name_request); } } } else if (result == 1) { /* there is another server which is responsible for this kind of names */ printf("[Info] forward request\n"); m = forward_request(sockfd, logfd, data, hostipaddr, port, n); if (m == -1) { /* fail */ fprintf(stderr, "[Error] fail to forward request\n"); pkt_write(sockfd, 8, name_request->name, "[Error] fail to forward pkt"); } } } else { /* find this kind of name locally */ if (name_request->type == 2) { /* add new name */ printf("[Info] adding new name: %s\n", name_request->name); add_name(sockfd, dbfd, logfd, name_request); } else if (name_request->type == 1) { /* lookup */ printf("[Info] lookup name: %s\n", name_request->name); lookup_name(sockfd, dbfd, logfd, name_request); } else if (name_request->type == 3) { /* delete */ printf("[Info] delete name: %s\n", name_request->name); delete_name(sockfd, dbfd, logfd, name_request); } else if (name_request->type == 4) { /* update */ printf("[Info] update name: %s\n", name_request->name); update_name(sockfd, dbfd, logfd, name_request); } } }