Tuesday, 29 November 2022

OS

 


---------------------------------------UNIX FILE SYSTEM-----------------------------------

    

   

#!/usr/bin/bash
while :
do
echo "*Welcome to Menu**"
echo "1. List files & directory"
echo "2. Create directory"
echo "3. Copy to others file"
echo "4. Delete a Directory"
echo "5. change a Directory"
echo "6. Create a File"
echo "7. Rename a file"
echo "8. DElete a file"
echo "9. Showing current directory"
echo "10. Display the file content"
echo -n "Enter ur choiche : " 
read ch
case $ch in 
1) ls
 ;;
2) echo "Enter the name of directory"
 read name 
 mkdir $name
 ;;
3) echo "Enetr the name of copy file"
 read cpy
 echo "Enetr the name of copy file"
 read cpy1
 cp $cpy $cpy1
 ;;
4) echo "Delete a directory"
 echo "Enter a name of directory"
 read ddir
 rm -r $ddir
 ;;
5) echo "CHange a Directory"
 echo "Enter a name of directory"
 read cdir
 cd $cdir
 ;;
6) echo "Create a file"
 echo "Enter a name of file:"
 read cfl
 touch $cfl
 ;;
7) echo "Delete a file"
 echo "Enter a name of file:"
 read dfl
 rm $dfl
 ;;
8) echo "REname a file"
 echo "Enter a name of file:"
 read rfl
 mv $rfl
 ;;
9) echo "showing current directoy a file"
 read sdr
 pwd $sfl
 ;;
10)echo "Display the file content"
 echo "Enter a name of file:"
 read dfc
 cat $dfc
 ;;
*)echo "Invalid choice"
esac
done




---------------------------------------Zero deleted file-----------------------------------



 #!/usr/bin/bash
filename="/home/waqar/student/abc.c"
if [ -e $filename ]
then
echo "File exists"
else
echo " File does not exists "
fi
if [ -s $filename ]
then
echo "File is not empty"
else
echo "File is empty"
rm $filename
echo "File Deleted"
fi



  ---------------------------------------sort----------------------------------

   
#!/usr/bin/bash
echo "Enter the file name"
read filename
cat -n $filename | sort -k2 | uniq -f1 | sort -k1 | cut -f2




---------------------------------------Fage replacement -----------------------------------




#include <stdio.h>

#include <iostream>

//#include <Algorithm>

#include <bits/stdc++.h>

using namespace std;

int findLRU(int time[], int n)

{

 int i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i)

{

 if(time[i] < minimum)

{

 minimum = time[i];

 pos = i;

}

}

return pos;

}

void fifo()

{

 cout<<"\n-------PAGE REPLACEMENT ALGORITHM [FIFO]-------

:\n\n";

 int n, a[50], frame[10], no, k, avail, count = 0;

cout<<"\n ENTER THE NUMBER OF PAGES :\n";

cin>>n;

cout<<"\n ENTER THE VALUE FOR PAGE NUMBER \n";

for (int i = 1; i <= n; i++)

{

cout<<"Page no a[" << i << "] ";

cin>>a[i];

}

cout<<"\nENTER THE NUMBER OF FRAMES\n";

cin>>no;

for (int i = 0; i < no; i++)

{

frame[i] = -1;

}

int j = 0;

cout << "\tRef Pg Number:\t Page frames:\n";

for (int i = 1; i <= n; i++)

{

cout << "\t\t" << a[i];

avail = 0;

for (k = 0; k < no; k++)

{

if (frame[k] == a[i])

avail = 1;

}

if (avail == 0)

{

frame[j] = a[i];

j = (j + 1) % no;

count++;

for (k = 0; k < no; k++)

{

cout << "\t" << frame[k];

}

}

cout << endl;

}

cout << "\nNUMBER OF PAGE FAULTS : " << count<<endl;

cout<<"----------------------------------------------------------------------

"<<endl;

}

void LRU()

{

cout<<"\n-------PAGE REPLACEMENT ALGORITHM [LRU]------- :\n\n";

int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, 

time[10], flag1, flag2, i, j, pos, faults = 0;

 cout<<"\nENTER THE NUMBER OF FRAMES: ";

 cin>>no_of_frames;

cout<<"\nENTER THE NUMBER OF PAGES: ";

cin>>no_of_pages;

cout<<"\ENTER THE REFERENCE STRING: ";

for(i = 0; i < no_of_pages; ++i)

{

cin>>pages[i];

}

for(i = 0; i < no_of_frames; ++i)

{

frames[i] = -1;

}

for(i = 0; i < no_of_pages; ++i)

 {

flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j)

{

if(frames[j] == pages[i])

 {

counter++;

time[j] = counter;

flag1 = flag2 = 1;

break;

}

 }

if(flag1 == 0)

{

for(j = 0; j < no_of_frames; ++j)

{

if(frames[j] == -1)

{

counter++;

faults++;

frames[j] = pages[i];

time[j] = counter;

flag2 = 1;

break;

 }

 } 

}

if(flag2 == 0)

{

pos = findLRU(time, no_of_frames);

counter++;

faults++;

frames[pos] = pages[i];

time[pos] = counter;

}

cout<<"\n";

for(j = 0; j < no_of_frames; ++j)

{

cout<<"\t"<<frames[j];

}

}

cout<<"\n\nTOTAL FAGE FAULTS: "<<faults<<endl;

 cout<<"----------------------------------------------------------------------

"<<endl;

}

void Optimal()

