Tuesday, April 26, 2011

Dining-Philosophers Problem

This one springs from Ash(es)... ;)

#include<semaphore.h>
#include<stdio.h>

sem_t f[5],r;
void philosopher(void * i)
{
  int *p = (int*)i;
  while(1)
    {
    printf("\nphilosopher %d is Thinking",*p);
    sleep(2);
    sem_wait(&r);
    sem_wait(&f[*p]);
      sem_wait(&f[(*p+1)%5]);
      printf("\nPhilosopher %d eating",*p);
      sleep(5);
      sem_post(&f[(*p+1)%5]);
      sem_post(&f[*p]);
    sem_post(&r);
    }
    }


int main()
{
  pthread_t ph[5];
  pthread_attr_t *a = NULL;
  int p[] ={0,1,2,3,4};
  int i;
  sem_init(&r,0,4);
  for(i=0;i<5;i++)
    sem_init(&f[i],0,1);
  pthread_create(&ph[0],a,philosopher,(void *)&p[0]);
  pthread_create(&ph[1],a,philosopher,(void *)&p[1]);
  pthread_create(&ph[2],a,philosopher,(void *)&p[2]);
  pthread_create(&ph[3],a,philosopher,(void *)&p[3]);
  pthread_create(&ph[4],a,philosopher,(void *)&p[4]);
 
  for(i=0;i<5;i++)
    pthread_join(ph[i],NULL);
  return 0;
}

Reader-Writer Problem

Small and simple

#include<stdio.h>
#include<semaphore.h>

sem_t x,wsem;
int rc;
int data;
void* reader(void *p)
{
    int *i=(int*)p;
    while(1)
    {
        sem_wait(&x);
        rc++;
        if(rc==1)
            sem_wait(&wsem);
        sem_post(&x);
        printf("Reader %d Data= %d\n",*i,data);
        sem_wait(&x);
        rc--;
        if(rc==0)
            sem_post(&wsem);
        sem_post(&x);
        sleep(2);
    }
}
void* writer(void *p)
{
    while(1)
    {
        sem_wait(&wsem);
        data=random()%100;
        printf("Writer wrote data= %d\n",data);
        sem_post(&wsem);
        sleep(2);
    }
}
int main()
{
    int i;
    pthread_t readerTh[4],writerTh;
    int p[]={1,2,3,4};
    sem_init(&x,0,1);
    sem_init(&wsem,0,1);
    for(i=0;i<4;i++)
        pthread_create(&readerTh[i],NULL,reader,(void*)&p[i]);
    pthread_create(&writerTh,NULL,writer,NULL);
    pthread_join(writerTh,NULL);
    return;
}

Producer-Consumer Problem

A sweet implementation of producer consumer courtesy of anurag mathur

#include<semaphore.h>
#include<stdio.h>
#include<sys/types.h>

sem_t n,mutex,e;
int buf,*Buffer,Count;

void producer()
{
    int i;

    while(1)
    {   
        i=random()%100;
        printf("Producer producing the item: %d\n",i);
        sleep(2);
        printf("The producer produced an item: %d \n",i);       
        sem_wait(&e);
        sem_wait(&mutex);
        Buffer[Count++]=i;
        printf("producer added an item : %d\n",i);
        sem_post(&n);
        sem_post(&mutex);
    }
}
void consumer()
{
    sem_wait(&n);
    while(1)
    {
        sem_wait(&n);       
        sem_wait(&mutex);
        printf("consumer took one item %d\n",Buffer[--Count]);
        sem_post(&e);
        sem_post(&mutex);
        printf("Consumer consuming the item %d \n", Buffer[Count]);
        sleep(2);
        printf("consumer consumed the item %d\n",Buffer[Count]);
    }
}
int main()
{
    pthread_t con,prod;   
    pthread_attr_t *a=NULL;
    sem_init(&n,0,0);
    sem_init(&mutex,0,1);
    printf("Enter the buffer size\n");
    scanf("%d",&buf);
    Buffer= (int *)calloc(buf,sizeof(int));
    sem_init(&e,0,buf);
    int *ptr=NULL;
    pthread_create(&prod,a,&producer,ptr);
    pthread_create(&con,a,&consumer,ptr);
   
    pthread_join(prod,NULL);
    pthread_join(con,NULL);
   
    return 0;
}