Lab 5: Threads Creation Using Posix Api: Pthread
Lab 5: Threads Creation Using Posix Api: Pthread
Lab 5: Threads Creation Using Posix Api: Pthread
Pthread
The Pthreads library is a POSIX C API thread library that has standardized functions for using
threads across different platforms. Historically, hardware vendors have implemented their own
proprietary versions of threads. These implementations differed substantially from each other
making it difficult for programmers to develop portable threaded applications. In order to take full
advantage of the capabilities provided by threads, a standardized programming interface was
required. For UNIX systems, this interface has been specified by the IEEE POSIX 1003.1c standard
(1995). Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads.
Most hardware vendors now offer Pthreads in addition to their proprietary API's. Pthreads are
defined as a set of C language programming types and procedure calls. Vendors usually provide a
Pthreads implementation in the form of a header/include file and a library that you link with your
program.
Thread Creation
#include <pthread.h>
#include <stdio.h>
Objectives
Problem 4.26
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, ....
Formally, it can be expressed as:
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.
Problem 4.21
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 variables representing the average, minimum, and maximum values 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.)
Problem 4.24
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.