Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <signal.h>
- // Configurations
- #define MAX_LEN 100
- // Global variables
- pthread_t *threads;
- unsigned int nbThreads;
- pthread_mutex_t nom_mutex = PTHREAD_MUTEX_INITIALIZER;
- const char *alphabet[63] =
- {
- "a", "b", "c", "d", "e", "f", "g",
- "h", "i", "j", "k", "l", "m", "n",
- "o", "p", "q", "r", "s", "t", "u",
- "v", "w", "x", "y", "z", "A", "B",
- "C", "D", "E", "F", "G", "H", "I",
- "J", "K", "L", "M", "N", "O", "P",
- "Q", "R", "S", "T", "U", "V", "W",
- "X", "Y", "Z", "1", "2", "3", "4",
- "5", "6", "7", "8", "9", "0", " "
- };
- void signal_handler(int sig) {
- printf("aleeeeeeerte !!!\n");
- free(threads);
- exit(0);
- }
- void bruteImpl(int start_origin, unsigned int start, char* str, int index, int maxDepth)
- {
- if (start_origin == start) {
- str[index] = *alphabet[start];
- if (strcmp(str, "teste") == 0) {
- printf("Trouvé teste !\n");
- exit(0);
- }
- if (index != maxDepth - 1)
- bruteImpl(-1, 0, str, index + 1, maxDepth);
- } else {
- for (int i = start; i < 63; ++i)
- {
- str[index] = *alphabet[i];
- if (strcmp(str, "teste") == 0) {
- printf("Trouvé teste !\n");
- exit(0);
- }
- if (index != (maxDepth - 1))
- bruteImpl(-1, 0, str, index + 1, maxDepth);
- }
- }
- }
- void bruteSequential(int start)
- {
- int save_start = start;
- for (int i = 1; i <= MAX_LEN; i++) {
- while (start < 63) {
- char* buf = malloc(i+ 1);
- for (int j = 1; j <= i; ++j)
- {
- memset(buf, 0, i + 1);
- bruteImpl(start, start, buf, 0, j);
- }
- free(buf);
- start += nbThreads;
- }
- start = save_start;
- }
- }
- void start_routine(int *start) {
- bruteSequential(*start);
- free(start);
- }
- void start_routine2(char *string) {
- raise(SIGUSR1);
- }
- int main(int argc, char **argv) {
- if (argc < 3) {
- printf("Error : Too few arguments given !\n");
- return 1;
- }
- if (argc > 3) {
- printf("Error : Too much arguments given !\n");
- return 1;
- }
- if (atoi(argv[1]) == 0) {
- printf("The first argument \"%s\"is not valid (not integer)\n", argv[1]);
- return 1;
- }
- nbThreads = atoi(argv[1]);
- signal(SIGUSR1, signal_handler);
- threads = (pthread_t*) malloc(sizeof(pthread_t) * nbThreads);
- for (int i = 0; i < nbThreads; i++) {
- int *start = (int*) malloc(sizeof(int));
- *start = i;
- pthread_create(&threads[i], NULL, (void * (*) (void *)) start_routine, start);
- }
- for (unsigned int j = 0; j < nbThreads; j++) {
- pthread_join(threads[j], NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement