Showing posts with label Operating Systems. Show all posts
Showing posts with label Operating Systems. Show all posts

Roundrobin - Scheduling - Preemptive- Systems Lab - C Program

Program:

#include<stdio.h>
#define true 1
#define false 0
int n,tq,totwt=0,tottrnd=0;
struct pr
{
    int srvst,wt;
//    int wt;
    int trndt;
    int flag;
    int temp;
}prc[10];

void printpr()
{
    printf("\nProcess_id\tServicetime\tWaitingtime\tTurnarndtime\n");int i;
    for(i=0;i<n;i++)
    {
        printf("\n%d \t\t%d \t\t%d \t\t%d\t\n",i,prc[i].srvst,prc[i].wt,prc[i].trndt);
    }
    printf("Average waiting time=%f\nAverage turnaroundtime=%f\n\n",(float)totwt/n,(float)tottrnd/n);
}

void rschedule()
{
    int trnd=0,i=0,t1;
    while(completed()==false)
    {
        if(prc[i].flag==false)
        {
            if(prc[i].temp==0||prc[i].temp<=tq)
            {  
                prc[i].flag=true;
                trnd+=prc[i].temp;
                tottrnd+=prc[i].trndt=trnd;
                prc[i].temp=0;
            }
            else
            {  
                trnd+=tq;
                prc[i].temp-=tq;
            }
        }
        i=(i+1)%n;
    }
}

int completed()
{
    int sum=0,i;
    for(i=0;i<n;i++)
    {
        if(prc[i].flag==true)
            sum++;
    }
    if(sum==n)
        return true;
        return false;
}      

main()
{
    int i;
    printf("\n\t\t<<<ROUND ROBIN SCHEDULING>>>\nEnter the timequantum: ");
    scanf("%d",&tq);  
    printf("Enter the number of processes:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        //printf("\nEnter the details for process %d:\nService time:",i);
        printf("\n Enter process %d Service time:",i);
        scanf("%d",&prc[i].srvst);
        prc[i].flag=false;
        prc[i].temp=prc[i].srvst;
    }
    prc[0].wt=0;int wtprmtr=0;
    for(i=0;i<n-1;i++)
    {
        if(prc[i].srvst<tq)
            wtprmtr+=prc[i].srvst;
        else
            wtprmtr+=tq;
        prc[i+1].wt=wtprmtr;
        totwt+=prc[i+1].wt;
    }
    rschedule();
    printpr();
}


Output:

nn@ubuntu:~$ gcc rrn.c
nn@ubuntu:~$ ./a.out

        <<<ROUND ROBIN SCHEDULING>>>
Enter the timequantum: 50
Enter the number of processes:5

 Enter process 0 Service time:350

 Enter process 1 Service time:125

 Enter process 2 Service time:475

 Enter process 3 Service time:250

 Enter process 4 Service time:75

Process_id    Servicetime    Waitingtime    Turnarndtime

0                   350                  0                     1100   

1                   125                  50                   550   

2                   475                  100                 1275   

3                   250                  150                 950   

4                     75                  200                 475   
Average waiting time=100.000000
Average turnaroundtime=870.000000
                                                                         
nn@ubuntu:~$



Inter Process Communication using Pipe- Fibonacci - Systems Lab - C

Program:
#include<stdio.h>
main()
{
    int pid;
    int p1[2],p2[2];
    pipe(p1);
    pipe(p2);
    int b,n,i,f1,f2;int ar[30],br[30];
    pid=fork();
    if(pid==0)
    {
        printf("enter count:");
        fflush(stdin);
        scanf("%d",&n);
        close(p1[0]);
        write(p1[1],&n,4);
       
        close(p2[1]);
        read(p2[0],br,30*sizeof(int));
        printf("\nFibonacci:\n");
        for(i=0;i<n;i++)
            printf("%d\n",br[i]);
           
    }
    else if(pid>0)
    {
    close(p1[1]);
    read(p1[0],&b,4);
    //printf("count is:%d",b);
   
    f1=0,f2=1;
    ar[0]=0;
    ar[1]=1;
    int i;
    for(i=2;i<b;i++)
    {
        int f3=f1+f2;
        f1=f2;
        f2=f3;
        ar[i]=f3;
    }
    close(p2[0]);
    write(p2[1],ar,30*sizeof(int));
    }
}


Output:
nn@ubuntu:~$ gcc pp.c
nn@ubuntu:~$ ./a.out
enter count:5
nn@ubuntu:~$
Fibonacci:
0
1
1
2
3

Inter Process Communication using Pipe - Sorting - Systems Lab

Program:
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
          
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }
   
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
           
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }


Output:
 nn@ubuntu:~$ gcc pb1.c
nn@ubuntu:~$ ./a.out
enter the limit
5
enter the numnbers5
4
3
2
1
5    4    3    2    1
nn@ubuntu:~$ gcc pb1.c

Shared Memory - String - Systems Lab - C Program


Program:

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#include"string.h"
#define shmsize 100
#define shmmode (SHM_R|SHM_W|IPC_CREAT|IPC_EXCL)
#define shmkey (key_t)31
int size;
int main()
{
    int shmid1, shmid2, pid, status;
   
    int *shmdata1, *shmdata2, *shmdata;
   
    char str1[20],str2[20],str3[20];
    int i,j,k,size2,size3;
    struct shmid_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("Enter the string:");
    scanf("%s",str1);
    size=strlen(str1);
    for(j=0;j<size;j++)
    {
        *shmdata1=str1[j];
        shmdata1=shmdata1+sizeof(char);
    }
    pid = fork();           
    if(pid == 0)
    {
        printf("Enter the string:");
        scanf("%s",str2);
        size2=strlen(str2);
        for(i=0;i<size2;i++)
        {
            *shmdata1=str2[i];
            shmdata1+=sizeof(char);
        }
        size=size+size2;
    }
    while((pid = wait(&status))!= -1);
    shmdata1=shmdata;
    for(i=0;i<size;i++)
    {
        str3[i]=*shmdata1;
        shmdata1+=sizeof(char);
    }
    str3[size]='\0';
    printf("%s\n\n\n\n\n\n",str3);
shmdt((void*)shmdata);
shmdt((void*)shmdata1);
shmctl(shmid1,IPC_RMID,shmidds);
    return 1;
}

Output:
nn@ubuntu:~$ gcc shmstr.c
nn@ubuntu:~$ ./a.out
Enter the string:hai
Enter the string:helo
haihelo





hai





nn@ubuntu:~$

Scheduling - Non-Preemptive - Systems Lab - C Program

///Scheduling Non-Preemptive -OS Lab

#include<stdio.h>
int i,j,n;float totwt,totserv;
struct pr
    {
        char name[5];
        float tservice;
        int priority;
        float twait;
        float trnd;
    };
    struct pr temp;
    struct pr pr1[20];
void init()
{
    printf("\nEnter the number of processes:");
    scanf("%d",&n);
  
    for(i=0;i<n;i++)
    {
        printf("\nEnter the details of process %d:\n1.Name: ",i+1);
        scanf("%s",pr1[i].name);
        printf("2.Service time: ");
        scanf("%f",&pr1[i].tservice);
        printf("3.Priority: ");
        scanf("%d",&pr1[i].priority);
    }
}
void prioritysort()
{
    for(i=0;i<n;i++)
    for(j=0;j<n-i-1;j++)
    {
        if(pr1[j].priority>pr1[j+1].priority)
        {
            temp=pr1[j];
            pr1[j]=pr1[j+1];
            pr1[j+1]=temp;
        }
    }
}
void sjnsort()
{
for(i=0;i<n;i++)
    for(j=0;j<n-i-1;j++)
    {
        if(pr1[j].tservice>pr1[j+1].tservice)
        {
            temp=pr1[j];
            pr1[j]=pr1[j+1];
            pr1[j+1]=temp;
        }
    }
}
void calculation()
{
pr1[0].twait=0;totwt=0;totserv=pr1[0].trnd=pr1[0].tservice;
    for(i=1;i<n;i++)
    {
        //printf("\nwait= %f",totwt);
        pr1[i].twait=pr1[i-1].twait+pr1[i-1].tservice;
        totwt+=pr1[i].twait;
        pr1[i].trnd=pr1[i].twait+pr1[i].tservice;
        totserv+=pr1[i].trnd;

    }
}
void printing()
{
    printf("Name \tPriority \tServiceTime \t WaitTime \t TurnAroundTime\n");
    for(i=0;i<n;i++)
        {
        printf("%s \t %d \t\t %f \t %f \t %f\n",pr1[i].name,pr1[i].priority,pr1[i].tservice,pr1[i].twait,pr1[i].trnd);
        }
        printf("\nAverage wait:%f \tAverage turnaround:%f \n",totwt/n,totserv/n);
}

main()
{
    int ch,exit=0;
    init();
  
    while(exit==0)
    {
    printf("\n---MENU---\n1.FIRST COME FIRST SERVED (FCFS)\n\t2.PRIORITY SCHEDULING\n\t\t3.SHORTEST JOB NEXT (SJN)\n\t\t\t5.Exit\nEnter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:printf("\t<<<FCFS SCHEDULING>>>\n");
            break;
        case 2:printf("\t<<<PRIORITY SCHEDULING>>>\n");
            prioritysort();
            break;
        case 3:printf("\t<<<SJN SCHEDULING>>>\n");
            sjnsort();
            break;
        case 5:    printf("\nExiting...\n");
        default: exit=1;
          
            break;
    }
    if(exit==0)
    {
        calculation();
        printing();  
    }

    }

}


Matrix Multiplication using Shared Memory- Systems Lab - C Program



//Matrix Multiplication using Shared Memory-OS Lab

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#define shmsize 100
#define shmmode (SHM_R|SHM_W)
#define shmkey (key_t)31

int main() {
    int shmid1, shmid2, pid, status;
    int *shmdata1, *shmdata2, *shmdata;
    int mtx1[10][10], mtx2[10][10];
    int i,j,k,r1,r2,c1,c2;
    struct shmd_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode|IPC_CREAT|IPC_EXCL);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("\nShmID: %d ShmData: %d \n",shmid1,*shmdata1);
    printf("Enter the rows and columns of matrix 1:");
    scanf("%d%d",&r1,&c1);
    printf("Enter the matrix 1: \n");
    for(i=0;i<r1;++i)
        for(j=0;j<c1;++j)
            scanf("%d",&mtx1[i][j]);
    printf("\nEnter the rows and columns of matrix 2:");
    scanf("%d%d",&r2,&c2);
    printf("Enter the matrix 2: \n");   
    for(i=0;i<r2;++i)
        for(j=0;j<c2;++j)
            scanf("%d",&mtx2[i][j]);   
    printf("\n Hello,note this...");
    if(r2!=c1) {
        printf("\nCannot Multiply");
        return 0;
    }

    for(i=0;i<r1/2;i++)
        for(j=0;j<c1;j++) {
        *shmdata1 = 0;
        for(k=0;k<c1;k++)
            *shmdata1 += mtx1[i][k]*mtx2[k][j];
            shmdata1 += sizeof(int);
        }
    pid = fork();
    if(pid == 0) {
        for(i=r1/2;i<r1;i++)
            for(j=0;j<c2;j++) {
                *shmdata1 = 0;
                for(k=0;k<c1;k++)
                    *shmdata1 += mtx1[i][k]*mtx2[k][j];
                    shmdata1 += sizeof(int);
            }       
    }
    while((pid = wait(&status))!= -1);
    shmdata1 = shmdata;
    printf("\n\n\nResult from %d\n", getpid());
    for(i=0;i<r1;++i) {
        printf("\n    ");
        for(j=0;j<c2;j++,shmdata1+=sizeof(int))
            printf("%d ",*shmdata1);
    }
    shmdt((void*)shmdata1);
    shmdt((void*)shmdata2);
    shmctl(shmid1,IPC_RMID,shmidds);
    //shmctl(shmid2,IPC_RMID,shmidds);
    return 1;
}


