What’s wrong with my program?

0 comments

My issue: My program does not work for the input of 1,000,000 and I am not sure what I’m doing wrong. When I try to input 1,000,000 it stops at around 180,000

Write a C program that will create threads (Prime Thread and Print Thread) that will find and print the prime numbers between 0 and an integer number that the user provides. There is no limit on the number of threads and you can create as many threads as you need.

The prime numbers will be stored in a shared buffer named “Prime”. The threads will take turns between finding the prime numbers and printing the found numbers.

MAIN
The main will get a number from the user and store it in a variable named “Max_Num” and will create the threads (Prime Thread and Print Thread) to find and print the prime numbers.

Prime Thread and Print Thread

The Prime threads will find the prime numbers and will store them in a global shared variable “Prime”. The Prime threads will continue to produce prime numbers until the buffer is full then will wait until the Print threads empty the buffer and print it to the screen.

You can use any synchronization technique (lock, mutex, semaphore) we discussed in class. You may need to use a Mutex lock “Lock_Prime” to ensure that only one thread accesses the “Prime” buffer at a time. Your program should run reasonably fast for any given number.

-Your program should be able to adjust the number of threads according to the input size.

-You can use any synchronization technique. Code must be well documented with plenty of comments.

-Your code should work correctly with any input.

-While some delay is accepted, results should be ready within a reasonable time.

-Your program should be able to accept inputs of 10000, 100000, and 1000000. For 1000000 a reasonable execution time is < 5 minutes.

– The waiting for the completion of threads should occur at the end i.e. threads need to be created before any of the joining happens.

– The program should print the amount of prime numbers within the given range, as a check

Example: N = 10

Acceptable: 2,3,5,7 3,2,5,7 5,7,2,3

Not Acceptable: 2,2,3,5,7 2,3,5

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}