Tuesday, 12 May 2015

Program 8:
AIM: Write a C Program to implement Producer – consumer problem using semaphores using UNIX/LINUX system calls.

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

#define SIZE 5
#define NUMB_THREADS 6
#define PRODUCER_LOOPS 2

typedef int buffer_t;
buffer_t buffer[SIZE];
int buffer_index;

pthread_mutex_t buffer_mutex;

sem_t full_sem;  /* when 0, buffer is full */
sem_t empty_sem; /* when 0, buffer is empty. Kind of
                    like an index for the buffer */




void insertbuffer(buffer_t value) {
    if (buffer_index < SIZE) {
        buffer[buffer_index++] = value;
    } else {
        printf("Buffer overflow\n");
    }
}

buffer_t dequeuebuffer() {
    if (buffer_index > 0) {
        return buffer[--buffer_index]; // buffer_index-- would be error!
    } else {
        printf("Buffer underflow\n");
    }
    return 0;
}


void *producer(void *thread_n) {
    int thread_numb = *(int *)thread_n;
    buffer_t value;
    int i=0;
    while (i++ < PRODUCER_LOOPS) {
        sleep(rand() % 10);
        value = rand() % 100;
        sem_wait(&full_sem); // sem=0: wait. sem>0: go and decrement it
       
        pthread_mutex_lock(&buffer_mutex); /* protecting critical section */
        insertbuffer(value);
        pthread_mutex_unlock(&buffer_mutex);
        sem_post(&empty_sem); // post (increment) emptybuffer semaphore
        printf("Producer %d added %d to buffer\n", thread_numb, value);
    }
    pthread_exit(0);
}

void *consumer(void *thread_n) {
    int thread_numb = *(int *)thread_n;
    buffer_t value;
    int i=0;
    while (i++ < PRODUCER_LOOPS) {
        sem_wait(&empty_sem);
       
        pthread_mutex_lock(&buffer_mutex);
        value = dequeuebuffer(value);
        pthread_mutex_unlock(&buffer_mutex);
        sem_post(&full_sem); // post (increment) fullbuffer semaphore
        printf("Consumer %d dequeue %d from buffer\n", thread_numb, value);
   }
    pthread_exit(0);
}

int main(int argc, int **argv) {
    buffer_index = 0;

    pthread_mutex_init(&buffer_mutex, NULL);
    sem_init(&full_sem, // sem_t *sem
             0, // int pshared. 0 = shared between threads of process,  1 = shared between processes
             SIZE); // unsigned int value. Initial value
    sem_init(&empty_sem,
             0,
             0);
  
    pthread_t thread[NUMB_THREADS];
    int thread_numb[NUMB_THREADS];
    int i;
    for (i = 0; i < NUMB_THREADS; ) {
        thread_numb[i] = i;
        pthread_create(thread + i, // pthread_t *t
                       NULL, // const pthread_attr_t *attr
                       producer, // void *(*start_routine) (void *)
                       thread_numb + i);  // void *arg
        i++;
        thread_numb[i] = i;
        // playing a bit with thread and thread_numb pointers...
        pthread_create(&thread[i], // pthread_t *t
                       NULL, // const pthread_attr_t *attr
                       consumer, // void *(*start_routine) (void *)
                       &thread_numb[i]);  // void *arg
        i++;
    }

    for (i = 0; i < NUMB_THREADS; i++)
        pthread_join(thread[i], NULL);

    pthread_mutex_destroy(&buffer_mutex);
    sem_destroy(&full_sem);
    sem_destroy(&empty_sem);

    return 0;

}
Program 7):
AIM: Write C programs to illustrate the following IPC mechanisms:
a.     Pipes
b.     Fifos
c.      Message queues
d.     Shared memory


a)  PIPES


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
int fd[2];
char data[100];
pid_t pid;
pipe(fd);
pid=fork();
if(pid==0)
{
close(fd[1]);
read(fd[0],data,100);
printf("Reading from pipe.....\n");
printf("%s",data);
}
else
{
close(fd[0]);
printf("Enter the string.....");
printf("\nWritng to pipe from parent...\n");
gets(data);
write(fd[1],data,100);
wait();
printf("\n");
}
return 0;
}



OUTPUT:


Reading from pipe….
Venky
Enter the string …..
Writing to pipe from parent…



b)  FIFO

      Sender

#includestdio.h
#includefcntl.h
#includesysstat.h
#includeunistd.h
int main()
{
int fd=0;
char data[100];
int len=0;
int i=1;
unlink(datapipe);
mkfifo(datapipe,0660);
fd=open(datapipe,O_RDONLY);
if(fd==-1)
{
printf(Error creating pipe);
return 1;
}
printf(receiver.c);
 
while(1)
{
len=read(fd,data,100);
if(len=0){break;}
data[len]='0';
printf(“\nstring %d!%s--” ,i,data);
i++;
}
close(fd);
unlink(datapipe);
printf(n);
return 0;
}


