Sunday, July 17, 2011

Network Management Summer training !!

JAI Ho !! :D
It was plain awesomeness!! nothing short of it. just one rule, one motto: study hard and party hard ! and yeah dont forget the chocolate.


 To quote Swapan Sir: hum hum hai baki pani kam hai !
                                      abe sun to le !
                                      wo bamboo dekha hai hara walaa !! :D
                                      abe dabba hai dabba !
                                      ye dekho gaadha kidhar milta hai nettech ka class me milta hai !
                                      hehehaaaahahaaaaaa ! :D

As for pre-requisites... they scratch the scratch there. so no worries for newbies.
And the fun never stops. They make technical lectures a sport. nothing like the boring shit we have to put up with in college.

A must attend for network enthusiasts who need some introduction in the line.

Nettech Home: nettech.in

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;
}

Sunday, January 16, 2011

To Write a Blog

What is a blog? I googled it and found many meaning... a personal diary...a web log-maintained by an individual with regular entries of his actions and achievements.... and on and on...
Some people told me its a way to express yourself, to share your feelings and emotions. Others told me to use it to describe and popularize any piece of technology i liked.
But i wanted to be unique. i wanted to be different. So i start this blog not by my autobiography, nor by any technical discussion nor any emotional trauma i feel. I start this blog with what every child starts his life with...a story :)

Once upon a time, there lived a boy. He was not very handsome or smart or dashing or awesome in any manner; just a normal boy who believed in abnormal things. He considered himself to be a little abnormal, not with any fantastic magical powers or super strength or intelligence, but just abnormal in a very normal way.
He lived among the common people doing common stuff. But he had big dreams. Dreams he cherished and wanted to fulfill. He wanted to be a wealthy grownup man, content and satisfied, loved and happy. He wanted to be honest, good at heart, helpful, friendly and caring. He wanted comforts sure but was never wanted them greedily so. But no matter how hard he tried, he always lacked in being such. Some where or the other his own good and greed would take over his conscience and he would say or do something that would hurt others or simply benefit him without concern for others. The boy, whenever realized his mistakes regretted them but could never fully conquer them, bound to the human destiny of imperfection as he was...
But the boy never gave up trying. He continued to do mistakes, fail and succeed but on he went; or more like life pushed him on. He did get depressed at times and lost all hope, considered himself doomed in darkness and sadness. But he had good friends- friends who supported him and guided him, cheered him and gave him hope. And so he moved on through his boyhood to become a remarkable man.
He earned himself name and wealth, recognition and respect and fulfilled all his dreams---> he bought himself a huge estate, where he made a natural sanctuary for animals... he wanted to have horse, dogs, lions and tigers in his sanctuary... he wanted to be friends with them and play with them all... he wanted to wear nine gems on his fingers... and to be dear to people who d remember him and make him immortal in their memories... and so he did.

When his time came, he accepted death  with reverence, remembering the lines of Tithonus, and saw in his memories someone... someone 19 years old and writing..... a blog.