{

 cout<<"\n-------PAGE REPLACEMENT ALGORITHM [OPTIMAL]-------

:\n\n";

int l;

cout<<"\nENTER THE LENGTH OF REFERENCE STRING :";

cin>>l;

int 

arr[l],pf=0,ph=0,flag1=0,flag2=0,flag3=0,count1=0,count2=0,count3=0;

cout<<"\nENTER THE REFERENCE STRING:\n";

for(int i=0;i<l;i++)

{

cout<<"-> ";

cin>>arr[i];

 }

cout<<endl;

int a=-1,b=-1,c=-1,d=0,i=0,f1=0,f2=1,f3=1;

while(i<l)

{

d=arr[i];

if(a==-1)

{

a=d;

pf++;

cout<<a<<endl;

 }

else if(b==-1)

{

b=d;

pf++;

cout<<a<<"\t"<<b<<endl;

}

else if(c==-1)

{

c=d;

pf++;

cout<<a<<"\t"<<b<<"\t"<<c<<endl;

 } 

if(i>2)

{

if(d==a || d==b || d==c)

{

ph++;

cout<<endl;

 }

else

{

flag1=0,flag3=0,flag2=0;

count1=0,count2=0,count3=0;

 for(int k=i; k<l; k++)

 {

if(arr[k]==a && flag1==0)

{

flag1=1;

 } 

else if(arr[k]==b && flag2==0)

{

flag2=1;

}

else if(arr[k]==c && flag3==0)

{

flag3=1;

}

if(flag1==0)

{

count1++;

}

if(flag2==0)

{

count2++;

}

if(flag3==0)

{

count3++;

}

if(flag1==1 && flag2==1 && flag3==1)

{

break;

}

}

if(count1>count2 && count1>count3)

{

a=d;

pf++;

}

else if(count2>count1 && count2>count3)

{

b=d;

pf++;

}

else if(count3>count2 && count3>count1)

{

c=d;

pf++;

}

else{

if(flag1==0 ){

a=d;

}

else if(flag2==0){

b=d;

}

else{

c=d;

}

pf++;

}

cout<<a<<"\t"<<b<<"\t"<<c<<endl; 

}

}

i++;

}

cout<<"\nPAGE FAULTS:"<<pf;

cout<<"\nPAGE HITS:"<<ph<<endl;

};

int main()

{

cout << "\n\t*****----------PAGE REPLACEMENT 

ALGORITHM----------*****\n";

int ch;

//int exit=0;

while (1)

{

cout << "\n 1. FIFO \n 2. LRU \n 3. OPTIMAL \n 4. EXIT 

\n";

cout << "\n ENETR YOUR CHOICE: ";

cin >> ch;

switch (ch)

 {

case 1:

{

fifo();

break;

}

case 2:

{

LRU();

break;

}

case 3:

{

Optimal();

break;

 }

case 4:

{

exit(1);

break;

 }

 }

}

 }



 --------------------------------------✌

---------------------------------------FIFO------------------------------------------


FIFO

#include<iostream>


using namespace std;


int main()

{

    // cout<<"\nPage Replacement Algorithm (FIFO) :\n\n";

    cout<<"ENTER THE LENGTH OF REFRENCE STRING:"<<endl;

    int l;

    cin>>l;

    

    //cout<<"\nEnter the Length of the reference string :";

    //cin>>l;

    int pf=0,ph=0,flag1=0,flag2=0,flag3=0,count1=0,count2=0,count3=0;

    int arr[l];

   // cin>>arr[l];

    cout<<"\nEnter the Reference String:";

     for(int i=0;i<l;i++)

     {

         //cout<<"-> ";

         cin>>arr[i];

     }

    cout<<endl;

    int a=-1,b=-1,c=-1,d=0,i=0,f1=0,f2=1,f3=1;

    while(i<l)

    {

        d=arr[i];

        if(a==-1)

        {

            a=d;

            pf++;

            cout<<a<<endl;

        }

        else if(b==-1)

        {

            b=d;

            pf++;

            cout<<a<<"\t"<<b<<endl;

        }

        else if(c==-1)

        {

            c=d;

            pf++;

            cout<<a<<"\t"<<b<<"\t"<<c<<endl;

        }

        if(i>2)

        {

            if(d==a || d==b || d==c)

            {

                ph++;

                cout<<endl;

            }

            else

            {

                flag1=0,flag3=0,flag2=0;

                count1=0,count2=0,count3=0;

                for(int k=i; k<l; k++)

                {

                    if(arr[k]==a && flag1==0)

                    {

                        flag1=1;

                    }

                    else if(arr[k]==b && flag2==0)

                    {

                        flag2=1;

                    }

                    else if(arr[k]==c && flag3==0)

                    {

                        flag3=1;

                    }


                    if(flag1==0)

                    {

                        count1++;

                    }

                    if(flag2==0)

                    {

                        count2++;

                    }

                    if(flag3==0)

                    {

                        count3++;

                    }


                    if(flag1==1 && flag2==1 && flag3==1)

                    {

                        break;

                    }



                }

                if(count1>count2 && count1>count3)

                {

                    a=d;

                    pf++;

                }

                else if(count2>count1 && count2>count3)

                {

                    b=d;

                    pf++;

                }

                else if(count3>count2 && count3>count1)

                {

                    c=d;

                    pf++;

                }

                else{

                if(flag1==0 ){

                a=d;

}

else if(flag2==0){

b=d;

}

else{

c=d;

}

pf++;

}

               cout<<a<<"\t"<<b<<"\t"<<c<<endl; 

            }

        }


        i++;


    }

    cout<<"\nPage Faults :"<<pf;

    cout<<"\nPage Hits :"<<ph<<endl;


return 0;


}









-------------------------------------✌✌✌✌✌✌✌✌✌

---------------------------------------LRU -----------------------------------


#include<iostream>


using namespace std;


int main()

{

int n;

cout<<"\nenter the no of pages=";

cin>>n;

int arr[n];

int nq;

bool ter;

for(int i=0;i<n;i++)

{

cout<<"\nEnter page no=";

cin>>arr[i];

}

cout<<"\n enter the size of page=";

cin>>nq;

int que[nq];

int page_hit=0;

int page_fault=0;

int count=0;

int qp=0;

for(int i=0;i<n;i++)

{ter=false;

if(i<nq)

{

que[count]=arr[i];

count++;

page_fault++;

}

else

{

for(int j=0;j<nq;j++)

{

if(que[j]==arr[i])

{

page_hit++;

ter=true;

break;

}

}

if(ter==false)

{

int temp[nq]={0,0,0};

int count2=0;

while(count2<nq)

{

for(int l=i-1;l>=0;l--)

{

if(arr[l]==que[count2])

{

temp[count2]=l;

break;

}

}

count2++;

    }

int min,min_pos;

min=temp[0];

min_pos=0;

for(int k=1;k<nq;k++)

{

if(min>temp[k])

{

min=temp[k];

min_pos=k;

}

}

que[min_pos]=arr[i];

page_fault++;

}

}

cout<<"\n temp=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}

}

cout<<"\n page hit="<<page_hit;

cout<<"\n page fault="<<page_fault;

cout<<"\n quee=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}


}

// 7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1



-----------------------------------✌✌✌✌✌✌✌✌✌

--------------------------------------OPTIMAL -----------------------------------