Receiver

#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdlib.h>
int main()
{
int fd1=0;
char data[100];int len=0;
fd1=open("datapipe",O_WRONLY);
if(fd1==-1)
{
  printf("Error in accessing pipes:");
  exit(0);
}
while(1)
{
  len=read(0,data,100);
  if(len<=0)
     break;
  write(fd1,data,len);
}
  close(fd1);
  printf("\n");
return 0;
}


OUTPUT:


Receiver.c
String 1 venky


Program 6):
AIM: To implement deadlock avoidance & Prevention by using Banker’s Algorithm using Deadlock avoidance & Dead Lock Prevention

#include<stdio.h>
#include<conio.h>
struct da
{
    int max[10],a1[10],need[10],before[10],after[10];
 }p[10];
 void main()
 {
  int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
 clrscr();
 printf("\n ENTER THE NO. OF PROCESSES:");
 scanf("%d",&n);
 printf("\n ENTER THE NO. OF RESOURCES:");
 scanf("%d",&r);
 for(i=0;i<n;i++)
 {
    printf("PROCESS %d \n",i+1);
    for(j=0;j<r;j++)
    {
     printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
     scanf("%d",&p[i].max[j]);
    }
   for(j=0;j<r;j++)
    {
     printf("ALLOCATED FROM RESOURCE %d:",j+1);
     scanf("%d",&p[i].a1[j]);
     p[i].need[j]=p[i].max[j]-p[i].a1[j];
    }
 }
          for(i=0;i<r;i++)
          {
            printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
            scanf("%d",&tot[i]);
          }
          for(i=0;i<r;i++)
          {
            for(j=0;j<n;j++)
            temp=temp+p[j].a1[i];
            av[i]=tot[i]-temp;
            temp=0;
          }
  printf("\n   RESOURCES  ALLOCATED   NEEDED   TOTAL  AVAIL");
  for(i=0;i<n;i++)
  {
    printf("\n P%d \t",i+1);
    for(j=0;j<r;j++)
    printf("%d",p[i].max[j]);
    printf("\t");
    for(j=0;j<r;j++)
    printf("%d",p[i].a1[j]);
    printf("\t\t");
    for(j=0;j<r;j++)
    printf("%d",p[i].need[j]);
    printf("\t");
    for(j=0;j<r;j++)
    {
            if(i==0)
            printf("%d",tot[j]);
    }
    printf("    ");
    for(j=0;j<r;j++)
   {
       if(i==0)
       printf("%d",av[j]);
   }
 }
   printf("\n\n\t AVAIL  BEFORE\T AVAIL AFTER ");
   for(l=0;l<n;l++)
   {
    for(i=0;i<n;i++)
    {
           for(j=0;j<r;j++)
           {
             if(p[i].need[j] >av[j])
             cn++;
             if(p[i].max[j]==0)
             cz++;
           }
    if(cn==0 && cz!=r)
    {
      for(j=0;j<r;j++)
      {
          p[i].before[j]=av[j]-p[i].need[j];
          p[i].after[j]=p[i].before[j]+p[i].max[j];
          av[j]=p[i].after[j];
          p[i].max[j]=0;
      }
      printf("\n P %d \t",i+1);
       for(j=0;j<r;j++)
       printf("%d",p[i].before[j]);
       printf("\t");
       for(j=0;j<r;j++)
       printf("%d",p[i].after[j]);
           cn=0;
           cz=0;
           c++;
           break;
   }
   else
   {
     cn=0;cz=0;
   }
  }
}
 if(c==n)
 printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
    else
    printf("\n DEADLOCK OCCURED");
   
}







Program 5a):
AIM: Write a C program to implement the various process scheduling mechanisms such as FCFS scheduling.

#include<stdio.h>
void main()
{
 int i,n,sum,wt,tat,twt,ttat;
 int t[10];
 float awt,atat;
 clrscr();

 printf("Enter number of processors:\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
   printf("\n Enter the Burst Time of the process %d",i+1);
   scanf("\n %d",&t[i]);
 }
 printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
 printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
 printf("1 \t\t 0 \t\t %d \n",t[0]);
 sum=0;
 twt=0;
 ttat=t[0];
 for(i=1;i<n;i++)
 {
  sum+=t[i-1];
  wt=sum;
  tat=sum+t[i];
  twt=twt+wt;
  ttat=ttat+tat;
  printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
  printf("\n\n");
 }
  awt=(float)twt/n;
  atat=(float)ttat/n;
  printf("\n Average Waiting Time %4.2f",awt);
  printf("\n Average Turnaround Time %4.2f",atat);
  getch();
}

Program 5b):
AIM: Write a C program to implement the various process scheduling mechanisms such as SJF Scheduling .

#include<stdio.h>
void main()
{
 int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
 int t[10],p[10];
 float awt,atat;
 clrscr();

 printf("Enter number of process\n");
 scanf("%d",&n);

 for(i=0;i<n;i++)
 {
    printf("\n Enter the Burst Time of Process %d",i);
    scanf("\n %d",&t[i]);
 }

 for(i=0;i<n;i++)
   p[i]=i;
   for(i=0;i<n;i++)
   {
     for(k=i+1;k<n;k++)
     {
       if(t[i]>t[k])
       {
            int temp;
            temp=t[i];
            t[i]=t[k];
            t[k]=temp;

            temp=p[i];
            p[i]=p[k];
            p[k]=temp;
       }
     }
     printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");
     printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n\n");
     wt[0]=0;
     for(i=0;i<n;i++)
     {
       sum=0;
       for(k=0;k<i;k++)
       {
          wt[i]=sum+t[k];
          sum=wt[i];
       }
     }
     for(i=0;i<n;i++)
     {
       tt[i]=t[i]+wt[i];
     }
     for(i=0;i<n;i++)
     {
       printf("%5d \t\t5%d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);
     }
     twt=0;
     ttat=t[0];
     for(i=1;i<n;i++)
     {
          twt=twt+wt[i];
          ttat=ttat+tt[i];
     }
     awt=(float)twt/n;
     atat=(float)ttat/n;

     printf("\n AVERAGE WAITING TIME %4.2f",awt);
     printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
     getch();
   }
}


Program 5c):
AIM: Write a C program to implement the various process scheduling mechanisms such as Round Robin Scheduling.

 

#include<stdio.h>
#include<conio.h>

void main()
{
 int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
 int bt[10],flag[10],ttat=0,twt=0;
 float awt,atat;
 printf("\t\t ROUND ROBIN SCHEDULING \n");
 printf("Enter the number of Processors \n");
 scanf("%d",&n);
 n1=n;
 printf("\n Enter the Timeslice \n");
 scanf("%d",&ts);
 for(i=1;i<=n;i++)
 {
   printf("\n Enter the process ID %d",i);
   scanf("%d",&pid[i]);
   printf("\n Enter the Burst Time for the process");
   scanf("%d",&bt[i]);
   need[i]=bt[i];
 }
 for(i=1;i<=n;i++)
 {
  flag[i]=1;
  wt[i]=0;
 }
 while(n!=0)
 {
   for(i=1;i<=n;i++)
   {
     if(need[i]>=ts)
     {
       for(j=1;j<=n;j++)
       {
            if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
            wt[j]+=ts;
       }
       need[i]-=ts;
       if(need[i]==0)
       {
           flag[i]=0;
           n--;
       }
     }
     else
     {
       for(j=1;j<=n;j++)
       {
            if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
            wt[j]+=need[i];
       }
       need[i]=0;
       n--;
       flag[i]=0;
   }
 }
}
for(i=1;i<=n1;i++)
{
  tat[i]=wt[i]+bt[i];
  twt=twt+wt[i];
  ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;

printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n\n Process \t Process ID  \t BurstTime \t Waiting Time \t TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
  printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}

printf("\n The average Waiting Time=4.2f",awt);
printf("\n The average Turn around Time=4.2f",atat);
getch();
}        
program 5d):
AIM: Write a C program to implement the various process scheduling mechanisms such  as Priority Scheduling.

#include <stdio.h>
#include <conio.h>
void main()
{
          int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
          float awt,atat;
          clrscr();
          printf("\n-----------PRIORITY SCHEDULING--------------\n");
          printf("Enter the No of Process: ");
          scanf("%d", &n);
          for (i=0;i<n;i++)
                   {
                             pid[i] = i;
                             printf("Enter the Burst time of Pid %d : ",i);
                             scanf("%d",&bt[i]);
                             printf("Enter the Priority   of Pid %d : ",i);
                             scanf ("%d",&pr[i]);
                   }
// Sorting start
                   for (i=0;i<n;i++)
                             for(j=i+1;j<n;j++)
                             {
                                      if (pr[i] > pr[j] )
                                      {
                                                t = pr[i];
                                                pr[i] = pr[j];
                                                pr[j] = t;

                                                t = bt[i];
                                                bt[i] = bt[j];
                                                bt[j] = t;

                                                t = pid[i];
                                                pid[i] = pid[j];
                                                pid[j] = t;
                                      }
                             }



//Sorting finished
          tat[0] = bt[0];
          wt[0] = 0;

          for (i=1;i<n;i++)
                   {
                   wt[i] = wt[i-1] + bt[i-1];
                   tat[i] = wt[i] + bt[i];
                   }

 
          printf("\n---------------------------------------------------------------\n");
          printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");
          printf("\n--------------------------------------------------------------\n");
                   for(i=0;i<n;i++)
                   {
                             printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
                   }
          for(i=0;i<n;i++)
                   {
                   ttat = ttat+tat[i];
                   twt = twt + wt[i];
                   }
          awt = (float)twt / n;
          atat = (float)ttat / n;
          printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time: %f\n",awt,atat);
          getch();
}