14SCS16 Aos Lab

Share Embed Donate


Short Description

VTU MTech Computer Science 2014 Scheme Lab program. Advanced Operating Systems lab programs....

Description

AOS record

LABORATORY WORK Note: The following programs can be executed on Java/C#/ any equivalent language or tool Page with suitable platform. no. 01

Design and Develop a shell that should support at least 20 commands.

02

Design and develop a program to implement lazy buddy system algorithm.

03

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.

04

Use ECOS operating system to develop a program for controlling accessing to a pool of resources using mutexes and condition variables.

05

Design and develop a program to realize the virus classification, such as boot sector infector, file infector and macro virus.

AOS record

Program 1 Design and Develop a shell that should support at least 20 commands. A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute, or by creating text scripts of one or more such commands. Users typically interact with a Unix shell using a terminal emulator, however, direct operation via serial hardware connections, or networking session, are common for server systems. A Shell is the most fundamental way that a user can interact with the system, and the shell hides the details of the underlying operating system from the user. The most influential Unix shells have been the Bourne shell and the C shell. The Bourne shell, sh, was written by Stephen Bourne at AT&T as the original Unix command line interpreter; it introduced the basic features common to all the Unix shells, including piping, here documents, command substitution, variables, control structures for condition-testing and looping and filename wildcarding. The C shell, csh, was written by Bill Joy while a graduate student at University of California, Berkeley. The language, including the control structures and the expression grammar, was modelled on C. The C shell also introduced a large number of features for interactive work, including the history and editing mechanisms, aliases, directory stacks, tilde notation, cdpath, job control and path hashing. Commands Command time pwd man ls echo

date cal cp file1 file2

Description Measure Program running time Print Working Directory Help manual List information about file(s) Display message on screen Display or change the date & time Display a calendar copy file1 to file2

AOS record

#include #include #include #include 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 cmdexecutes(char **arguvec) { pid_t pid; int status; pid = fork(); if(pid < 0) { printf("\n error in creating fork"); exit(0); } else if(pid == 0) { if(execvp (*arguvec, arguvec) < 0) { printf("\n error in calling exec"); exit(0); } }

else {

AOS record

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) { exit(0); } cmdexecutes(arguvec); } return 0; }

Output: ubuntu@ubuntu-VirtualBox:~/lab$ gedit 1.c ubuntu@ubuntu-VirtualBox:~/lab$ cc 1.c

AOS record

ubuntu@ubuntu-VirtualBox:~/lab$ ./a.out [MYSHELL]ls 1.c 1.c~ a.out [MYSHELL]who ubuntu tty7 2015-11-25 09:04 ubuntu pts/1 2015-11-25 09:27 (:0) [MYSHELL]ps PID TTY TIME 2610 pts/1 00:00:01 2825 pts/1 00:00:00 2828 pts/1 00:00:00

CMD bash a.out ps

[MYSHELL]date Wed Nov 25 09:38:46 IST 2015 [MYSHELL]cal November 2015 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [MYSHELL]pwd /home/ubuntu/lab [MYSHELL]

AOS record

Program 2 Design and Develop a program to implement lazy buddy system algorithm. Lazy-Buddy System o

Combines free buffer coalescing with a power of two allocator.

o

Creates small buffers by repeatedly halving a large buffer and coalescing adjacent free buffer.

o

Each half of a split buffer is called a Buddy.

o

Coalescing delay time taken to coalesce a single buffer with its buddy.

o

To avoid coalescing delay we can defer coalescing until it becomes necessary and then coalesces as many buffers as possible.

o

It improves the average time for allocation and release.

o

SVR4 offers an intermediate solution which is more efficient.

o

Buffer release involves two steps: First the buffer is put on the free list. Second the buffer is marked as free in the bitmap and coalesced with adjacent buffer is possible.

o

Lazy buddy system always performs the first step, whether it performs the second step it depends on the state of the buffer class.

o

Buffer class can be in one of 3 states: Lazy, Reclaiming, Accelerated.

o

State of the buffer determined by the Slack, Slack= N-2L-G where, N- number of buffers in class. L- number of locally from buffers. G- number of globally free buffers. System is in, Lazy state when Slack>=2, Reclaiming state when Slack=1, Accelerated state when Slack=0.

AOS record

#include #include int tree[2050], i, j, k; void segmentalloc(int, int), makedivided(int), makefree(int), printing(int, int); int place(int), power(int, int); int main() { int totsize, ch, req; printf("BUDDY SYSTEM REQUIREMENTS"); printf("\n Enter the Size of the memory : "); scanf("%d", &totsize); while(1) { printf("\n B U D D Y S Y S T E M "); printf("\n 1) Locate the process into the Memory"); printf("\n 2) Remove the process from Memory"); printf("\n 3) Tree structure for Memory allocation Map"); printf("\n 4) Exit"); printf("\n Enter your choice : "); scanf("%d", &ch); switch(ch) { case 1: printf("\n MEMORY ALLOCATION "); printf("\n Enter the Process size : "); scanf("%d", &req); segmentalloc(totsize, req); break; case 2: printf("\n MEMORY DEALLOCATION "); printf("\n Enter the process size : "); scanf("%d", &req); makefree(req); break;

AOS record

case 3: printf("\n MEMORY ALLOCATION MAP"); printing(totsize, 0); break; default: return; } } } 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("\n The system don't have enough free memory"); printf("\n Suggession : Go for VIRTUAL MEMORY"); return; } while(1) { if(request < size && request > (size/2)) break; else { size /= 2; flevel++; } } for(i = power(2, flevel) - 1; i 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) {

AOS record

tree[node%2 == 0? (node-1)/2 : node/2] = 0; node = node%2 == 0 ? (node-1)/2 : node/2; } else break; } } int power(int x, int y) { int z, ans; if(y == 0) return 1; ans = x; for(z=1; z= llimit && node 1) printf("---> Allocated %d ", tree[node]); else if(tree[node] == 1) printf("---> Divided"); else printf("---> Free"); printing(totsize, 2*node+1); printing(totsize, 2*node+2); } }

AOS record

Output: ubuntu@ubuntu-VirtualBox:~/lab$ gedit 2.c ubuntu@ubuntu-VirtualBox:~/lab$ cc 2.c ubuntu@ubuntu-VirtualBox:~/lab$ ./a.out BUDDY SYSTEM REQUIREMENTS Enter the Size of the memory : 500 BUDDY SYSTEM 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 MEMORY ALLOCATION Enter the Process size : 400 Result : Successful Allocation BUDDY SYSTEM 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 MEMORY ALLOCATION MAP 500 ---> Allocated 400 BUDDY SYSTEM 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 MEMORY DEALLOCATION Enter the process size : 400

BUDDY SYSTEM 1) Locate the process into the Memory

AOS record

2) Remove the process from Memory 3) Tree structure for Memory allocation Map 4) Exit Enter your choice : 4 Case 2: BUDDY SYSTEM REQUIREMENTS Enter the Size of the memory : 500

BUDDY SYSTEM 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

MEMORY ALLOCATION Enter the Process size : 600 RESULT : The system don't have enough free memory Suggession : Go for VIRTUAL MEMORY

BUDDY SYSTEM 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 :

Case 3: BUDDY SYSTEM REQUIREMENTS

AOS record

Enter the Size of the memory : 500

BUDDY SYSTEM 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

MEMORY ALLOCATION Enter the Process size : 100 Result : Successful Allocation BUDDY SYSTEM 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

MEMORY ALLOCATIONMAP 500 ---> Divided 125 ---> Allocated 100

125 ---> Free

BUDDY SYSTEM 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 :

250 ---> Divided

250 ---> Free

