VTU - SS and OS programs 2012.-unix & OS

UNIX PROGRAMMING ::

                                            7A.sh

if test $# -eq 0
then
echo "no  arguments"
else
echo "input string is $*"
for x in "$@"
do
y=$x" "$y
done
echo "reversed  string $y"
fi                                                        
                                                                

                                           7B .c


#include<stdio.h>
int main()
{
char str[10];
int pid;
pid=fork();
 if(!pid)
 {
  printf("child process\n");
  printf("enter a command\n");
  scanf("%s",str);
  system(str);
  printf("finished with child\n");
 }
else
  {
  wait();
  }
}
----------------------------------------------------------------

                                           8A.sh

f1=`ls -l $1|cut -c2-10`
f2=`ls -l $2|cut -c2-10`
if [ $f1 = $f2 ]
then
echo "files $1  and  $2 have  same permission :$f1"
else
echo "permission are different "
echo 
echo "files $1 has file permission :$f1"
echo "files $2 has file permission :$f2"
fi

                                           8B.c


#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>

int main()
{
char buf1[]="123456789abcdefgh";
char buf2[]="hijklmnopqrstuvw";
int fd;
fd=creat("t.txt",O_WRONLY|777);
write(fd,buf1,16);
lseek(fd,48,SEEK_SET);
write(fd,buf2,16);
return 0;
}
-------------------------------------------------------------------------

                                        9A.sh

for i in $*
do
echo "echo $i 1>&2"
echo "cat >$i <<'end of $i'"
cat $i
echo "end of $i"
done

                                         9B.c

#include<stdio.h>
int main()
{
char str[10];
int pid;
pid=fork();
if(!pid)
{
printf("CHILD PROCESS :: ");
printf("\n parent pid: %d",getppid());
printf("\nchild pid: %d",getpid());

}
else
{
 wait();
 printf("\n PARENT PROCESS ::");
 printf("\n parent pid: %d",getpid());
 printf("\nchild pid: %d\n",pid);

}
return 0;
}
-------------------------------------------------------------------
OPERATING SYSTEM
10.ROUND ROBIN AND SHORTEST REMAINING TIME FIRST ALGORITHM
#include<stdio.h>
struct proc
{
int id;
int arrival;
int burst;
int rem;
int wait;
int finish;
int turnaround;
float ratio;
}process[10];  //structure to hold the process information
struct proc temp;
int no;
int chkprocess(int);
int nextprocess();
 void roundrobin(int, int, int[], int[]);
 void srtf(int);

main()
{
  int n,tq,choice;       
  int bt[10],st[10],i,j,k;
for(; ;)
{
printf("Enter the choice  \n");
printf(" 1. Round Robin\n 2. SRT\n 3. Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Round Robin scheduling algorithm\n");
printf("Enter number of processes:\n");
  scanf("%d",&n);
  printf("Enter burst time for sequences:");
  for(i=0;i<n;i++)
   {
     scanf("%d",&bt[i]);
     st[i]=bt[i]; //service time
   }
   printf("Enter time quantum:");
   scanf("%d",&tq);
   roundrobin(n,tq,st,bt);
   break;
case 2: 
printf("\n \n ---SHORTEST REMAINING TIME NEXT---\n \n ");
printf("\n \n Enter the number of processes: ");
scanf("%d", &n);
srtf(n);
break;
case 3: exit(0);
}// end of switch
}// end of for
}//end of main()

void  roundrobin(int n,int tq,int st[],int bt[])
 {        int time=0;
  int tat[10],wt[10],i,count=0,swt=0,stat=0,temp1,sq=0,j,k;
  float awt=0.0,atat=0.0;
 while(1)
   {
     for(i=0,count=0;i<n;i++)
     {
       temp1=tq;
     if(st[i]==0) //when service time of a process equals zero then count value is  incremented          
       {                                      
count++;
continue;
       }
       if(st[i]>tq)      // when service time of a process greater than time quantum then time
st[i]=st[i]-tq;     //quantum value subtracted from service time
       else
if(st[i]>=0)              
{
 temp1=st[i];      // temp1 stores the service time of a process
 st[i]=0;               // making service time equals 0
}
sq=sq+temp1;      // utilizing temp1 value to calculate turnaround time
tat[i]=sq;               // turn around time
    }  //end of for
     if(n==count)      // it indicates all processes  have completed their task because the count value 
     break;                       // incremented when service time equals 0
   } //end of while
for(i=0;i<n;i++)       // to calculate the wait time and turnaround time of each process
   {       
    wt[i]=tat[i]-bt[i];     // waiting time calculated from the turnaround time - burst time
    swt=swt+wt[i];       // summation of  wait time 
    stat=stat+tat[i];      // summation of turnaround time
   }
   awt=(float)swt/n;          // average wait time
   atat=(float)stat/n;         // average turnaround time
   printf("Process_no Burst time Wait time Turn around time\n");
   for(i=0;i<n;i++)
    printf("%d\t\t%d\t\t%d\t\t%d\n",i+1,bt[i],wt[i],tat[i]);
    printf("Avg wait time is %f\n Avg turn around time is %f\n",awt,atat);
 }// end of Round Robin

int chkprocess(int s)         // function to check process remaining time is zero or not
{
int i;
for(i = 1; i <= s; i++)
{
if(process[i].rem != 0)
return 1;
}
return 0;
} // end of chkprocess    
           
int nextprocess()        // function to identify the next process to be executed
{
int min, l, i;
min = 32000;   //any limit assumed
for(i = 1; i <= no; i++)
{
if( process[i].rem!=0 && process[i].rem < min)
{
min = process[i].rem;
l = i;
}
}
return l;
}  // end of nextprocess

void srtf(int n)
{
int i,j,k,time=0;
float tavg=0,wavg=0;
for(i = 1; i <= n; i++)
{
process[i].id = i;
printf("\n\nEnter the arrival time for process %d: ", i);
scanf("%d", &(process[i].arrival));
printf("Enter the burst time for process %d: ", i);
scanf("%d", &(process[i].burst));
process[i].rem = process[i].burst;
}
for(i = 1; i <= n; i++)
{
for(j = i + 1; j <= n; j++)
{
if(process[i].arrival > process[j].arrival)   //  sort  arrival time of a process
{
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}

no = 0;
j = 1;
       while(chkprocess(n) == 1)
{
if(process[no + 1].arrival == time)
{
no++;
if(process[j].rem==0)          
   process[j].finish=time;
j = nextprocess(); 
}
if(process[j].rem != 0)             // to calculate the waiting time of  a process
{
process[j].rem--;
for(i = 1; i <= no; i++)
{
if(i != j && process[i].rem != 0)
process[i].wait++;             
}
}
              else
{
process[j].finish = time;
j=nextprocess();
time--;
k=j;
}

time++;
}
process[k].finish = time;
printf("\n\n\t\t\t---SHORTEST REMAINING TIME FIRST---");
printf("\n\n Process  Arrival  Burst   Waiting  Finishing turnaround  Tr/Tb \n");
printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", "time", "time");
     for(i = 1; i <= n; i++)
      {
                  process[i].turnaround = process[i].wait + process[i].burst; // calc of turnaround 
         process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;        
  printf("%5d %8d %7d  %8d %10d %9d %10.1f ", process[i].id, process[i].arrival        process[i].burst, process[i].wait, process[i].finish,  process[i].turnaround,  process[i].ratio);
       tavg=tavg+ process[i].turnaround;    //summation of turnaround time
       wavg=wavg+process[i].wait;             // summation of waiting time
                    printf("\n\n");
}
tavg=tavg/n;        // average turnaround time
wavg=wavg/n;     // average wait time
            printf("tavg=%f\t wavg=%f\n",tavg,wavg);
   }// end of srtf

------------------------------------------------------------------------------------------------------------

11.Fibonacci series !!!

#include<stdio.h>
#include<omp.h>
#include<stdlib.h>

int main(int argc,char* argv[])
{
int FibNumber[25]={0};
int j, temp, tmp, id, i=0;
int n, tid, threads;
printf("Enter the number range\n");
scanf("%d",&n);
omp_set_num_threads(2);

#pragma omp parallel
{
printf("The number of threads are %d\n",omp_get_num_threads());
#pragma omp for private(tid,tmp,FibNumber)
for(j=1;j<=n;j++)
{
                 tmp = rand()%24;
printf("Thread id %d for Fib series generation  \n",omp_get_thread_num());
                #pragma omp critical
   {
for(i=1;i<=tmp;i++)
{
FibNumber[1] = 1;
FibNumber[2] = 1;
for(temp=3;temp<=tmp;temp++)    
FibNumber[temp] = FibNumber[temp-1]+FibNumber[temp-2];
}         
printf("Thread id %d for Fib series Printing \n",omp_get_thread_num());
printf("The number value is %d : ",tmp);
for(i=1;i<=tmp;i++)
printf("%d \t",FibNumber[i]);
printf("\n\n");
}

}

}
}
---------------------------------------------------------------------------------
12.Banker's algorithm :: 

#include<stdio.h>
#include<stdlib.h>
main()
{
int np,i,j,k,z,nr,p[10]={0},c[10];
int max[10][10],alloc[10][10],sum[10]={0},avail[10]={0};
printf("Enter the number of processes : ");
scanf("%d",&np);
printf("Enter the number of resources ");
scanf("%d",&nr);
printf("Enter the available resource \n");
for(i=0;i<nr;i++)
{
printf("Resources %d :",i+1);
scanf("%d",&c[i]);
}
printf("Enter the maximum resource\n");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the allocated resource \n");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
printf("avail vector < ");
for(j=0;j<nr;j++)
{
for(i=0;i<np;i++)
{
sum[j]=sum[j]+alloc[i][j];
}
avail[j]=c[j]-sum[j];
printf("%d ",avail[j]);
}
printf(" >\n");
for(z=0;z<np;z++)
{
for(i=0;i<np;i++)
{
if(p[i]==0)
{
for(j=0;j<nr;j++)
{
if((max[i][j]-alloc[i][j])>avail[j])
{
break;
}
}
if(j==nr)
{
p[i]=1;
printf("process %d executed and terminated \n ",i+1);
printf("avail vector < ");
for(k=0;k<nr;k++)
{
avail[k]=avail[k]+alloc[i][k];
alloc[i][k]=0;
printf("%d ",avail[k]);
}
printf(" >\n");
}
}
}

for(i=0;i<np;i++)
{
if(p[i]!=1)
{
printf("unsafe state \n");
exit(0);
}
}
printf("safe state \n");
}
----------------------------------------------------------------------------------------
12.Banker's algorithm :: 


#include<stdio.h>
struct process
{
int all[6],max[6],need[6],finished,request[6];
}p[10];
int avail[6],sseq[10],ss=0,check1=0,check2=0,n,pid,work[6];
int nor,nori;
void main()
{
int safeseq(void);
int ch,i=0,j=0,k,pid,ch1;
int violationcheck=0,waitcheck=0;
do
{
printf("\n\n\t 1. Input");
printf("\n\n\t 2. New Request");
printf("\n\n\t 3. Safe State or Not");
printf("\n\n\t 4. print");
printf("\n\n\t 5. Exit");
printf("\n\n\t Enter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n\t Enter number of processes : ");
scanf("%d",&n);
printf("\n\n\t Enter the Number of Resources : ");
scanf("%d",&nor);
printf("\n\n\t Enter the Available Resouces : ");
for(j=0;j<n;j++)
{
for(k=0;k<nor;k++)
{
if(j==0)
{
printf("\n\n\t For Resource type %d : ",k);
scanf("%d",&avail[k]);
}
p[j].max[k]=0;
p[j].all[k]=0;
p[j].need[k]=0;
p[j].finished=0;
p[j].request[k]=0;
}
}
for(i=0;i<n;i++)
{
printf("\n\n\t Enter Max and Allocated resources for P : ",i);
 for(j=0;j<nor;j++)
{
printf("\n\n\t Enter the Max of resource %d : ",j);
scanf("%d",&p[i].max[j]);
printf("\n\n\t Allocation of resource %d    :",j);
scanf("%d",&p[i].all[j]);
if(p[i].all[j]>p[i].max[j])
{
printf("\n\n\t Allocation should be less < or == max");
j--;
}
else
p[i].need[j]=p[i].max[j]-p[i].all[j];
avail[j]=avail[j]-p[i].all[j];
}
}
break;
case 2:
violationcheck=0;
waitcheck=0;
printf("\n\n\t Requesting process id :");
scanf("%d",&pid);
for(j=0;j<nor;j++)
{
printf("\n\n\t Number of Request for resource %d :",j);
  scanf("%d",&p[pid].request[j]);
if(p[pid].request[j]>p[pid].need[j])
violationcheck=1;
if(p[pid].request[j]>avail[j])
waitcheck=1;
}
if (violationcheck==1)
printf("\n\n\t The Process Exceeds it’s Max Need: Terminated");
else if(waitcheck==1)
printf("\n\n\t Lack of Resourcess : Process State – Wait");
else
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]-p[pid].request[j];
p[pid].all[j]=p[pid].all[j]+p[pid].request[j];
p[pid].need[j]=p[pid].need[j]-p[pid].request[j];
}
ch1=safeseq();
if(ch1==0)
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]+p[pid].request[j];
p[pid].all[j]=p[pid].all[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]+p[pid].request[j];
}
}
else if(ch1==1)
printf("\n\n\t Request Committed ");
}
break;
case 3:
if(safeseq()==1)
printf("\n\n\t The System is in safe state ");
else
printf("\n\n\t The System is Not in safe state ");
break;
case 4:
printf("\n\n\t Number of processes : %d",n);
printf("\n\n\t Number of Resources : %d",nor);
printf("\n\n\t Pid \t   Max \t   Allocated \t Need ");
for(i=0;i<n;i++)
{
printf("\n\n\t  P%d : ",i);
for(j=0;j<nor;j++)
printf(" %d",p[i].max[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%d",p[i].all[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%d",p[i].need[j]);
}
printf("\n\n\t Available :");
for(i=0;i<nor;i++)
printf("%d",avail[i]);
break;
case 5:
break;
}
}while(ch!=5);
}
int safeseq()
{
int tj,tk,i,j,k;
ss=0;
for(j=0;j<nor;j++)
work[j]=avail[j];
for(j=0;j<n;j++)
p[j].finished=0;
for( tk=0;tk<nor;tk++)
{
for(j=0;j<n;j++)
{
if(p[j].finished==0)
{
check1=0;
for(k=0;k<nor;k++)
if(p[j].need[k]<=work[k])
check1++;
if(check1==nor)
{
for(k=0;k<nor;k++)
{
work[k]=work[k]+p[j].all[k];
p[j].finished=1;
}
sseq[ss]=j;
ss++;
}
}
}
}
check2=0;
for(i=0;i<n;i++)
if(p[i].finished==1)
check2++;
printf("\n\n\t");
if(check2>=n)
{
printf("\n\n\t The system is in safe state");
for( tj=0;tj<n;tj++)
printf("P%d,",sseq[tj]);
return 1;
}
else
printf("\n\n\t The system is Not in safe state");
return 0;
}


                                                              

9 comments:

  1. The first Banker program, the smaller one, is it a 'valid' one according to VTU syllabus?
    Can the smaller Banker program be done instead of the huge one? Please reply ASAP.
    Thank you! :)

    ReplyDelete
    Replies
    1. if it works fine then, yes it is accepted.

      Delete
  2. It works correctly .. If external asks explain him the Banker algorithm
    logic about how it works :-)

    ReplyDelete
  3. is the small program is valid or what???

    ReplyDelete
  4. What s the format of getting the o/p in 8b.c program guys?

    ReplyDelete
  5. i m getting segmentation fault for srtf please help

    ReplyDelete
  6. im not geting output for Fibonacci series . error is
    ""undefined reference to `omp_set_num_threads'""

    ReplyDelete
  7. can we study 1st banker algorithm for exam? there is no problem right?

    ReplyDelete
  8. Sanyukta u cannot study 1st algorithm, Ask durga devi mam she can help u for externals dont follow random websites or ask TWINS

    ReplyDelete