Lab 5: Threads Creation Using Posix Api: Pthread

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lab 5: Threads creation using posix api

CS 2203: Operating Systems Spring 2019


Center for Advance Studies in Engineering (CASE) Computer Science Program
Lab 06 Threads creation using posix api
Lab Instructors: Waqas Ahmed, Danish Khatak

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>

int sum; /* shared sum by threads – global variable */


void *runner (void *param); /* thread start function */

void *runner (void *param)


{
int i;
int upper;
upper = atoi(param);
sum = 0;
for (i = 1; i <= upper; ++i)
sum += i;
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t tid; /* id of the created thread */
pthread_attr_t attr; /* set of thread attributes */
if (argc != 2)
{
fprintf (stderr, “usage: a.out <value>\n”);
return -1;
}
if (atoi(argv[1]) < 0)
{
fprintf (stderr, “%d must be >= 0\n”, atoi(argv[1]);
return -1;
}

pthread_create (&tid, NULL, runner, argv[1]);


pthread_join (tid, NULL);
printf (“sum = %d\n”, sum);
}

Objectives

• Learn to create threads in a process using posix pthread api.

Credits: Operating System concepts by silbershatz 9th edition.

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 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 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.

You might also like