Pages

Subscribe:

Ads 468x60px

Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts

Wednesday, January 23, 2013

Producer Consumer Problem in C++

Today I'm going to solve a most common problem which is known as the consumer producer problem. In this context we have a shared buffer which the producers produce and the consumers consume. The consumers and producers are threads which will simultaneously produce and consume. There are some conditions to be met where consumers have to wait until producers produce, and another thing is that when the buffer is full producers must halt until the consumers consume.

In the following example was implemented using pthreads. The producer produce a random number to the buffer and consumers consume that number. Mutex locks are used to protect the shared buffer. 
ProducerConsumer.cpp     DOWNLOAD SOURCE

#include <iostream>
#include <pthread.h>
#include <vector>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>



#define BUFFER_SIZE 10

void InitializeData();
void *Produce(void *);
void *Consume(void *);
int InsertItem(int);
int RemoveItem(int *);

int iCounter;
pthread_mutex_t mutex;
sem_t full, empty;

int buffer[BUFFER_SIZE];



int main(int argc , char * argv[])
{
 InitializeData(); 
 pthread_t ProducerThread , ConsumerThread;
 int *aa = new int [10];
 for(int i = 0 ; i < 10 ; i++)
 {
  aa[i] = i;
  pthread_t t;
  pthread_create(&t , NULL, Produce , &aa[i]);
  printf("Creating Producer %d \n", i);
 }
 int *bb = new int[10];
 for(int i = 0 ; i < 10 ; i++)
 {
  bb[i] = i;
  pthread_t t;
  pthread_create(&t , NULL, Consume , &bb[i]);
  printf("Creating Consumer %d \n", i);
 }
 
 sleep(5);
 delete [] aa;
 delete [] bb;
 

 return 0;
}

//****************************************************************************************************
void InitializeData()
{
 pthread_mutex_init(&mutex , NULL);
 sem_init(&full , 0 ,0);
 sem_init(&empty , 0 , BUFFER_SIZE);
 
 iCounter = 0;
 

}

//****************************************************************************************************
void * Produce(void * Param)
{
 int item;
 
 while(1)
 {
  //sleep(1);
  item = rand() % 100;
  sem_wait(&empty);
  pthread_mutex_lock(&mutex);
  int iMsg = InsertItem(item);
  
  if(iMsg == -1){
   printf("Error Inserting Item \n");
  }else
  {
   printf("Produced Item :: %d  Thread No :: %d\n", item , *((int *)Param));
  }
  pthread_mutex_unlock(&mutex);
  sem_post(&full);
  
 }
}

//****************************************************************************************************
void * Consume(void * Param)
{
 int item;
 
 while(1)
 {
  //sleep(1);
  sem_wait(&full);
  pthread_mutex_lock(&mutex);
  int iMsg = RemoveItem(&item);
  
  if(iMsg == -1){
   printf("Error Removing Item \n");
  }else
  {
   printf("Consumed Item :: %d  Thread No :: %d \n", item ,*((int *)Param));
  }
  pthread_mutex_unlock(&mutex);
  sem_post(&empty);
  
 }
}

//****************************************************************************************************
int InsertItem(int item)
{
 if(iCounter < BUFFER_SIZE)
 {
  buffer[iCounter] =  item;
  iCounter++;
  return 1;
 }
 else{
  return -1;
 }

}

//****************************************************************************************************
int RemoveItem(int *item)
{
 if(iCounter > 0)
 {
  *item = buffer[iCounter - 1];
  iCounter--;
  return 1;
 }
 else{
  return -1;
 }
}

That's it folks. Hope to see you soon in another exciting tutorial.

Thursday, February 9, 2012

What are these System Calls?

What are these System calls with respect to Operating Systems?. More simple definition I could give you is that System Calls is the interface between the Operating System and the User Programs. These functions may be vary with respect to the Operating System Type. Systems calls are machine dependent and in Unix Systems System Calls are written in assembly language and there is a library available to initiate those functions within a C program.

When we run our programs mostly in user mode, the programs may need a System service such as writing some buffered data to a file. Then the execution of the User Program put into a TRAP mode and the control is transferred into the Operating System. Then by examining the method called, the kernel will handle the necessary procedures and return the control to the User Program by sending the relevant message.  

Here is a example for calling the write data from a buffer into a file.
             n = write(fd, buffer, nbytes)
Example write System call
Lets examine this example write system call. [1] , [2] and [3] step shows that the parameters of the write method is pushed into the stack. See the order which they are pushed into the stack. The rightmost parameter is pushed first followed by others from right to left. 
*Reason behind this is that in earlier days when printf method is called the format of the string appears on top of the stack.
Then it comes to the call write and it will execute the library procedure for the write [4]. Then step [5] is to put the system call number for write in a register. Step [6] shows the execution of the TRAP instruction to switch modes, from USER MODE to the KERNEL MODE. Then in the KERNEL MODE it examines the system call number and  the relevant system call handler will be pointed via the table indexed all the system calls and the system call handlers [7]. In this moment [8] system call handler runs.
Once the execution completed the control is returned to the USER MODE library procedure [9]. Then the library procedure returns to the user program [10]. To complete the execution it will clear the stack [11].

That is the typical  life cycle of a system call. In POSIX systems there are around hundred system calls. System calls are used in various things Process and File management etc.

Hope you got some knowledge in system calls.. See you next time :)