The following program only needs some changes which are asked below. Please don't write this program with a pen. Please

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

The following program only needs some changes which are asked below. Please don't write this program with a pen. Please

Post by answerhappygod »

The following program only needs some changes which areasked below. Please don't write this program with a pen. Please fixthe program in Linux and if you upload a screenshot of the output,that will help me a lot.
Please copy this program and past it into your Linuxterminal run it and you will understand what itneeds.
upload the screen of the output.
In this assignment, we would be using pthread and Mutexfunctions to write the number of primes. But you would becreating 10 threads.
Also, in line #34, we have 200000. Change this valueso the user can pass the value from the command line. Say
a.out 200000
or
a.out 100000
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/times.h> 4 #include <time.h> 5 #include <unistd.h> 6 #include <pthread.h> 7 8 9 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; // KEY 10 11 int count = 1; // SHARED VARIABLE AMONGTHREADS 12 int numOfPrimes = 0; 13 void countNumberOfPrimeNumber( void *empty ) 14 { 15 16 do { 17 int num = 2; 18 pthread_mutex_lock (&lock); // blocked 19 int localCount =++count; // VERY IMPORTANT 20 pthread_mutex_unlock (&lock); 21 22 for ( num = 2 ; num < localCount ; num++) 23 { 24 if ( localCount%num == 0) 25 break; 26 } 27 if ( num == localCount) { 28 pthread_mutex_lock (&lock); // blocked 29 numOfPrimes++; 30 pthread_mutex_unlock (&lock); 31 //printf ( "prime number : %d %d \n",numOfPrimes, count); 32 } 33 34 } while ( count < 200000 ); 35 } 36 37 main() 38 { 39 pthread_t T1, T2; 40 int id1, id2; 41 clock_t start, end; 42 43 struct tms st_start, st_end; 44 start = times (&st_start); 45 46 pthread_t t1 , t2, t3; // twothreads 47 int thread1 ; 48 int thread2 ; 49 int thread3 ; 50 thread1 = 51 pthread_create ( &t1, NULL, (void*)countNumberOfPrimeNumber, (void *) NULL); 52 thread2 = 53 pthread_create ( &t2, NULL, (void*)countNumberOfPrimeNumber, (void *) NULL); 54 thread3 = 55 pthread_create ( &t3, NULL, (void*)countNumberOfPrimeNumber, (void *) NULL); 56 57 if (thread1 == 0) 58 printf (" Thread 1 success\n"); 59 if (thread2 == 0) 60 printf (" Thread 2 success\n"); 61 if (thread3 == 0) 62 printf (" Thread 3 success\n"); 63 64 pthread_join( t1 , NULL); 65 pthread_join( t2 , NULL); 66 pthread_join( t3 , NULL); 67 68 69 end = times (&st_end); 70 71 printf ( "Time taken by system command %6.2f \n", (float)(end-start)/sysconf( _SC_CLK_TCK ) ); 72 73 printf ( "Time taken by parent process in User mode %6.2f\n", (float)( st_end.tms_utime)/sysconf( _SC_CLK_TCK ) ); 74 printf ( "Time taken by parent process in Kernel mode%6.2f \n", (float)( st_end.tms_stime)/sysconf( _SC_CLK_TCK )); 75 76 printf ( "Time taken by child process in user mode %6.2f\n", (float)( st_end.tms_cutime)/sysconf( _SC_CLK_TCK ) ); 77 printf ( "Time taken by child process in kernel mode %6.2f\n", (float)( st_end.tms_cstime)/sysconf( _SC_CLK_TCK ) ); 78 79 80 printf ( "Number of prime numbers : %d\n", numOfPrimes ); 81 82 }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply