This program is a multi-threaded spellchecker that allows multiple clients to connect to a local network and see if an entered word is spelled correctly. The program consists of a main thread, a pool of worker threads, and a logger thread.
The program accepts a dictionary text file as a command line parameter. If no text file is specified, the default dictionary.txt is used. The program also prompts for a port number, allowing the user to set their own value for a port number as long as it is in the range of 1024 and 65535. If no port number is specified (user enters ‘1’), the default port number is used.
Once these values are set, the dictionary file is read and stored into char* dictionary. Next, the worker thread pool and the logger thread are created using pthread_create. Then, a socket is created, allowing clients to connect locally to the program.
The worker threads, main thread, and logger thread all run concurrently. The worker threads receive jobs from a job queue, which is protected by a mutex lock and condition variables so that adding and removing from the queue is done safely. “Jobs” consist of the job’s associated client socket, client number, and word entered by the user. When a word is processed, the worker thread searches the dictionary data structure for the user’s entry. If the entry is found in the dictionary, the worker reports that the word is spelled correctly. If the entry is not found in the dictionary, the worker reports that the word is spelled incorrectly. Next, the worker thread sends the job to the log queue, and the logger thread takes in jobs from the log queue and writes the result to log.txt. The log queue is also protected by a mutex lock and condition variables for safe access. The workers are able to run as long as the buffer is not full. If the buffer is full, the threads wait until there is space in the buffer. Additionally, if the buffer is empty, the threads wait until there is a job in the buffer to be processed. This is done using pthread_cond_wait.
Client connections last until the user disconnects by entering the escape key.