AOS record

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. Sleeping Barber The barber shop has one barber, one barber chair, and ā€˜nā€™ chairs for waiting customers, if any to sit on. If there are no customers present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while the barber is cutting a customer's hair, they either sit down (if there are empty chairs) or leave the shop (if all chairs are full). The problem is to program the barber and the customers without getting into race conditions. This problem is similar to various queuing situations, such as a multi-person helpdesk with a computerized call waiting system for holding a limited number of incoming calls. Our solution uses three semaphores, customers, which counts waiting customers (excluding the customer in the barber chair, who is not waiting), barbers, the number of barbers (0 or 1) who are idle, waiting for customers, and mutex, which is used for mutual exclusion. We also need a variable, waiting, which also counts the waiting customers. The reason for having waiting is that there is no way to read the current value of a semaphore. In this solution, a customer entering the shop has to count the number of waiting customers. If it is less than the number of chairs, he stays; otherwise, he leaves. When the barber shows up for work in the morning, he executes the procedure barber, causing him to block on the semaphore customers because it is initially 0. The barber then goes to sleep. He stays asleep until the first customer shows up. When a customer arrives, he executes customer, starting by acquiring mutex to enter a critical region. If another customer enters shortly thereafter, the second one will not be able to do anything until the first one has released mutex. The customer then checks to see if the number of waiting customers is less than the number of chairs. If not, he releases mutex and leaves without a haircut. If there is an available chair, the customer increments the integer variable, waiting. Then he does an Up on the semaphore customers, thus waking up the barber. At this point, the customer and the barber are

AOS record

both awake. When the customer releases mutex, the barber grabs it, does some housekeeping, and begins the haircut. When the haircut is over, the customer exits the procedure and leaves the shop. The barber loops, however, to try to get the next customer. If one is present, a haircut is given. If not, the barber goes to sleep. As an aside, it is worth pointing out that although the readers and writers and sleeping barber problems do not involve data transfer, they still belong to the area of IPC because they involve synchronization between multiple processes.

AOS record

import java.util.concurrent.*; public class SleepingBarber extends Thread { public static Semaphore customers = new Semaphore(0); public static Semaphore barber = new Semaphore(0); public static Semaphore accessSeats = new Semaphore(1); /* the number of chairs in this barbershop is 5. */ public static final int CHAIRS = 5; public static int numberOfFreeSeats = CHAIRS; /* THE CUSTOMER THREAD */ class Customer extends Thread { int iD; boolean notCut = true; /* Constructor for the Customer */ public Customer(int i) { iD = i; } public void run() { while (notCut) { // as long as the customer is not cut try { accessSeats.acquire(); if (numberOfFreeSeats > 0) { System.out.println("Customer "+this.iD+" just sat down."); numberOfFreeSeats--; //sitting down on a chair customers.release(); //notify the barber that there is a customer accessSeats.release(); // don't need to lock the chairs anymore try

AOS record

{ barber.acquire(); notCut = false; this.get_haircut(); //cutting... } catch (InterruptedException ex) {} } else { // there are no free seats System.out.println("There are no free seats. Customer " + this.iD + " has left the barbershop."); accessSeats.release(); //release the lock on the seats notCut=false; } } catch (InterruptedException ex) {} } //while ends } // public void run() ends /* this method will simulate getting a hair-cut */ public void get_haircut() { System.out.println("Customer " + this.iD + " is getting his hair cut"); try { sleep(5050); } catch (InterruptedException ex) {} } } //customer class ends here

AOS record

/* THE BARBER THREAD */ class Barber extends Thread { public Barber() {} public void run() { while(true) { // runs in an infinite loop try { customers.acquire(); //tries to acquire a customer, if none available, he goes to sleep accessSeats.release(); // at this time he has been woken up numberOfFreeSeats++; // one chair gets free barber.release(); // the barber is ready to cut accessSeats.release(); // no need of lock on the chairs anymore this.cutHair(); //cutting... } catch (InterruptedException ex) {} } } /* this method will simulate cutting hair */

}

public void cutHair() { System.out.println("The barber is cutting hair"); try { sleep(5000); } catch (InterruptedException ex){ } } //barber class ends here

/* main method */

AOS record

public static void main(String args[]) { SleepingBarber barberShop = new SleepingBarber(); //Creates a new barbershop barberShop.start(); } public void run() { Barber b = new Barber(); b.start(); //Ready for another day of work /* This method will create new customers for a while */ for (int i=1; i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF