Wednesday, May 30, 2018

Project on Library Management System (C++)

=>The project titled Library Management system is Library management software for monitoring and controlling the transactions in a library. The project “Library Management System” is developed in C++, which mainly focuses on basic operations in a library like adding new member, new books, and updating new information and facility to borrow and return books.

The user will find it easy in this automated system rather than using the manual writing system. The system contains a database where all the information will be stored safely. The system is user-friendly and error free.
(For Investigatory Project)



//****************************************************************
// HEADER FILE USED IN PROJECT
//****************************************************************

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip.h>

//****************************************************************
// CLASS USED IN PROJECT
//****************************************************************

class book
{
 char bno[6];
 char bname[50];
 char aname[20];
 public:
 void create_book()
 {
  cout<<"\nNEW BOOK ENTRY...\n";
  cout<<"\nEnter The book no. : ";
  cin>>bno;
  cout<<"\n\nEnter The Name of The Book : ";
  gets(bname);
  cout<<"\n\nEnter The Author's Name : ";
  gets(aname);
  cout<<"\n\n\nBook Created...";
 }

 void show_book()
 {
  cout<<"\nBook No. : "<<bno;
  cout<<"\nBook Name : ";
  puts(bname);
  cout<<"Author Name : ";
  puts(aname);
 }

 void modify_book()
 {
  cout<<"\nBook No. : "<<bno;
  cout<<"\nModify Book Name : ";
  gets(bname);
  cout<<"\nModify Author's Name of Book : ";
  gets(aname);
 }

 char* retbno()
 {
  return bno;
 }

 void report()
 {
  cout<<bno<<setw(30)<<bname<<setw(30)<<aname<<endl;}
 }; //class ends here

class student
{
 char admno[6];
 char name[20];
 char stbno[6];
 int token;

 public:
 void create_student()
 {
  clrscr();
  cout<<"\nNEW STUDENT ENTRY...\n";
  cout<<"\nEnter The admission no. : ";
  cin>>admno;
  cout<<"\n\nEnter The Name of The Student : ";
  gets(name);
  token=0;
  stbno[0]='/0';
  cout<<"\n\nStudent Record Created...";
 }

 void show_student()
 {
  cout<<"\nAdmission no. : "<<admno;
  cout<<"\nStudent Name : ";
  puts(name);
  cout<<"\nNo of Book issued : "<<token;
  if(token==1)
  cout<<"\nBook No "<<stbno;
 }

 void modify_student()
 {
  cout<<"\nAdmission no. : "<<admno;
  cout<<"\nModify Student Name : ";
  gets(name);
 }

 char* retadmno()
 {
  return admno;
 }

 char* retstbno()
 {
  return stbno;
 }

 int rettoken()
 {
  return token;
 }

 void addtoken()
 {
  token=1;
 }

 void resettoken()
 {
  token=0;
 }

 void getstbno(char t[])
 {
  strcpy(stbno,t);
 }

 void report()
 {
  cout<<"\t"<<admno<<setw(20)<<name<<setw(10)<<token<<endl;}
 }; //class ends here

//****************************************************************
// global declaration for stream object, object
//****************************************************************

 fstream fp,fp1;
 book bk;
 student st;

//****************************************************************
// function to write in file
//****************************************************************

 void write_book()
 {
  char ch;
  fp.open("book.dat",ios::out|ios::app);
  do{
     clrscr();
     bk.create_book();
     fp.write((char*)&bk,sizeof(book));
     cout<<"\n\nDo you want to add more record...(y/n?)";
     cin>>ch;
    }while(ch=='y'||ch=='Y');
  fp.close();
 }

 void write_student()
 {
  char ch;
  fp.open("student.dat",ios::out|ios::app);
  do{
     st.create_student();
     fp.write((char*)&st,sizeof(student));
     cout<<"\n\ndo you want to add more record...(y/n?)";
     cin>>ch;
    }while(ch=='y'||ch=='Y');
  fp.close();
 }

//****************************************************************
// function to read specific record from file
//****************************************************************

 void display_spb(char n[])
 {
  cout<<"\nBOOK DETAILS\n";
  int flag=0;
  fp.open("book.dat",ios::in);
  while(fp.read((char*)&bk,sizeof(book)))
  {
   if(strcmpi(bk.retbno(),n)==0)
   {
    bk.show_book();
    flag=1;
   }
  }
  fp.close();
  if(flag==0)
  cout<<"\n\nBook does not exist";
  getch();
 }

 void display_sps(char n[])
 {
  cout<<"\nSTUDENT DETAILS\n";
  int flag=0;
  fp.open("student.dat",ios::in);
  while(fp.read((char*)&st,sizeof(student)))
  {
   if((strcmpi(st.retadmno(),n)==0))
   {
    st.show_student();
    flag=1;
   }
  }
  fp.close();
  if(flag==0)
  cout<<"\n\nStudent does not exist";
  getch();
 }

//****************************************************************
// function to modify record of file
//****************************************************************

 void modify_book()
 {
  char n[6];
  int found=0;
  clrscr();
  cout<<"\n\n\tMODIFY BOOK REOCORD... ";
  cout<<"\n\n\tEnter The book no. of The book : ";
  cin>>n;
  fp.open("book.dat",ios::in|ios::out);
  while(fp.read((char*)&bk,sizeof(book)) && found==0)
  {
   if(strcmpi(bk.retbno(),n)==0)
   {
    bk.show_book();
    cout<<"\nEnter The New Details of book : "<<endl;
    bk.modify_book();
    int pos=-1*sizeof(bk);
    fp.seekp(pos,ios::cur);
    fp.write((char*)&bk,sizeof(book));
    cout<<"\n\n\t Record Updated";
    found=1;
   }
  }
  fp.close();
  if(found==0)
  cout<<"\n\n Record Not Found ";
  getch();
 }

 void modify_student()
 {
  char n[6];
  int found=0;
  clrscr();
  cout<<"\n\n\tMODIFY STUDENT RECORD... ";
  cout<<"\n\n\tEnter The admission no. of The student : ";
  cin>>n;
  fp.open("student.dat",ios::in|ios::out);
  while(fp.read((char*)&st,sizeof(student)) && found==0)
  {
   if(strcmpi(st.retadmno(),n)==0)
   {
    st.show_student();
    cout<<"\nEnter The New Details of student : "<<endl;
    st.modify_student();
    int pos=-1*sizeof(st);
    fp.seekp(pos,ios::cur);
    fp.write((char*)&st,sizeof(student));
    cout<<"\n\n\t Record Updated";
    found=1;
   }
  }
  fp.close();
  if(found==0)
  cout<<"\n\n Record Not Found ";
  getch();
 }

//****************************************************************
// function to delete record of file
//****************************************************************

 void delete_student()
 {
  char n[6];
  int flag=0;
  clrscr();
  cout<<"\n\n\n\tDELETE STUDENT...";
  cout<<"\n\nEnter The admission no. of the Student You Want To Delete : ";
  cin>>n;
  fp.open("student.dat",ios::in|ios::out);
  fstream fp2;
  fp2.open("Temp.dat",ios::out);
  fp.seekg(0,ios::beg);
  while(fp.read((char*)&st,sizeof(student)))
  {
   if(strcmpi(st.retadmno(),n)!=0)
   fp2.write((char*)&st,sizeof(student));
   else
   flag=1;
  }
  fp2.close();
  fp.close();
  remove("student.dat");
  rename("Temp.dat","student.dat");
  if(flag==1)
  cout<<"\n\n\tRecord Deleted...";
  else
  cout<<"\n\nRecord not found";
  getch();
 }

 void delete_book()
 {
  char n[6];
  clrscr();
  cout<<"\n\n\n\tDELETE BOOK...";
  cout<<"\n\nEnter The Book no. of the Book You Want To Delete : ";
  cin>>n;
  fp.open("book.dat",ios::in|ios::out);
  fstream fp2;
  fp2.open("Temp.dat",ios::out);
  fp.seekg(0,ios::beg);
  while(fp.read((char*)&bk,sizeof(book)))
  {
   if(strcmpi(bk.retbno(),n)!=0) //change later
   {
    fp2.write((char*)&bk,sizeof(book));
   }
  }
  fp2.close();
  fp.close();
  remove("book.dat");
  rename("Temp.dat","book.dat");
  cout<<"\n\n\tRecord Deleted...";
  getch();
 }

