Question: Your program must first open the directory passed to it as a command line argument ( for example, . / hw 2 directoryName threadNumber )

Your program must first open the directory passed to it as a command line argument (for
example, ./hw2 directoryName threadNumber). The program is to use threads to compute the
number of prime numbers in each file and print thread by thread. The number of active threads
at a time is to be limited to threadNumber.
Hint1: use a semaphore (mutex) to limit the number of simultaneously active threads
(threadNumber). Create a threads whenever an active thread terminates. Your program must
work for any number of files.
Hint2: you may heck number of active threads per process by executing ps -eLf command in
a second terminal window under NLWP column, after your program starts execution.
Notes about the problem: you may think of this problem as if an unknown number of students
are queued in front of an instructors office to see their exam papers. But he instructors office
can only host at most n (threadNumber) students. So, the new student can enter the room, only
when one of the students inside leaves the room.
Execute your program observing time data:
time ./hw2 myDir 1
time ./hw2 myDir 2
...
Thread 1 has found 6454 primes in file1.txt
...
Thread 5 has found 23935 primes in file2.txt
...
real 0m54.277s
user 1m48.500s
sys 0m0.
Here is my code:
#include
#include
#include
#include
#include
#include
#define MAX_THREADS 100
// Global variables
int active_threads =0;
sem_t mutex;
// Function to count prime numbers in a file
void *count_primes(void *arg){
// Extract file-specific information from the argument
char *filename =((char **)arg)[0];
int thread_number =*((int *)(((char **)arg)[1]));
// Implementation of prime counting in a file
// This is a placeholder. Replace it with actual prime counting logic.
printf("Thread %d has found some primes in %s
", thread_number, filename);
// Increment active_threads when entering
sem_wait(&mutex);
active_threads++;
sem_post(&mutex);
// Critical section - process the file
// Count primes in the file
// Decrement active_threads when leaving
sem_wait(&mutex);
active_threads--;
sem_post(&mutex);
free(arg); // Free memory allocated for the argument
pthread_exit(NULL);
}
int main(int argc, char *argv[]){
// Check command-line arguments
if (argc !=3){
fprintf(stderr, "Usage: %s directoryName threadNumber
", argv[0]);
return EXIT_FAILURE;
}
// Open the directory
DIR *dir;
struct dirent *ent;
if ((dir = opendir(argv[1]))== NULL){
perror("Error opening directory");
return EXIT_FAILURE;
}
// Initialize semaphore
sem_init(&mutex, 0, atoi(argv[2]));
// Loop through files in the directory
int thread_number =1; // Initialize thread number
while ((ent = readdir(dir))!= NULL){
// Skip "." and ".." directories
if (strcmp(ent->d_name, ".")==0|| strcmp(ent->d_name, "..")==0){
continue;
}
// Construct argument for the thread
char **arg = malloc(2* sizeof(char *));
arg[0]= ent->d_name;
arg[1]=(char *)malloc(sizeof(int));
*((int *)arg[1])= thread_number;
// Create a new thread to count primes in the file
pthread_t tid;
pthread_create(&tid, NULL, count_primes, arg);
// Increment thread number
thread_number++;
// Wait if the maximum number of active threads is reached
while (active_threads >= atoi(argv[2])){
// Implement wait logic here
}
}
// Close directory
closedir(dir);
// Destroy semaphore
sem_destroy(&mutex);
return EXIT_SUCCESS;
}
and here is output of this code :
Thread 1 has found some primes in file1.txt
Thread 2 has found some primes in file10.txt
Thread 3 has found some primes in file11.txt
Thread 4 has found some primes in file12.txt...
real 0m0.006s
user 0m0.003s
sys 0m0.000s;
Can you modify my code so its counting the primes in the files like below
Thread 1 has found 6454 primes in file1.txt
...
Thread 5 has found 23935 primes in file2.txt

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!