Threads, Pipes Eratosthenes' Sieve

This project received 8 bids from talented freelancers with an average bid price of $133 USD.

Get free quotes for a project like this
Project Budget
$30 - $250 USD
Total Bids
Project Description

Hey, in need of a project in 6 hours?

Threads, Pipes

Eratosthenes' Sieve

Eratosthenes' sieve is a method for generating prime numbers. Our goal is to implement it using threads and pipes. The task may occur difficult to do at once, so it is divided into several steps.


[10 p.]

Here is the algorithm:

A thread named Generator generates consecutive natural numbers (> 1) (until it reaches the limit given as argument) and writes them to a pipe.

The numbers are received by another thread named Sieve, created by the generator.

The sieving thread stores the first number read from the pipe. It is a prime number.

Afterwards it creates new sieving thread and transfers to it (using another pipe) the rest of the received numbers except for numbers divisible by the stored prime number.

Then the sieving thread returns (using another pipe) the stored prime number to the thread, from which it received the numbers.

Next, the sieving thread reads prime numbers from the (another, not used yet) pipe connecting it and the sieving thread it created earlier and sends them to its parent (the thread, to which the stored prime was sent; it is the thread, that created this one).

Having generated all the natural numbers, the main thread - the Generator - starts reading prime numbers from the pipe connecting it and its descendant and writes them to the command prompt.


Step 1

Create the class Generator. An object of this class is a thread generating integer numbers (>1).


Step 2

Create the class Sieve. An object of this class is a thread reading numbers from the pipe connecting it to the generator.


A stream object must be passed to the receiver thread as an argument to a constructor.

A pipe stream must be decorated with a data stream (DataOutputStream or DataInputStream) to enable transfer of primitive types (integers).

Having printed all the received numbers, both threads should terminate cleanly. To achieve this, the special value 0 may be sent as a termination signal (EOD, end of data).


Step 3

The receiver thread passes all the received numbers back to the sender using another pipe.

The sender (the generator) prints them out to the command prompt.


Step 4

The sieving thread creates another sieving thread and transfers to it all the numbers it has received.

The created sieving thread (descendant) reads the numbers and sends them back to the sender immediately (using another pipe).

The sieving thread created by the generator receives the numbers from the descendant and returns them to the generator, who prints them.

There are 3 threads here.


Step 5, the last

The sieving thread does not pass all the numbers it gets to the descendant. It passes only those of them that are not divisible by the first number it got (and stored).

Each sieving thread creates another sieving thread until there are any numbers left in the transferred sequence.

Besides the stored prime number, it passes to the parent thread all the numbers received from the descendant.



Each sieving thread must take 2 stream objects as arguments to its constructor (only in the final version).

All the streams must be closed properly.

Each thread should print lots of diagnostic messages to ease debugging. However, a correctly working program should print only the prime numbers.

Skills Required

Looking to make some money?

  • Set your budget and the timeframe
  • Outline your proposal
  • Get paid for your work

Hire Freelancers who also bid on this project

    • Forbes
    • The New York Times
    • Time
    • Wall Street Journal
    • Times Online