//****************************************************************
// function to display all students list
//****************************************************************

 void display_alls()
 {
  clrscr();
  fp.open("student.dat",ios::in);
  if(!fp)
  {
   cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
   getch();
   return;
  }
  gotoxy(35,2);
  cout<<"STUDENT LIST\n\n";
  cout<<"==================================================================\n";
  cout<<"\tAdmission No."<<setw(10)<<"Name"<<setw(20)<<"Book Issued\n";
  cout<<"==================================================================\n";

  while(fp.read((char*)&st,sizeof(student)))
  {
   st.report();
  }
  fp.close();
  getch();
 }

//****************************************************************
// function to display Books list
//****************************************************************

 void display_allb()
 {
  clrscr();
  fp.open("book.dat",ios::in);
  if(!fp)
  {
   cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
   getch();
   return;
  }
  cout<<"\n\n\tBook LIST\n\n";
  cout<<"=========================================================================\n";
  cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author\n";
  cout<<"=========================================================================\n";

  while(fp.read((char*)&bk,sizeof(book)))
  {
   bk.report();
  }
  fp.close();
  getch();
 }

//****************************************************************
// function to issue book
//****************************************************************

 void book_issue()
 {
  char sn[6],bn[6];
  int found=0,flag=0;
  clrscr();
  cout<<"\n\nBOOK ISSUE...";
  cout<<"\n\n\tEnter The student's admission no. : ";
  cin>>sn;
  fp.open("student.dat",ios::in|ios::out);
  fp1.open("book.dat",ios::in|ios::out);
  while(fp.read((char*)&st,sizeof(student)) && found==0)
  {
   if(strcmpi(st.retadmno(),sn)==0)
   {
    found=1;
    if(st.rettoken()==0)
    {
     cout<<"\n\n\tEnter the book no. : ";
     cin>>bn;
     while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
     {
      if(strcmpi(bk.retbno(),bn)==0)
      {
       bk.show_book();
       flag=1;
       st.addtoken();
       st.getstbno(bk.retbno());
       int pos=-1*sizeof(st);
       fp.seekp(pos,ios::cur);
       fp.write((char*)&st,sizeof(student));
       cout<<"\n\n\t Book issued successfully\n\nPlease Note: Write the current date in backside of your book and submit within 15 days, fine Rs. 1 for each day after 15 days period";
      }
     }
      if(flag==0)
      cout<<"Book no. does not exist";
    }
   else
   cout<<"You have not returned the last book ";
   }
  }
  if(found==0)
  cout<<"Student record not exist...";
  getch();
  fp.close();
  fp1.close();
 }

//****************************************************************
// function to deposit book
//****************************************************************

 void book_deposit()
 {
  char sn[6],bn[6];
  int found=0,flag=0,day,fine;
  clrscr();
  cout<<"\n\nBOOK DEPOSIT ...";
  cout<<"\n\n\tEnter The student's admission no. : ";
  cin>>sn;
  fp.open("student.dat",ios::in|ios::out);
  fp1.open("book.dat",ios::in|ios::out);
  while(fp.read((char*)&st,sizeof(student)) && found==0)
  {
   if(strcmpi(st.retadmno(),sn)==0)
   {
    found=1;
    if(st.rettoken()==1)
    {
     while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
     {
      if(strcmpi(bk.retbno(),st.retstbno())==0)
      {
       bk.show_book();
       flag=1;
       cout<<"\n\nBook deposited in no. of days : ";
       cin>>day;
       if(day>15)
       {
fine=(day-15)*1;
cout<<"\n\nFine has to deposited Rs."<<fine;
       }
       st.resettoken();
       int pos=-1*sizeof(st);
       fp.seekp(pos,ios::cur);
       fp.write((char*)&st,sizeof(student));
       cout<<"\n\n\t Book deposited successfully";
      }
     }
    if(flag==0)
    cout<<"Book no does not exist";
    }
    else
    cout<<"No book is issued..please check!!";
   }
  }
  if(found==0)
  cout<<"Student record not exist...";
  getch();
  fp.close();
  fp1.close();
 }

//****************************************************************
// INTRODUCTION FUNCTION
//****************************************************************

 void intro()
 {
  clrscr();
  gotoxy(25,2);
  cout<<"LIBRARY MANAGEMENT SYSTEM";
  gotoxy(25,3);
  cout<<"=========================";
  cout<<"\n\nMADE BY : XYZ";
  cout<<"\n\nSCHOOL : ABC";
  cout<<"\n\t New Delhi";
  getch();
 }

//****************************************************************
// ADMINISTRATOR MENU FUNCTION
//****************************************************************

 void admin_menu()
 {
  clrscr();
  int ch2;
  cout<<"\n\n\n\tADMINISTRATOR MENU";
  cout<<"\n\n\t1.CREATE STUDENT RECORD";
  cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORD";
  cout<<"\n\n\t3.DISPLAY SPECIFIC STUDENT RECORD ";
  cout<<"\n\n\t4.MODIFY STUDENT RECORD";
  cout<<"\n\n\t5.DELETE STUDENT RECORD";
  cout<<"\n\n\t6.CREATE BOOK ";
  cout<<"\n\n\t7.DISPLAY ALL BOOKS ";
  cout<<"\n\n\t8.DISPLAY SPECIFIC BOOK ";
  cout<<"\n\n\t9.MODIFY BOOK ";
  cout<<"\n\n\t10.DELETE BOOK ";
  cout<<"\n\n\t11.BACK TO MAIN MENU";
  cout<<"\n\n\tPlease Enter Your Choice (1-11) ";
  cin>>ch2;
  switch(ch2)
  {
   case 1: clrscr();
   write_student();break;
   case 2: display_alls();break;
   case 3: char num[6];
   clrscr();
   cout<<"\n\n\tPlease Enter The Admission No. : ";
   cin>>num;
   display_sps(num);
   break;
   case 4: modify_student();break;
   case 5: delete_student();break;
   case 6: clrscr();
   write_book();break;
   case 7: display_allb();break;
   case 8: {
    char num[6];
    clrscr();
    cout<<"\n\n\tPlease Enter The book No. : ";
    cin>>num;
    display_spb(num);
    break;
   }
   case 9: modify_book();break;
   case 10: delete_book();break;
   case 11: return;
   default: cout<<"\a";
  }
  admin_menu();
 }

//****************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************

void main()
{
 char ch;
 intro();
 do
 {
  clrscr();
  cout<<"\n\n\n\tMAIN MENU";
  cout<<"\n\n\t01. BOOK ISSUE";
  cout<<"\n\n\t02. BOOK DEPOSIT";
  cout<<"\n\n\t03. ADMINISTRATOR MENU";
  cout<<"\n\n\t04. EXIT";
  cout<<"\n\n\tPlease Select Your Option (1-4) ";
  ch=getche();
  switch(ch)
  {
   case '1': clrscr();
     book_issue();
     break;
   case '2': book_deposit();
     break;
   case '3': admin_menu();
     break;
   case '4': exit(0);
     default :cout<<"\a";
  }
 }while(ch!='4');
}

//****************************************************************
// END OF PROJECT
//****************************************************************

Tuesday, May 22, 2018

Program to implement first-fit, best-fit and worst-fit allocation strategies

/* Program to implement first-fit, best-fit and worst-fit allocation strategies.
*/

#include<stdio.h>
#include<stdlib.h>
void display(int ,int [10]);
void firstfit(int ,int [10],int [10],int);
void bestfit(int ,int [10],int [10],int);
void worstfit(int,int [10],int [10],int);
int main()
{
 int npr,nh=0,i,j,k,pr[10],hol[10],ch,temp[10];

 printf("\n Enter the number of processes:");
 scanf("%d",&npr);
 printf("\nNumber of holes:");
 scanf("%d",&nh);
 for(i=0;i<npr;i++)
 {
  printf("\nProcess %d:",i+1);
  scanf("%d",&pr[i]);
 }
 for(i=0;i<nh;i++)
 {
  printf("\nHole %d:",i+1);
  scanf("%d",&hol[i]);
  temp[i]=hol[i];
 }
 do
 {
  printf("\n----------MENU------------"
"\n1.FIRST-FIT \n2.BEST FIT"
"\n3.WORST FIT\n4.EXIT\nENTER YOUR CHOICE:");
  scanf("%d",&ch);
  for(i=0;i<nh;i++)
  temp[i]=hol[i];
  switch(ch)
  {
   case 1: firstfit(npr,temp,pr,nh);
   break;
   case 2: bestfit(npr,temp,pr,nh);
   break;
   case 3:worstfit(npr,temp,pr,nh);
  break;
   case 4:exit (0);
  break;
   default:printf("\nWrong choice......!");
  }
 }
 while(ch!=4);
 return 0;
}
//First Fit
void firstfit(int npr,int hol[10],int pr[10],int nh)
{
 int i,j,k,flag=0;
 for(i=0;i<npr;i++)
 {
  flag=0;
  for(j=0;j<nh;j++)
  {
   if(pr[i]<hol[j])
   {
    hol[j]=hol[j]-pr[i];
    printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
    flag=1;
    break;
   }
   else if(pr[i]==hol[j])
   {
    flag=1;
    printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
    for(k=j;k<nh-1;k++)
    {
     hol[k]=hol[k+1];
    }
    nh--;
    break;
   }
  }
  if(flag==0)
  {
   printf("\n Hole is not Available.....");
   break;
  }
  display(nh,hol);
 }
}
//Best fit
void bestfit(int npr,int hol[10],int pr[10],int nh)
{
 int i,j,k,min,flag;
 for(i=0;i<npr;i++)
 {
  flag=0;
  for(j=0;j<nh;j++)
  {
   if(pr[i]<=hol[j])
   {
    flag=1;
    min=j;
    for(k=j;k<nh;k++)
    {
     if((hol[min]>hol[k])&&(pr[i]<=hol[k]))
     min=k;
    }
    if(pr[i]<hol[min])
    {
     printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
     hol[min]=hol[min]-pr[i];
     break;
    }
    else if(pr[i]==hol[min])
    {
     printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
     for(k=min;k<nh-1;k++)
     hol[k]=hol[k+1];
     nh--;
     break;
    }
   }
  }
  if(flag==0)
  {
   printf("\n Hole is not Available.....");
   break;
  }
  display(nh,hol);
 }
}
//worst fit
void worstfit(int npr,int hol[10],int pr[10],int nh)
{
 int i,j,k,max,flag;
 for(i=0;i<npr;i++)
 {
  flag=0;
  for(j=0;j<nh;j++)
  {
   if(pr[i]<=hol[j])
   {
    printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
    flag=1;
    max=j;
    for(k=j;k<nh;k++)
    {
     if((hol[max]<hol[k])&&(pr[i]<=hol[k]))
     max=k;
    }
    if(pr[i]<hol[max])
    {
     printf("\nCPU is Allocated for process %d",i+1);
     hol[max]=hol[max]-pr[i];
     break;
    }
    else if(pr[i]==hol[max])
    {
     printf("\nCPU is Allocated for process %d",i+1);
     for(k=max;k<nh-1;k++)
     hol[k]=hol[k+1];
     nh--;
     break;
    }
   }
  }
  if(flag==0)
  {
   printf("\n Hole is not Available.....");
   break;
  }
  display(nh,hol);
 }
}
void display(int nh,int hol[10])
{
 int i=0;
 printf("\nHoles:");
 for(i=0;i<nh;i++)
 {
  printf("%d\t",hol[i]);
 }
}




OUTPUT SCREEN:



Program to implement the following scheduling algorithms

/* Program to implement the following scheduling algorithms
a. FCFS
b. Shortest Job First
c. Non-preemptive priority based
d. Round Robin
*/

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

typedef struct process
{
int pid,st,ft,prior;
int wt,tat,at,bt;
int flag;
}pr;

int n;
pr p[10];
void accept(int);
void fcfs();
void disp_gantt();
void sjf_sort();
void priority_sort();
void round_robin(int);

void main()
{
int ch,tq;
do
{
printf("\n\t******Scheduling Algorithms********");
printf("\n\t1. FCFS\n\t2. SJF\n\t3. Priority Based\n\t4. Round Robin\n\t5. Exit");
printf("\n\t Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: accept(ch);
fcfs();
break;

case 2: accept(ch);
sjf_sort();
fcfs();
break;

case 3: accept(ch);
priority_sort();
fcfs();
break;

case 4: printf("\n\tEnter the Time Quantum: ");
scanf("%d",&tq);
accept(ch);
round_robin(tq);
break;

case 5: break;
}
printf("\n\tPress any key to continue:");
}while(ch!=5);
}

void accept(int ch)
{
int i;
printf("\n\tHow many processes: ");
scanf("%d",&n);
printf("\tEnter the values\n");
if(ch==1 || ch==2)
{
printf("\n\tArrival Time and Burst Time\n");
for(i=0;i<n;i++)
{
printf("\tEnter for Process %d :",i);
scanf("%d%d",&p[i].at,&p[i].bt);
p[i].pid=i;
}
}
if(ch==3)
{
printf("\n\tArrival Time , Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\tEnter for Process %d :",i);
scanf("%d%d%d",&p[i].at,&p[i].bt,&p[i].prior);
p[i].pid=i;
}
}
if(ch==4)
{
printf("\n\tArrival Time and Burst Time\n");
for(i=0;i<n;i++)
{
printf("\tEnter for Process %d :",i);
scanf("%d%d",&p[i].at,&p[i].bt);
p[i].pid=i;
}
}
}

void fcfs()
{
int i;
float avgtat=0,avgwt=0;
for(i=0;i<n;i++)
{
if(i==0)
{
p[i].st=i;
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].wt+p[i].bt;
p[i].ft=p[i].st+p[i].bt;
}
else
{
p[i].st=p[i-1].ft;
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].wt+p[i].bt;
p[i].ft=p[i].st+p[i].bt;
}
avgtat=avgtat+p[i].tat;
avgwt=avgwt+p[i].wt;
}
avgtat=avgtat/n;
avgwt=avgwt/n;
disp_gantt(n);
printf("\n\t** Average Turn Around Time:%f **",avgtat);
printf("\n\t** Average waiting time:%f **",avgwt);
}

void disp_gantt()
{
int i;
printf("\n\tGANTT CHART\n ");
for(i=0;i<n;i++)
{
printf("\t P%d",p[i].pid);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("\t %d",p[i].st);
}
printf("\t %d",p[i-1].ft);
printf("\n");
}

void sjf_sort()
{
int i,j;
pr temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].bt>p[j+1].bt)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;

}
}
}
}

void priority_sort()
{
int i,j;
pr temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].prior>p[j+1].prior)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;

}
}
}
}

void round_robin(int tq)
{
int allterminate=0,cnt=0,i;
int gt[30],ps[30],temp=0,pcnt=0;
float avgtat=0;
printf("\n\tGANTT CHART\n");
while(allterminate<n)
{
for(i=0;i<n;i++)
{
if(p[i].flag==0) //process incomplete
{
p[i].st=temp;
cnt++;
if(p[i].bt>=tq)//burst time > time slice
{

p[i].bt=p[i].bt-tq;
temp=temp+tq;
gt[cnt]=temp;
ps[pcnt]=p[i].pid;
pcnt++;
}
else
{ //burst time < time slice
temp=temp+p[i].bt;
gt[cnt]=temp;
p[i].bt=0;
ps[pcnt]=p[i].pid;
pcnt++;
}

if(p[i].bt==0)
{
allterminate++;
p[i].flag=1;
p[i].ft=temp;
}

if(p[i].flag==1)
{
p[i].tat=p[i].ft;
avgtat=avgtat+p[i].tat;
}
}//if

}//for

}//while
for(i=0;i<pcnt;i++)
{
printf("\t P%d",ps[i]);
}
printf("\n\t0 ");
for(i=1;i<=cnt;i++)
{
printf("\t %d ",gt[i]);
}
avgtat=avgtat/n;
printf("\n\t** Average Turn Around Time:%f **",avgtat);
}




OUTPUT SCREEN:



Program to understand working of pthread library

/* Program to understand working of pthread library.
*/

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
int sum;
void *runner(void *param);
int main(int argc,char *argv[])
{
 pthread_t tid;
 pthread_attr_t attr;
if(argc!=2)
{
 fprintf(stderr,"%d must be>=0\n",atoi(argv[1]));
 return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("Sum=%d\n",sum);
return 0;
}
void *runner(void *param)
{
int i,upper=atoi(param);
sum=0;
for(i=1;i<=upper;i++)
sum+=i;
pthread_exit(0);
}




OUTPUT SCREEN:


Program to demonstrate Inter-Process Communication (IPC) between parent and child

/* Program to demonstrate Inter-Process Communication (IPC) between parent and child using pipe system call.
*/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/uio.h>
#include<sys/types.h>
main()
{
int pid,pfd[2],n,a,b,c;
if(pipe(pfd)==-1)
{
printf("\nError in pipe connection\n");
exit(1);
}
pid=fork();
if(pid>0)
{
printf("\nParent Process");
printf("\nFibonacci Series");
printf("\nEnter the limit for the series:");
scanf("%d",&n);
close(pfd[0]);
write(pfd[1],&n,sizeof(n));
close(pfd[1]);
exit(0);
}
else
{
close(pfd[1]);
read(pfd[0],&n,sizeof(n));
printf("\nChild Process");
a=0;
b=1;
close(pfd[0]);
printf("\nFibonacci Series is:");
printf("\n\n%d\n%d",a,b);
while(n>1)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
n--;
}
}
}



OUTPUT SCREEN:


Program to demonstrate Producer-Consumer Problem

/* Program to demonstrate producer-consumer problem using shared memory.
*/

#include<stdio.h>
int n;
void main()
{

int Q[30];
int front,rear;
int value;
char rr;
int ch;
printf("Enter the size of the Buffer=");
scanf("%d",&n);
printf("\n");
n++;

front=rear=0;
do
{
printf("\nMENU");
printf("\n1.Producer");
printf("\n2.Consume");
printf("\n3.Display Inventory Status");
printf("\nEnter Your Choice=");
scanf("%d",&ch);
if(ch==1)
{

int value=1;
 if ((rear+1)%n==front)
{
printf("The Buffer is full\n");
printf("Unable to Produce\n");
}
 else
 {
rear=(rear+1)%n;
Q[rear]=value;
printf("\nItem Produced");
 }

}
if(ch==2)
{

 int val;
 if(front!=rear)
 {
front=(front+1)%n;
val=Q[front];
printf("\nItem Consumed");
//printf("The Consumed value is: "<<val;
 }
 else
 {
 printf("The Inventory is empty");
 printf("\nUnable to Consume Item");
 }

}
if(ch==3)
{

 int i;
 i=front;
 while(i!=rear)
 {
 i=(i+1)%n;
 printf("\n");
 printf("%d",Q[i]);
 printf("  ");
 }
}

printf("\nWANT TO RUN IT AGAIN=");
scanf("%s",&rr);
}while(rr=='y'||rr=='Y');

}
/*
void add(int Q[],int front,int &rear,int value)
 {
 if ((rear+1)%n==front)
{
printf("The Buffer is full\n");
printf("Unable to Produce\n");
}
 else
 {
rear=(rear+1)%n;
Q[rear]=value;
printf("\nItem Produced");
 }
}


 void del(int Q[],int &front,int rear)
 {
 int val;
 if(front!=rear)
 {
front=(front+1)%n;
val=Q[front];
printf("\nItem Consumed");
//printf("The Consumed value is: "<<val;
 }
 else
 {
 printf("The Inventory is empty");
 printf("\nUnable to Consume Item");
 }
 }


 void display(int Q[],int front,int rear)
 {
 int i;
 i=front;
 while(i!=rear)
 {
 i=(i+1)%n;
 cout<<endl;
 printf("%d",Q[i]);
 printf("  ");
 }
 }




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
d. before terminating, the parent waits for the child to finish its task
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else if(a==0)
{
 printf("in child process id=%d\n",getpid());
}
else
{
 wait(NULL);
 printf("in parent process id=%d\n",getppid());
}
return 0;
}




OUTPUT SCREEN:


Project on Library Management System (C++)

=> The project titled Library Management system is Library management software for monitoring and controlling the transactions in a lib...