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, hw 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.
Hint: 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.
Hint: 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 hw myDir
time hw myDir
Thread has found primes in filetxt
Thread has found primes in filetxt
real ms
user ms
sys m
Here is my code:
#include
#include
#include
#include
#include
#include
#define MAXTHREADS
Global variables
int activethreads ;
semt mutex;
Function to count prime numbers in a file
void countprimesvoid arg
Extract filespecific information from the argument
char filename char arg;
int threadnumber int char arg;
Implementation of prime counting in a file
This is a placeholder. Replace it with actual prime counting logic.
printfThread d has found some primes in s
threadnumber, filename;
Increment activethreads when entering
semwait&mutex;
activethreads;
sempost&mutex;
Critical section process the file
Count primes in the file
Decrement activethreads when leaving
semwait&mutex;
activethreads;
sempost&mutex;
freearg; Free memory allocated for the argument
pthreadexitNULL;
int mainint argc, char argv
Check commandline arguments
if argc
fprintfstderr "Usage: s directoryName threadNumber
argv;
return EXITFAILURE;
Open the directory
DIR dir;
struct dirent ent;
if dir opendirargv NULL
perrorError opening directory";
return EXITFAILURE;
Initialize semaphore
seminit&mutex, atoiargv;
Loop through files in the directory
int threadnumber ; Initialize thread number
while ent readdirdir NULL
Skip and directories
if strcmpentdname, strcmpentdname,
continue;
Construct argument for the thread
char arg malloc sizeofchar ;
arg entdname;
argchar mallocsizeofint;
int arg threadnumber;
Create a new thread to count primes in the file
pthreadt tid;
pthreadcreate&tid, NULL, countprimes, arg;
Increment thread number
threadnumber;
Wait if the maximum number of active threads is reached
while activethreads atoiargv
Implement wait logic here
Close directory
closedirdir;
Destroy semaphore
semdestroy&mutex;
return EXITSUCCESS;
and here is output of this code :
Thread has found some primes in filetxt
Thread has found some primes in filetxt
Thread has found some primes in filetxt
Thread has found some primes in filetxt
real ms
user ms
sys ms;
Can you modify my code so its counting the primes in the files like below
Thread has found primes in filetxt
Thread has found primes in filetxt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
