int main(int argc, char *argv[]) { int num_threads = 1; int num_libs = 1; if (argc >= 2) num_threads = atoi(argv[1]); if (argc >= 3) num_libs = atoi(argv[2]); assert(num_libs <= MAX_N_FUNCTIONS); int lib; for (lib = 0; lib < num_libs; lib++) { char buf[4096]; snprintf(buf, sizeof(buf), "%s-f%d.so", argv[0], lib); void *handle = dlopen(buf, RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); exit(1); } snprintf(buf, sizeof(buf), "f%d", lib); Functions[lib] = (f_t)dlsym(handle, buf); if (!Functions[lib]) { fprintf(stderr, "%s\n", dlerror()); exit(1); } fprintf(stderr, "LIB[%03d] %s: %p\n", lib, buf, Functions[lib]); PrintStuff(0); int i; for (i = 0; i < num_threads; i++) { pthread_t t; fprintf(stderr, "Creating thread %d\n", i); pthread_create(&t, 0, PrintStuff, 0); pthread_join(t, 0); } } return 0; }
/* ---------------------------- USERINTERACTION --------------------------- */ void UserInteraction (Student *hash_table, int *access_table, int *elements) { int selection; char user_input[KILOBYTE]; do { PrintStuff (hash_table, access_table); /* print the two tables every single time */ printf ("\n------- MENU -------\n"); printf ("1. Insert a new entry.\n"); printf ("2. Search with an ID number.\n"); printf ("3. Search with a name\n"); printf ("4. Exit\n"); printf ("Enter you selection: "); fgets (user_input, KILOBYTE, stdin); selection = atoi (user_input); /* get the user input from the keyboard and convert it integer */ if (selection == INSERT) { char one_line[KILOBYTE]; int student_ID = 0; char *token; int hash_index; printf ("Please enter data (student_ID, first_name last_name): "); fgets (one_line, KILOBYTE, stdin); /* prompt user for data and read from keyboard */ /* ### START of cut and paste from ReadFile() */ strtok (one_line, " "); student_ID = atoi (one_line); hash_index = Hash (hash_table, student_ID); hash_table[hash_index].student_ID = student_ID; token = (char *) strtok (NULL, " "); hash_table[hash_index].first_name = (char *) malloc (strlen (token) + 1); strcpy (hash_table[hash_index].first_name, token); token = (char *) strtok (NULL, " \n"); hash_table[hash_index].last_name = (char *) malloc (strlen (token) + 1); strcpy (hash_table[hash_index].last_name, token); hash_table[hash_index].name = (char *) malloc (strlen (hash_table[hash_index].last_name) + strlen (hash_table[hash_index].first_name) + 1); strcpy (hash_table[hash_index].name, hash_table[hash_index].last_name); strcat (hash_table[hash_index].name, hash_table[hash_index].first_name); /* ### END of cut and paste from ReadFile() */ /* MAKE SURE elements and the access table get updated */ *elements = QuickSort (hash_table, access_table); } else if (selection == SEARCH_ID) { int hash_index; int probe_number = 0; char user_search[KILOBYTE]; int student_ID = 0; printf ("Please enter the student ID: "); fgets (user_search, KILOBYTE, stdin); student_ID = atoi (user_search); /* prompt user for student ID, get it, and conver to integer */ hash_index = student_ID % HASH_SIZE; /* do the hash function !!! */ /* if studentID does not match, then keep probing */ while (hash_table[hash_index].student_ID != student_ID && probe_number < ( (HASH_SIZE + 1) / 2) ) { probe_number++; hash_index = hash_index + (2 * probe_number - 1); hash_index = hash_index % HASH_SIZE; } /* if probe_number == HASH_SIZE / 2 then NOT FOUND */ if (probe_number == (HASH_SIZE + 1) / 2) { hash_index = -1; /* -- printf ("--> Unable to find \"%d\".\n", student_ID); -- */ } /* ... otherwise print out what has been found */ /* else { */ printf ("--> \"%d\" found at location %d.\n", student_ID, hash_index); /* } */ } else if (selection == SEARCH_NAME) { char user_search[KILOBYTE]; char *search_for; char first[KILOBYTE]; int i = 0; printf ("Please enter the name (first_name last_name): "); fgets (user_search, KILOBYTE, stdin); /* prompt for names, get them, and REVERSE..... */ /* ### REVERSE PROCESS */ search_for = (char *) malloc (strlen (user_search) + 1); user_search[strlen (user_search) - 1] = '\0'; while (user_search[i] != ' ') { first[i] = user_search[i]; i++; } first[i] = '\0'; i++; strcpy (search_for, &user_search[i]); strcat (search_for, first); /* ### END OF REVERSE PROCESS */ printf ("(Comparing: "); /* ?Gay */ i = BinarySearch (hash_table, access_table, search_for, *elements); /* perform the binary search here */ printf (")"); /* ?Gay */ printf ("--> \"%s\" found at location %d.\n", user_search, i); /* print out what was found */ } } while (selection != SELECT_EXIT); /* keep loopint until the user enters '4' */ }