Readers Writers Problem - Semaphore - Systems Lab - C Program


//Readers Writers Problem -OS Lab

Program:
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>

sem_t mutex,writeblock;
int data = 0,rcount = 0;

void *reader(void *arg)
{
  int f;
  f = ((int)arg);
  sem_wait(&mutex);
  rcount = rcount + 1;
  if(rcount==1)
   sem_wait(&writeblock);
  sem_post(&mutex);
  printf("Data read by the reader%d is %d\n",f,data);
  sleep(1);
  sem_wait(&mutex);
  rcount = rcount - 1;
  if(rcount==0)
   sem_post(&writeblock);
  sem_post(&mutex);
}

void *writer(void *arg)
{
  int f;
  f = ((int) arg);
  sem_wait(&writeblock);
  data++;
  printf("Data writen by the writer%d is %d\n",f,data);
  sleep(1);
  sem_post(&writeblock);
}

main()
{
  int i,b; 
  pthread_t rtid[5],wtid[5];
  sem_init(&mutex,0,1);
  sem_init(&writeblock,0,1);
  for(i=0;i<=2;i++)
  {
    pthread_create(&wtid[i],NULL,writer,(void *)i);
    pthread_create(&rtid[i],NULL,reader,(void *)i);
  }
  for(i=0;i<=2;i++)
  {
    pthread_join(wtid[i],NULL);
    pthread_join(rtid[i],NULL);
  }
}



Producer-Consumer Problem - Semaphore - Systems Lab - C Program



//Producer Consumer Problem -OS Lab

Program:

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
int buf[5],f,r;
sem_t mutex,full,empty;
void *produce(void *arg)
{
    int i;
    for(i=0;i<10;i++)
    {
        sem_wait(&empty);
        sem_wait(&mutex);
        printf("produced item is %d\n",i);
        buf[(++r)%5]=i;
        sleep(1);
        sem_post(&mutex);
                sem_post(&full);
        printf("full %u\n",full);
    }
}
void *consume(void *arg)
{
        int item,i;
        for(i=0;i<10;i++)
        {
                sem_wait(&full);
        printf("full %u\n",full);
                sem_wait(&mutex);
                item=buf[(++f)%5];
                printf("consumed item is %d\n",item);
                sleep(1);
                sem_post(&mutex);
                sem_post(&empty);
        }
}
main()
{
    pthread_t tid1,tid2;
    sem_init(&mutex,0,1);
    sem_init(&full,0,1);
    sem_init(&empty,0,5);
    pthread_create(&tid1,NULL,produce,NULL);
        pthread_create(&tid2,NULL,consume,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
}



Dining Philosophers Problem - Systems Lab - C Program


//Dining Philosophers

Program:

#include<stdio.h>
#include<semaphore.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<pthread.h>
struct{
        int data[5];
        sem_t *mutex[5];
    }sh;
char *name[]={"p1","p2","p3","p4","p5"};
void *work(void *);
main()
{
        pthread_t tid[5];
        int i;
        for(i=0;i<5;i++)
        {
            sh.mutex[i]=sem_open(name[i],O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,1);//
        }
        pthread_setconcurrency(6);
        setbuf(stdout,NULL);
        for(i=0;i<5;i++)
            pthread_create(&tid[i],NULL,work,&i);
        for(i=0;i<5;i++)
            pthread_join(tid[i],NULL);
        for(i=0;i<5;i++)
            sem_unlink(name[i]);
}
void *work(void *arg)
{
    int p,i=1;
    p=*((int *)arg);
    while(i++<2)
    {
        sem_wait(sh.mutex[p]);
        sem_wait(sh.mutex[(p+1)%5]);
        sh.data[p]=1;
        printf("p%d[%d %d %d %d %d]\n",p+1,sh.data[0],sh.data[1],sh.data[2],sh.data[3],sh.data[4]);
        sleep(1);//eating
        sh.data[p]=0;
        sem_post(sh.mutex[p]);
        sem_post(sh.mutex[(p+1)%5]);
        sleep(2);//thinking
    }
}








Bankers Algorithm- Systems Lab - C Program


//Bankers Algorithm

Program :

#include<stdio.h>
#define true 1
#define false 0
int m,n,i,j,count=0,process;
int max[10][10],alloc[10][10],need[10][10],c[10],avail[10],finish[10];
void readtable(int t[10][10])
{
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&t[i][j]);   
}
void printtable(int t[10][10])
{
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
            printf("\t%d",t[i][j]);   
        printf("\n");
    }
}
void readvector(int v[10])
{
    for(j=0;j<n;j++)
        scanf("%d",&v[j]);   
}
void printvector(int v[10])
{
    for(j=0;j<n;j++)
        printf("\t%d",v[j]);   
}
void init()
{
    printf("enter the number of process\n");
    scanf("%d",&m);
    printf("enter the number of resources\n");
    scanf("%d",&n);
    printf("enter the claim table\n");
    readtable(max);
    printf("enter the allocation table\n");
    readtable(alloc);
    printf("enter the max units of each resource\n");
    readvector(c);
    for(i=0;i<n;i++)
        finish[i]=false;
}

