EmbLogic's Blog

Pthreads program to find Sum of first n-natural numbers,thread synchronization is achieved by using sleep() system call.

RCS file: sum_of_n_natural_number.c,v
Working file: sum_of_n_natural_number.c
head: 1.7
branch:
locks: strict
root: 1.7
access list:
symbolic names:
keyword substitution: kv
total revisions: 7;    selected revisions: 7
description:
Code to demonstrate the sum of first n-natural numbers using Pthreads.
—————————-
revision 1.7    locked by: root;
date: 2014/01/25 05:49:00;  author: root;  state: Exp;  lines: +1 -0
The program is compiling successfully.
Logical error is fixed.
Issue:The threads are attempting to modify the shared resource i.e the global variable sum at the same time.
Bug is fixed by using sleep system call inside the main thread after the creation of thread.
The result is getting displayed correctly.
—————————-
revision 1.6
date: 2014/01/25 05:46:11;  author: root;  state: Exp;  lines: +4 -1
Definition of the function is given.
pthread_exit(NULL) is added inside the function body to terminate the thread.
Checking the result….
—————————-
revision 1.5
date: 2014/01/25 05:43:27;  author: root;  state: Exp;  lines: +5 -0
Prototype of the function is declared.
Defining the body of the calculate_sum() function……
—————————-
revision 1.4
date: 2014/01/25 05:41:08;  author: root;  state: Exp;  lines: +3 -0
Header file:pthread.h and stdlib.h is included to support inbuilt functions.
—————————-
revision 1.3
date: 2014/01/25 05:37:31;  author: root;  state: Exp;  lines: +21 -1
An array of pthread_t type is created that will store the thread-id’s.
Inside the loop,pthread_create() function is called that will create thread and prints an appropriate message.
As soon as the thread gets created it will start executing calculate_sum() function.
—————————-
revision 1.2
date: 2014/01/25 05:30:59;  author: root;  state: Exp;  lines: +7 -1
A global variable “sum” is declared that acts as a shared resourse and is accessible to the threads.
defining the body of the main thread.
—————————-
revision 1.1
date: 2014/01/25 05:29:53;  author: root;  state: Exp;
Initial revision
=============================================================================

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>