#include<iostream>


using namespace std;


int main()

{

int n;

cout<<"\nenter the no of pages=";

cin>>n;

int arr[n];

int nq;

bool ter;

for(int i=0;i<n;i++)

{

cout<<"\nEnter page no=";

cin>>arr[i];

}

cout<<"\n enter the size of page=";

cin>>nq;

int que[nq];

int page_hit=0;

int page_fault=0;

int count=0;

int qp=0;

for(int i=0;i<n;i++)

{ter=false;

if(i<nq)

{

que[count]=arr[i];

count++;

page_fault++;

}

else

{

for(int j=0;j<nq;j++)

{

if(que[j]==arr[i])

{

page_hit++;

ter=true;

break;

}

}

if(ter==false)

{

int temp[nq]={0};

int count2=0;

while(count2<nq)

{

for(int l=i+1;l<n;l++)

{

if(arr[l]==que[count2])

{

temp[count2]=l;

break;

}

}

count2++;

    }

int max,max_pos;

max=temp[0];

max_pos=0;

for(int k=1;k<nq;k++)

{

if(temp[k]==0)

{

max=temp[k];

max_pos=k;

}

if(max<temp[k])

{

max=temp[k];

max_pos=k;

}

}

que[max_pos]=arr[i];

page_fault++;

}

}

cout<<"\n temp=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}

}

cout<<"\n page hit="<<page_hit;

cout<<"\n page fault="<<page_fault;

cout<<"\n quee=";

for(int i=0;i<nq;i++)

{

cout<<que[i]<<" ";

}


}

// 7 2 3 1 2 5 3 4 6 7 7 1 0 5 4 6 2 3 0 1



---------------------------------------Bankers✌✌✌✌✌----------------------------------



#include<iostream>

using namespace std;


int alloc[10][10],Max[10][10],need[10][10],avail[1][10];

int N,R,finish[10];


void calculateNeed(){

for(int i=0;i<N;i++){

for(int j=0;j<R;j++){

need[i][j]=0;

need[i][j]=Max[i][j]-alloc[i][j];

}

}

}


void sequence(){

int cp=0,cur=0;

cout<<"\n The safe sequence is : ";

while(cp<N){

if(finish[cur]==0){

bool flag=false;

for(int j=0;j<R;j++){

if(need[cur][j]>avail[0][j]){

flag=false;

break;

}

else

flag=true;

}

if(flag){

cout<<"\t P"<<cur;

cp++;

finish[cur]=1;

for(int i=0;i<R;i++){

avail[0][i]+=alloc[cur][i];

}

}

}

cur=(cur+1)%N;

}

}


int main(){

cout<<"\n Enter the no. of process : ";

cin>>N;

cout<<"\n Enter the no. of Resources : ";

cin>>R;

for(int i=0;i<N;i++){

cout<<"\n";

finish[i]=0;

for(int j=0;j<R;j++){

cout<<" Enter the no of instances of resource "<<j<<" alloated to P "<<i<<" : ";

cin>>alloc[i][j];

}

}


for(int i=0;i<N;i++){

cout<<"\n";

for(int j=0;j<R;j++){

cout<<" Enter the max no of instances of resource "<<j<<" required to P "<<i<<" : ";

cin>>Max[i][j];

}

}


cout<<"\n";

for(int j=0;j<R;j++){

cout<<" Enter the available instances of resource "<<j<<" : ";

cin>>avail[0][j];

}


calculateNeed();

cout<<"\n Allocaation :-";

for(int i=0;i<N;i++){

cout<<"\n";

for(int j=0;j<R;j++){

cout<<alloc[i][j]<<"\t";

}

}

cout<<"\n Max:- ";

for(int i=0;i<N;i++){

cout<<"\n";

for(int j=0;j<R;j++){

cout<<Max[i][j]<<"\t";

}

}

cout<<"\n Need :- ";

for(int i=0;i<N;i++){

cout<<"\n";

for(int j=0;j<R;j++){

cout<<need[i][j]<<"\t";

}

}

  sequence();

 

return 0;

}





--------------------------------------Barber ✌✌✌✌✌✌✌✌✌-----------------------------------


#include<stdio.h>

#include<stdlib.h>

#include<semaphore.h>

#include<pthread.h>


using namespace std;


sem_t empty;

pthread_mutex_t mutex;

int val;

void *none;


void * cutting(void *cno){

printf("\nCustomer %d is arrived.",*((int*)cno));

sem_getvalue(&empty,&val);

if(val==5){

sem_wait(&empty);

pthread_mutex_lock(&mutex);

printf("\n Barber is sleeping ...");

printf("\n Customer %d wake ups barber",*((int*)cno));

printf("\n Barber cuts hair of customer %d",*((int*)cno));

printf("\n Hair cutting of customer %d is completed",*((int*)cno));

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

else if(val>0){

sem_wait(&empty);

pthread_mutex_lock(&mutex);

printf("\n Customer %d is waiting ....",*((int*)cno));

printf("\n Barber cuts hair of customer %d",*((int*)cno));

printf("\nHair cutting of customer %d is completed",*((int*)cno));

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}

else{

printf("\n no empty chairs are there so, Customer %d is leaving.",*((int*)cno));

}

return none;

}


int main(){

int a[8]={1,2,3,4,5,6,7,8};

pthread_t customer[8];

sem_init(&empty,0,5);

for(int i=0;i<8;i++){

pthread_create(&customer[i],NULL,&cutting,(void *)&a[i]);

}

for(int i=0;i<8;i++){

pthread_join(customer[i],NULL);

}

return 0;

}




✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌==-----PIPE-------------------------



#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/wait.h>


void processA(int writefd){

char buff[80];

int n=9;

printf("you are in the process A \n Enter the string : ");

scanf("%s",&buff);

write(writefd,buff,n);

}


void processB(int readfd){

char buff[80];

int n;

n=read(readfd,buff,80);

printf("you are in the process_B \n reading from a process_A : %s %d \n",buff,n);

printf("The reversed string is : \t");

for(int i=n;i>=0;i--){

printf("%c",buff[i]);

}

}



int main(){

int fd[2];

pid_t pid;

pipe(fd);

pid=fork();

if(pid>0){

processA(fd[1]);

}

else{

processB(fd[0]);

}

return 0;

}




✌✌✌✌✌✌✌✌✌ FCFS-------------------------



#include<iostream>

 

using namespace std;

 

int main()

{

    int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

    cout<<"Enter total number of processes:";

    cin>>n;

 

    cout<<"\nEnter Process Burst Time\n";

    for(i=0;i<n;i++)

    {

        cout<<"P["<<i+1<<"]:";

        cin>>bt[i];

    }

 

    wt[0]=0;    //waiting time for first process is 0

 

    //calculating waiting time

    for(i=1;i<n;i++)

    {

        wt[i]=0;

        for(j=0;j<i;j++)

            wt[i]+=bt[j];

    }

 

    cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";

 

    //calculating turnaround time

    for(i=0;i<n;i++)

    {

        tat[i]=bt[i]+wt[i];

        avwt+=wt[i];

        avtat+=tat[i];

        cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];

    }

 

    avwt/=i;

    avtat/=i;

    cout<<"\n\nAverage Waiting Time:"<<avwt;

    cout<<"\nAverage Turnaround Time:"<<avtat;

 

    return 0;

}