void findavail()
{
    int sum;
    for(j=0;j<n;j++)
    {
        sum=0;
        for(i=0;i<m;i++)   
        {
            sum=sum+alloc[i][j];
        }
        avail[j]=c[j]-sum;
    }
}
void findneed()
{
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)   
        {
            need[i][j]=max[i][j]-alloc[i][j];
        }
       
    }
}
void selectprocess()
{
    int flag;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)   
        {
            if(need[i][j]<=avail[j])
                flag=1;
            else
            {
                flag=0;       
                break;
            }
        }
        if((flag==1)&&(finish[i]==false))
        {
            process=i;
            count++;
            break;
        }
    }
    printf("current status is\n");
    printtable(alloc);
    if(flag==0)
    {
        printf("system is in unsafe state\n");
        exit(1);
    }
    printf("system is in safe state");
}
void executeprocess(int p)
{
    printf("excuting process is %d",p);
    printtable(alloc);
}
void releaseresource()
{
    for(j=0;j<n;j++)
        avail[j]=avail[j]+alloc[process][j];
    for(j=0;j<n;j++)
    {
        alloc[process][j]=0;
        need[process][j]=0;
    }
}

main()
{
    init();
    findavail();
    findneed();
    do
    {
        selectprocess();
        finish[process]=true;
        executeprocess(process);
        releaseresource();
    }while(count<m);
    printf("\n all proces executed correctly");
}





Operating Systems - Gary Nutt - Presentation Slides


Operating Systems
Gary Nutt

Visit SPELMAN COLLEGE website for the pdf files.







Visit GORDON COLLEGE WEBSITE website for the ppt files.



Download page for ppt.



Chap 01-Intoduction


Chap02-Using the Operating System


Chap 03-Operating System Organisatiom


Chap 04-Computer Organisation


Chap05-Device Management


Chap06-Implementing Processes Threads Resources


Chap07-Scheduling


Chap08-Basic Synchronisation Principles


Chap09-High Level Synchronisation IPC


Chap10-Deadlock


Chap11-Memory Management


Chap11-Memory Management


Chap12-Virtual Memory


Chap13-File Management


Chap14-Protection Security


Chap15-Networks


Chap16-Remote Files


Chap17-Distibuted Computing


Chap18-Distributed Programming Runtime Systems


Chap19-Design Strategies


Chap20-TheLinuxKernel


Chap21-WindowsKernel


workshop_1


workshop_2

Operating Systems (OS) - Previous Question Papers -Semester 5 - 2011 - 2K6CS505





Related Posts Plugin for WordPress, Blogger...