0% found this document useful (0 votes)
23 views

Chapter2.2-Thread Concurrent

The document discusses four examples of multithreaded programs: 1) A program that calculates statistical values of numbers in separate threads. 2) A program that generates Fibonacci numbers in a separate thread. 3) A program that finds prime numbers below a given number using multiple threads. 4) Modifying an existing date server program to handle each client request in a separate thread.

Uploaded by

22110164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Chapter2.2-Thread Concurrent

The document discusses four examples of multithreaded programs: 1) A program that calculates statistical values of numbers in separate threads. 2) A program that generates Fibonacci numbers in a separate thread. 3) A program that finds prime numbers below a given number using multiple threads. 4) Modifying an existing date server program to handle each client request in a separate thread.

Uploaded by

22110164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CHAPTER 2

2.2.Threads & Concurrencye

1. Write a multithreaded program that calculates various statistical values for a list of
numbers. This program will be passed a series of numbers on the command line and will
then create three separate worker threads . One thread will determine the average of the
numbers, the second will determine the maximum value, and the third will determine the
minimum value.
For example, suppose your program is passed the integers
90 81 78 95 79 72 85
The program will report
The average value is 82
The minimum value is 72
The maximum value is 95
The variables representing the average,minimum, and maximumvalues will be stored
globally. The worker threads will set these values, and the parent thread will output the
values once the workers have exited. (We could obviously expand this program by creating
additional threads that determine other statistical values, such as median and standard
deviation.)

2. The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, ....


Formally, it can be expressed as:
fib0 = 0
fib1 = 1
fibn = fibn−1 + fibn−2
Write a multithreaded program that generates the Fibonacci sequence. This program
should work as follows: On the command line, the user will enter the number of Fibonacci
numbers that the program is to generate. The program will then create a separate thread
that will generate the Fibonacci numbers , placing the sequence in data that can be shared
by the threads (an array is probably the most convenient data structure). When the thread
finishes execution, the parent thread will output the sequence generated by the child
thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the
child thread finishes, the parent thread will have to wait for the child thread to finish. Use
the techniques described in Section 4.4 to meet this requirement

3. Write a multithreaded program that outputs prime numbers. This program should work as
follows: The user will run the program and will enter a number on the command line. The
program will then create a separate thread that outputs all the prime numbers less than or
equal to the number entered by the user.
Extend:
N: prime <N
T: number of thread
1 thread: N/T
Result: T threads return => parent thread output

Prime(x,y).
4. Modify the socket-based date server so that the server services each client request in a
separate thread.

You might also like