------------------------------------------SJF-NON PREMP  ✌✌✌✌



#include<iostream>

using namespace std;

int main()
{
    int bt[10],pt[10],wt[10],wt1=0,ta[10],ta1;
    char arr4[100];
    int a,b,c;

    cout<<"Enter the No of Processes :";
    cin>>a;

    for(int i=0;i<a;i++)
    {
        cout<<"\nEnter the Name of the Process :";
        cin>>arr4[i];
        cout<<"Enter the Burst Time :";
        cin>>bt[i];
    }

    cout<<"\nName\tB.T\n";
    for(int i=0;i<a;i++)
    {
        cout<<endl<<arr4[i]<<"\t"<<bt[i]<<endl;
    }
    for(int i=1;i<a;i++)
    {
        int j=i-1;
        int d=bt[i];
        char e=arr4[i];
        while(j>=0 && bt[j]>d)
        {
            bt[j+1]=bt[j];
            arr4[j+1]=arr4[j];
            j--;
        }
          bt[j+1]=d;
            arr4[j+1]=e;
    }
     cout<<"\n\nName\tB.T\n";
    for(int i=0;i<a;i++)
    {
        cout<<endl<<arr4[i]<<"\t"<<bt[i];
    }
    int i=0;
    while(i<a)
    {
        if(i==0){
            wt[i]=0;
            ta[i]=bt[i];
        }
        else
        {
            wt[i]=ta[i-1];
            ta[i]=wt[i]+bt[i];
        }
        i++;
    }
     cout<<"\n\nName\tB.T\tW.T\tT.A";
    for(int i=0;i<a;i++)
    {
        cout<<endl<<arr4[i]<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\t"<<ta[i];
    }

}

✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌ SJF premp-----------------------

#include<iostream>

using namespace std;
int main()
{
      int n,temp,tt=0,min,d,i,j;
      float atat=0,awt=0,stat=0,swt=0;

      cout<<"enter no of processes"<<endl;
      cin>>n;
      int a[n],b[n],e[n],tat[n],wt[n];
  
      for(i=0;i<n;i++)
      {
            cout<<"enter arrival time ";       //input
            cin>>a[i];
      }
      for(i=0;i<n;i++)
      {
            cout<<"enter burst time ";      //input
            cin>>b[i];
      }
      for(i=0;i<n;i++)
      {
         for(j=i+1;j<n;j++)
          {
                if(b[i]>b[j])
                {
                      temp=a[i];
                      a[i]=a[j];
                      a[j]=temp;

                      temp=b[i];
                      b[i]=b[j];
                      b[j]=temp;
                }
          }
      }
      min=a[0];
      for(i=0;i<n;i++)
      {
            if(min>a[i])
            {
                  min=a[i];
                  d=i;
            }
      }
      tt=min;
      e[d]=tt+b[d];
      tt=e[d];

      for(i=0;i<n;i++)
      {
            if(a[i]!=min)
            {
                  e[i]=b[i]+tt;
                  tt=e[i];
            }
      }
      for(i=0;i<n;i++)
      {

            tat[i]=e[i]-a[i];
            stat=stat+tat[i];
            wt[i]=tat[i]-b[i];
            swt=swt+wt[i];
      }
      atat=stat/n;
      awt=swt/n;
      cout<<"Process  Arrival-time(s)  Burst-time(s)  Waiting-time(s)  Turnaround-time(s)\n";

    for(i=0;i<n;i++)
    {
    cout<<"P"<<i+1<<"              "<<a[i]<<"                "<<b[i]<<"                  "<<wt[i]<<"               "<<tat[i]<<endl;
    }

    cout<<"awt="<<awt<<" atat="<<atat;  //average waiting time and turn around time




}









✌✌✌✌✌✌✌✌✌✌✌👉 RounRobin-------------------------------




#include<iostream>
using namespace std;

void findWaitingTime( int n,int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

int t = 0; 

while (1)
{
bool done = true;

for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false; 
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}

else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}

if (done == true)
break;
}
}

void findTurnAroundTime( int n,int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

void findavgTime( int n, int bt[], int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime( n, bt, wt, quantum);

findTurnAroundTime( n, bt, wt, tat);

// Display processes along with all details
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}

cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
// process id's
int n;
cout<<"Enter the number of processes : \n";
cin>>n;
int bt[n];
cout<<"Enter Burst Time of each process  \n";
for(int i=0;i<n;i++){
cout<<"Enter burst time for Process P "<<i<<" :";
cin>>bt[i];
}
int quantum;
cout<<"\nEnter Quantum time for Round Robin Algorithm : ";
cin>>quantum;

findavgTime( n, bt, quantum);
return 0;
}


















Monday, 28 November 2022

cn

 PARITY CHECKER


#include<bits/stdc++.h>

using namespace std;

int parity_check(vector<int> &msg,int n)

{

int count=0;

for(int i=0;i<n;i++)

{

if(msg[i]==1)

{

count++;

}

}

return count;

}

int main()

{

int m,n;

cout<<"Enter total Number of bits: ";

cin>>n;

cout<<"Enter Number of bits:"<<endl;

vector<int> msg;

for(int i=0;i<n;i++)

{

cin>>m;

msg.push_back(m);

}

int a=parity_check(msg,n);

while(1){


int ch;

cout<<"\n\n---------Parity check-----------";

cout<<"\n\n1. Even parity";

cout<<"\n2. Odd Parity";

//cout<<"\n2. Odd Parity";

cout<<"\n\nEnter your choice: ";

cin>>ch;

switch(ch)

{

case 1:

if(a%2==0)

{

msg.push_back(0);

cout<<"\nNo error occurred";

}

else

{

msg.push_back(1);

cout<<"\nError occurred";

}

break;

case 2:

if(a%2!=0)

{

msg.push_back(0);

cout<<"\nNo error occurred";

}

else

{

msg.push_back(1);

cout<<"\nError occurred";

}

case 3:

{

exit(1)

break;

default:

cout<<"\nInvalid choice !!!";

}


}

}

return 0;

}


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


Hamming code



#include<bits/stdc++.h>

using namespace std;

void hammingCode(vector<int> &msg,int m)

{

int r=1;

while(pow(2,r)< (m+r+1))

{

r++;

}

 vector<int> hc(m+r);

 

 for(int i=0;i<r;++i)

 {

 hc[pow(2,i)-1]=-1;

}

int j=0;

for(int i=0;i<m+r;i++)

{

if(hc[i]!=-1)

{

hc[i]=msg[j];

j++;

}

}

for (int i = 0; i < (r + m); i++) 

{

 if(hc[i] != -1)

 continue;

 int x = log2(i + 1);

 int count = 0;

 for(int j = i + 2;j <= (r + m); ++j) 

{

 if(j & (1 << x)) 

{

 if(hc[j - 1] == 1) 

{

 count++;

 }

 }

 }

 

 

 //even parity

 if(count % 2 == 0) 

{

 hc[i] = 0;

 }

 

 else 

{

 hc[i] = 1;

 }

 }

 

 cout<<"-----------------Message bits---------------\n\n";

 for(int i=0;i<m;i++)

 {

 cout<<msg[i]<<" ";

}

cout<<"\n\n--------------Generated Hamming Code----------------\n";

 for(int i=0;i<hc.size();i++)

 {

 cout<<hc[i]<<" ";

}

 

}

int main()

{

int m;

cout<<"Enter number of message bits: ";

cin>>m;

int a;

vector<int> msg;

for(int i=0;i<m;i++)

{

cin>>a;

msg.push_back(a);

}

hammingCode(msg,m);

return 0;

}



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

CRC




#include <iostream>

#include <math.h>

#include <cstring>

using namespace std;

char exor(char a,char b) 

{

if(a==b)

return '0';

else

return '1';

}

void crc(char data[], char key[])

{

int datalen = strlen(data);

int keylen = strlen(key);

for(int i=0;i<keylen-1;i++) 

data[datalen+i]='0';

data[datalen+keylen-1]='\0';

int codelen = datalen+keylen-1; 

char temp[20],rem[20];

for(int i=0;i<keylen;i++)

rem[i]=data[i]; 

for(int j=keylen;j<=codelen;j++)

{

 for(int i=0;i<keylen;i++)

 temp[i]=rem[i]; 

 if(rem[0]=='0') 

 {

 for(int i=0;i<keylen-1;i++)

 rem[i]=temp[i+1];

 }

 else 

 { 

 for(int i=0;i<keylen-1;i++)

 rem[i]=exor(temp[i+1],key[i+1]);

 

 }

 

 if(j!=codelen)

 rem[keylen-1]=data[j]; 

 else

 rem[keylen-1]='\0';

}

 

for(int i=0;i<keylen-1;i++)

data[datalen+i]=rem[i]; 

data[codelen]='\0';

cout<<"CRC="<<rem<<"\nDataword="<<data;

}

int main()

{

char key[20],data[20];

cout<<"Enter the data:";

cin>>data;

cout<<"Enter the key:";

cin>>key;

crc(data,key); 

return 0;

}




-----------------------------------------Flow Control----------------------------------


Go Back and Q



#include <iostream>
#include <list>
#include <vector>
#include <unistd.h>
using namespace std;

class data
{
public:
int sw;
int sf; 
int sn;
int rn;
int ack;
vector<int> sv;
vector<int> rv;
list<int> window;
data()
{
sw = 4;
sf = 0;
sn = 0;
rn = 0;
ack= 0;
}
void sender();
void waitforevent();
void getdata();
void makeframe();
void sendframe();
void showWindow();
void receiver();
};

void data :: receiver()
{
cout<<"********* ON RECEIVER SIDE ************"<<endl;
waitforevent();
cout<<"Received "<<rn<<" frame"<<endl;
rv.push_back(window.front());
rn++;
ack++;
cout<<"Acknowledgement sent for "<<ack<<" frame .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
}


void data :: showWindow()
{
cout<<"Window currently contains : ";
list<int> :: iterator itr = window.begin();
for(itr; itr!=window.end(); itr++)
{
cout<<*itr;
}
cout<<endl;
}

void data :: sendframe()
{
cout<<"Window size is : "<<sw<<endl;
cout<<"Getting "<<sw<<" frames into window"<<endl;
for(int i=0; i<sw; i++)
{
window.push_back(sv.at(i));
}
sf = window.front();
sn = window.back();
showWindow();
cout<<"Sending these "<<sw<<" Frames without waiting for acknowlegement .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
cout<<"Sent successfully ..."<<endl;
for(int i=0; i<sv.size(); i++)
{
receiver();
cout<<"************ SENDER SIDE ****************"<<endl;
waitforevent();
cout<<"Acknowledgement Received ..."<<endl;
cout<<"Removing Acknowledged frame from window .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
window.pop_front();
if(i<(sv.size()-sw))
{
cout<<"Getting next frame into the window ";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
window.push_back(sv.at(i+4));
}
showWindow();
}
}




void data :: makeframe()
{
cout<<"Making Frames";
cout << " .";
sleep(2);
cout << " .";
sleep(2);
cout << " ."<<endl;
}

void data :: getdata()
{
int size;
cout<<"Enter the number of bits you want to send : "<<endl;
cin>>size;
cout<<"Enter the bits : "<<endl;
for(int i=0; i<size; i++)
{
int num;
cin>>num;
sv.push_back(num);
}
}


void data :: waitforevent()
{
cout << "Waiting for event";
sleep(2);
cout << " .";
sleep(2);
cout << " .";
sleep(2);
cout << " ."<<endl;
}

void data :: sender()
{
waitforevent();
getdata();
makeframe();
sendframe();
}


int main()
{
data d;
cout<<"**************** Data Transmission using Go Back N Protocol ***************"<<endl;
d.sender();
cout<<"*************After successfull Transmission***********"<<endl;
cout<<"The message sent by sender is : ";
for(int i=0; i<d.sv.size(); i++)
{
cout<<d.sv.at(i);
}
cout<<endl;
cout<<"The message received by receiver is : ";
for(int i=0; i<d.rv.size(); i++)
{
cout<<d.rv.at(i);
}
cout<<endl;
}




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


Stop and Wait



#include <iostream>
#include <unistd.h>
#include <vector>
using namespace std;

class data
{
public:
int sn;
int rn;
int ack;
int size;
int counter;
vector<int> sv;
vector<int> rv;
bool cansend;

data()
{
sn=0;
rn=0;
ack=0;
size=0;
counter=0;
cansend=true;
}
void sender();
void getdata();
void waitforevent();
void makeframe();
void sendframe();
void receiver(int i);
void result();
};


void data :: result()
{
cout<<"The data sent at sender side is : ";
for(int i=0; i<sv.size(); i++)
{
cout<<sv[i];
}
cout<<endl;
cout<<"The data received at receiver side is : ";
for(int i=0; i<rv.size(); i++)
{
cout<<rv[i];
}
cout<<endl;
}


void data :: receiver(int i)
{
cout<<"************ON RECEIVER SIDE*****************"<<endl;
cout<<"Waiting for an event .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;

    if(counter!=sv.size()/2)
    {
cout<<"Received "<<sn<<" Frame"<<endl;
cout<<" Timer stops"<<endl;
if(sn==rn)
{
cout<<"Extracting data ";
sleep(2);
cout<<" .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
sleep(2);
rv.push_back(i);
rn++;
cout<<"Sending Acknowledgement .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
}

    }

}

void data :: sendframe()
{
int ack1;
cout<<"Sending Frames : "<<endl;
for(int i=0; i<sv.size(); i++)
{
cout<<"Sending "<<sn<<" Frame .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
cout<<" Timer Starts .";
sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
receiver(sv[i]);
if(sn!=(rn-1))
{
            cout<<"************** ON SENDER SIDE ***************"<<endl;
            cout<<"Sender Timed Out "<<endl;
            cout<<"Acknowlegdement NOT received "<<endl;
            cout<<"Retrasmitting the "<<sn<<" Frame again .";
            cout<<" .";
            sleep(2);
            cout<<" ."<<endl;
            counter++;
            cout<<" Timer Starts again .";
            sleep(2);
            cout<<" .";
            sleep(2);
            cout<<" ."<<endl;
            receiver(sv[i]);
            sn++;
}else
{
cout<<"************** ON SENDER SIDE ***************"<<endl;
cout<<"Acknowledgement Received for  "<<rn<<" frame "<<endl;
            sn++;
            counter++;
}

}

cout<<" All frames sent Successfully ..."<<endl;
}


void data :: makeframe()
{
cout<<"Making Frames";
cout << " .";
sleep(2);
cout << " .";
sleep(2);
cout << " ."<<endl;
}

void data :: waitforevent()
{
cout << "Waiting for event";
sleep(2);
cout << " .";
sleep(2);
cout << " .";
sleep(2);
cout << " ."<<endl;
}

void data :: getdata()
{
cout<<"Enter the number of bits you want to send : "<<endl;
cin>>size;
cout<<"Enter the bits : "<<endl;
for(int i=0; i<size; i++)
{
int num;
cin>>num;
sv.push_back(num);
}
}



void data :: sender()
{
waitforevent();
getdata();
makeframe();
sendframe();
}




int main()
{
data d;
d.sender();
d.result();
}



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

SLECTIVE REPEAT


#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
using namespace std;

int n,r;
class frame
{
    public:
    char ack;
    int data;
};


void resendframe(frame frm[])
{
    cout<<"Resending frame "<<r<<" .";
    sleep(2);
cout<<" .";
sleep(2);
cout<<" ."<<endl;
    frm[r].ack='y';
    cout<<"Received frame is : "<<frm[r].data<<endl;

    cout<<"The message received at receiver side is : ";
    for(int i=1; i<=n; i++)
    {
        cout<<frm[i].data;
    }
    cout<<endl;
}

void receiver(frame frm[])
{
    r=n/2;
    frm[r].ack='n';
    for(int i=1; i<=n; i++)
    {
        if(frm[i].ack=='n')
        {
            cout<<"The frame number "<<r<<" is not received "<<endl;
        }
    }
    resendframe(frm);
    
}





int main()
{
    frame frm[10];
    int i;
    cout<<"Enter the number of frames to be sent : "<<endl;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        int j;
        cout<<"Enter the frame : "<<endl;
        cin>>j;
        frm[i].data = j;
        frm[i].ack = 'y';
    }
    receiver(frm);

    


}



-------------------------------------------------------Subnet Calculator----------------------------------------------

SUBNET Calculator


#include <iostream>
#include <math.h>
#include <iterator>
#include <vector>
#include <sstream>
using namespace std;

struct change : ctype<char>
{
    const mask* arr()
    {
        static vector<mask> m(classic_table(), classic_table() + table_size);
        m['.'] |= space;
        m['/'] |= space;
        return &m[0];
    }
    change(size_t s = 0) : ctype(arr(), false, s) {}
};

int n(int number)
{
    int num;

    if (number == 1)
        num = 128;
    if (number == 2)
        num = 192;
    if (number == 3)
        num = 224;
    if (number == 4)
        num = 240;
    if (number == 5)
        num = 248;
    if (number == 6)
        num = 252;
    if (number == 7)
        num = 254;

    return num;
}

int main()
{


    string str;
    cout << "Enter an IP address (format: x.x.x.x/y) : ";
    cin >> str;

    istringstream ins(str);

    ins.imbue(locale(ins.getloc(), new change()));
    istream_iterator<long> a(ins), end;
    vector<long> vect(a, end);
    char ch;

    if (vect[0] <= 127)
    {
        ch = 'A';
    }
    else if (vect[0] >= 128 && vect[0] <= 191)
    {
        ch = 'B';
    }
    else if (vect[0] >= 192 && vect[0] <= 223)
    {
        ch = 'C';
    }
    else {
        cout << "error" << '\n';
    }

    cout << "Number of usable Hosts: ";
    int nouh;
    nouh = 32 - vect[4];
    nouh = pow(2, nouh);
    cout << nouh - 2 << '\n';

    int snet;
    int snt;

    snet = vect[4] / 8;
    snt = snet;
    snet = vect[4] - (8 * snet);
    snet = n(snet);

    cout << "Class Type: " << ch << '\n';
    cout << "Subnet Address: ";

    if (snt == 0) {
        cout << snet << ".0.0.0" << '\n';
    }
    else if (snt == 1) {
        cout << "255." << snet << ".0.0" << '\n';
    }
    else if (snt == 2) {
        cout << "255.255." << snet << ".0" << '\n';
    }
    else if (snt == 3) {
        cout << "255.255.255." << snet << '\n';
    }
    else {
        cout << "ERROR!" << '\n';
    }

    int bc;
    int sub = 0;
    int sn = vect[4] / 8;
    sn = vect[4] - (8 * sn);
    sn = n(sn);
    bc = 256 - sn;
    bc = bc + sub - 1;

    cout << "Broadcast Address: ";
    if (vect[4] < 8) {
        cout << bc << ".255.255.255" << '\n';
    }

    if (vect[4] >= 8 && vect[4] < 16) {
        cout << vect[0] << "." << bc << ".255.255" << '\n';
    }

    if (vect[4] >= 16 && vect[4] < 24) {
        cout << vect[0] << "." << vect[1] << "." << bc << ".255" << '\n';
    }

    if (vect[4] >= 24 && vect[4] < 32) {
        cout << vect[0] << "." << vect[1] << "." << vect[2] << "." << bc << '\n';
    }

    return 0;
}

/*
Enter an IP address (format: x.x.x.x/y) : 192.168.10.1/16
Number of usable Hosts: 65534
Class Type: C
Subnet Address: 255.255.0.0
Broadcast Address: 192.168.255.255

--------------------------------
*/



------------------------------LINKSPACE ROUTING----------------------------------

    Links apce vector


#include<iostream>

#include<stdio.h>

using namespace std;

int main()

 

{

 

int count,src_router,i,j,k,w,v,min;

 

int cost_matrix[100][100],dist[100],last[100];

 

int flag[100];

 

cout<<"\n Enter the no of routers";

 

cin>>count;

 

cout<<"\n Enter the cost matrix values:";

 

for(i=0;i<count;i++)

 

 

{

 

for(j=0;j<count;j++)

 

{

 

printf("\n%d->%d:",i,j);

 

scanf("%d",&cost_matrix[i][j]);

 

if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;

 

}

 

}

 

printf("\n Enter the source router:");

 

scanf("%d",&src_router);

 

for(v=0;v<count;v++)

 

{

 

flag[v]=0;

 

last[v]=src_router;

 

dist[v]=cost_matrix[src_router][v];

 

}

 

flag[src_router]=1;

 

for(i=0;i<count;i++)

 

{

 

min=1000;

 

for(w=0;w<count;w++)

 

{

 

if(!flag[w])

 

if(dist[w]<min)

 

{

 

v=w;

 

min=dist[w];

 

}

 

}

 

flag[v]=1;

 

for(w=0;w<count;w++)

 

{

 

if(!flag[w])

 

if(min+cost_matrix[v][w]<dist[w])

 

{

 

dist[w]=min+cost_matrix[v][w];

 

last[w]=v;

 

}

 

}

 

}

 

for(i=0;i<count;i++)

 

{

 

printf("\n%d==>%d:Path taken:%d",src_router,i,i);

 

w=i;

 

while(w!=src_router)

 

{

 

printf("\n<--%d",last[w]);w=last[w];

 

}

 

printf("\n Shortest path cost:%d",dist[i]);

 

}

 

}



----------------------------------------Distance Vector-------------------------------------------



#include<iostream>

#include<conio.h>

using namespace std;

 

int main()

{

    int graph[50][50];

    int i,j,k,t;

    int nn;

 

    cout<<"\n Enter Number of Nodes:";

    cin>>nn;

 

    /* Initialize graph*/

    for (i=0;i<nn;i++)

    {

        for(j=0;j<nn;j++)

        {

            graph[i][j]=-1;

        }

    }

 

    /* Vertex names */

    char ch[7]={'A','B','C','D','E','F','G'};

 

    /* Get input */

    for (i=0;i<nn;i++)

    {

        for(j=0;j<nn;j++)

        {

            if(i==j)

            {

                graph[i][j]=0;

            }

            if(graph[i][j]==-1)

            {

                cout<<"\n Enter Distance between "<<ch[i]<<" - "<<ch[j]<<" : ";

                cin>>t;

                graph[i][j]=graph[j][i]=t;

            }

        }

    }

 

    /* Initializing via */

    int via[50][50];

    for (i=0;i<nn;i++)

    {

        for(j=0;j<nn;j++)

        {

            via[i][j]=-1;

        }

    }

 

    cout<<"\n After Initialization";

    /* Display table initialization */

    for (i=0;i<nn;i++)

    {

        cout<<"\n"<<ch[i]<<" Table";

 

        cout<<"\nNode\tDist\tVia";

        for(j=0;j<nn;j++)

        {

              cout<<"\n"<<ch[j]<<"\t"<<graph[i][j]<<"\t"<<via[i][j];

        }

    }

 

    //sharing table

    int sh[50][50][50];

    for(i=0;i<nn;i++)

    {

        for(j=0;j<nn;j++)

        {

            for (k=0;k<nn;k++)

            {

                /* Check if edge is present or not*/

                if((graph[i][j]>-1)&&(graph[j][k]>-1))

                {

                    sh[i][j][k]=graph[j][k]+graph[i][j];

                }

                else

                {

                    sh[i][j][k]=-1;

                }

            }

        }

    }

 

    /*displaying shared table */

    for(i=0;i<nn;i++)

    {

        cout<<"\n\n For "<<ch[i];

        for (j=0;j<nn;j++)

        {

            cout<<"\n From "<<ch[j];

            for(k=0;k<nn;k++)

            {

                cout<<"\n "<<ch[k]<<" "<<sh[i][j][k];

            }

        }

    }

    

    /* Updating */

    int final[50][50];

    for(i=0;i<nn;i++)

    {

        for(j=0;j<nn;j++)

        {

            /* Copy initial value from input graph*/

            final[i][j]=graph[i][j];

            via[i][j]=i;

            

            /*Update them*/

            /* Check condition a - b - c */

            for(k=0;k<nn;k++)

            {

                

                if((final[i][j]>sh[i][k][j]) || (final[i][j] == -1))

                {

                    if(sh[i][k][j]>-1)

                    {

                        final[i][j]=sh[i][k][j];

                        via[i][j]=k;

                    }

                }

            }

            /* After considering three vertex if final not found

                consider 4th

                a- b- c- d

            */

 

            if(final[i][j]==-1)

            {

                for(k=0;k<nn;k++)

                {

 

                    if((final[i][k]!=-1)&&(final[k][j]!=-1))

                    {

                        if((final[i][j]==-1) || ((final[i][j]!=-1) &&(final[i][j]>final[i][k]+final[k][j])))

                        {

                            if(final[i][k]+final[k][j]>-1)

                            {

                                final[i][j]=final[i][k]+final[k][j];

                                via[i][j]=k;

                            }

                        }

                    }

                    

                }

            }

        }

    }

 

    cout<<"\n After Update :";

    /* Display table Updation */

    for (i=0;i<nn;i++)

    {

        cout<<"\n"<<ch[i]<<" Table";

        cout<<"\nNode\tDist\tVia";

        for(j=0;j<nn;j++)

        {

            cout<<"\n"<<ch[j]<<"\t"<<final[i][j]<<"\t";

            if(i==via[i][j])

                cout<<"-";

            else

                cout<<ch[via[i][j]];

        }

    }

 

    getch();

}




