Print numbers in sequence using thread synchronization in C Program.

CServer Side ProgrammingProgramming

Given with the threads the program must print the thread based on their priorities starting from 0 to 10.

What is a thread?

Thread is lightweight process that runs inside a program. A simple program can contain n number of threads.

Unlike java, multithreading is not supported by the language standards, POSIX Threads (Pthreads) is the standard used in multithreading in C/C++. C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature.

How it works in our program?

To use the thread functions we use header file #include. This header file will include all the functions related to thread in our program like pthread_create(), etc.

Now the task is to synchronize n number of threads using pthread standard library present with gcc compiler. The idea is take thread count and print 1 in first thread, print 2 in second thread, print 3 in third thread till 10th thread. The output will contain numbers from 1 to 10 based upon the priorities of the threads.

Algorithm

Start
Step 1 -> Declare global variables as int MAX=10 and count=1
Step 2 -> declare variable thr of pthread_mutex_t and cond of pthread_cond_t
Step 3 -> Declare Function void *even(void *arg)
   Loop While(count < MAX)
      Call pthread_mutex_lock(&thr)
      Loop While(count % 2 != 0)
         Call pthread_cond_wait(&cond, &thr)
      End
      Print count++
      Call pthread_mutex_unlock(&thr)
      Call pthread_cond_signal(&cond)
   End
   Call pthread_exit(0)
Step 4 -> Declare Function void *odd(void *arg)
   Loop While(count < MAX)
      Call pthread_mutex_lock(&thr)
      Loop While(count % 2 != 1)
         Call pthread_cond_wait(&cond, &thr)
      End
      Print count++
      Call pthread_mutex_unlock(&thr)
      Call pthread_cond_signal(&cond)
   End
   Set pthread_exit(0)
Step 5 -> In main()
   Create pthread_t thread1 and pthread_t thread2
   Call pthread_mutex_init(&thr, 0)
   Call pthread_cond_init(&cond, 0)
   Call pthread_create(&thread1, 0, &even, NULL)
   Call pthread_create(&thread2, 0, &odd, NULL)
   Call pthread_join(thread1, 0)
   Call pthread_join(thread2, 0)
   Call pthread_mutex_destroy(&thr)
   Call pthread_cond_destroy(&cond)
Stop

Example

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
void *even(void *arg){
   while(count < MAX) {
      pthread_mutex_lock(&thr);
      while(count % 2 != 0) {
         pthread_cond_wait(&cond, &thr);
      }
      printf("%d ", count++);
      pthread_mutex_unlock(&thr);
      pthread_cond_signal(&cond);
   }
   pthread_exit(0);
}
void *odd(void *arg){
   while(count < MAX) {
      pthread_mutex_lock(&thr);
      while(count % 2 != 1) {
         pthread_cond_wait(&cond, &thr);
      }
      printf("%d ", count++);
      pthread_mutex_unlock(&thr);
      pthread_cond_signal(&cond);
   }
   pthread_exit(0);
}
int main(){
   pthread_t thread1;
   pthread_t thread2;
   pthread_mutex_init(&thr, 0);
   pthread_cond_init(&cond, 0);
   pthread_create(&thread1, 0, &even, NULL);
   pthread_create(&thread2, 0, &odd, NULL);
   pthread_join(thread1, 0);
   pthread_join(thread2, 0);
   pthread_mutex_destroy(&thr);
   pthread_cond_destroy(&cond);
   return 0;
}

Output

if we run above program then it will generate following output

1 2 3 4 5 6 7 8 9 10
raja
Published on 22-Aug-2019 07:15:18
Advertisements