----------------------------------------Socket Programming------------------------------------------


Client side.....



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netdb.h>


void error(const  char* msg)

{

    perror(msg);

    exit(1);

}

int main(int argc, char *argv[])

{

    int sockfd, port_no, temp;

    struct sockaddr_in server_address;

    struct hostent *server;

    char buffer[255];

    printf("Enter 'BHOKME' to disconnnect from Server \n");


    if(argc<3)

    {

        printf("Either Port number or host address not entered");

        exit(1);

    }


    port_no = atoi(argv[2]);

    sockfd = socket(AF_INET, SOCK_STREAM, 0  );

    

    if(sockfd < 0)

    {

        error("Error opening the socket");

    }


server = gethostbyname(argv[1]);

if(server == NULL)

{

    printf("No Host exist with such address");

}


bzero((char *) &server_address,  sizeof(server_address));

server_address.sin_family = AF_INET;

bcopy((char *) server->h_addr, (char *) &server_address.sin_addr, server->h_length);

server_address.sin_port = htons(port_no);


if(connect(sockfd, (struct sockaddr*) &server_address, sizeof(server_address)) < 0)

   {

    error("Connection Failed");

   }



while(1)

{

    bzero(buffer, 255);

    fgets(buffer, 255, stdin);

    temp = write(sockfd, buffer, strlen(buffer));

    if(temp < 0)

    {

error("Error wile writing");

    }

    bzero(buffer, 255);

    temp = read(sockfd, buffer, 255);

    if(temp < 0)

    {

error("Error while reading");

    }

printf("Server :  %s",buffer);


int trigger = strncmp("BHOKME", buffer, 6);

if(trigger == 0)

{   

break;  

}

}


close(sockfd);

return 0;


}








server Side....


#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>


void error(const  char* msg)

{

    perror(msg);

    exit(1);

}

int main(int argc, char *argv[])

{

printf("Enter 'BHOKME' to Close Server \n");

    if(argc<2)

    {

        printf("Port Number not Entered \n");

        exit(1);

    }


    int sockfd, sockfd_new, port_no, temp;

    char buffer[255];


    struct sockaddr_in server_address, client_address;

    socklen_t client_len;


    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if(sockfd<0)

    {

        error("Error Openinng Socket");

    }

    bzero((char *) &server_address, sizeof(server_address));

    port_no = atoi(argv[1]);

    

    server_address.sin_family = AF_INET;

    server_address.sin_addr.s_addr = INADDR_ANY;

    server_address.sin_port = htons(port_no);



    if(bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address))   < 0 )

    {

            error("Binding Failed");

    }


    listen(sockfd, 5);

    client_len = sizeof(client_address);


    sockfd_new = accept(sockfd, (struct sockaddr*) &client_address, &client_len);


    if(sockfd_new < 0)

    {

        error("Error while accepting the input");

    }


    while(1)

    {

        bzero(buffer, 255);

        temp = read(sockfd_new, buffer, 255);

        if(temp < 0)

        {

            error("Error While reading");

        }

        printf("client : %s \n", buffer );

        bzero(buffer, 255);

        fgets(buffer, 255, stdin);


        temp = write(sockfd_new, buffer, strlen(buffer));

        if(temp < 0)

        {

            error("Error while writing");

        }


        int trigger = strncmp("BHOKME", buffer, 6);

        if(trigger == 0)

        {   break;  }

    }

    close(sockfd_new);

    close(sockfd);

    return 0;




}










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

server side...


#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main(int argc, char const* argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = { 0 };
char* msg = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET,
SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr*)&address,
sizeof(address))
< 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket
= accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen))
< 0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
send(new_socket, msg, strlen(msg), 0);
printf("Hello message sent\n");
// buffer = { 0 };
valread = read(new_socket, buffer, 1024);
printf("Enter new msg: ");
scanf("%s",msg);
printf("%s\n", buffer);
send(new_socket, msg, strlen(msg), 0);
printf("New message sent\n");
// closing the connected socket
close(new_socket);
// closing the listening socket
shutdown(server_fd, SHUT_RDWR);
return 0;
}





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


Client side

#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main(int argc, char const* argv[])
{
int sock = 0, valread, client_fd;
struct sockaddr_in serv_addr;
char* msg = "Hello from client";
char buffer[1024] = { 0 };
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary
// form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
<= 0) {
printf(
"\nInvalid address/ Address not supported \n");
return -1;
}
if ((client_fd
= connect(sock, (struct sockaddr*)&serv_addr,
sizeof(serv_addr)))
< 0) {
printf("\nConnection Failed \n");
return -1;
}
send(sock, msg, strlen(msg), 0);
printf("Hello message sent\n");
valread = read(sock, buffer, 1024);
printf("%s\n", buffer);
// closing the connected socket
close(client_fd);
return 0;
}






Accessing and Parsing OneNote Notebook Content from Azure Storage Containers

Accessing and Parsing OneNote Notebook Content from Azure Storage Containers OneNote is a powerful tool for digital note-taking and collabor...