You are on page 1of 37
Assignment 1: Shell Programming #!bin/bash create() ( echo echo "Enter filename to create: " read filename if [ -£ $filename J then echo “File already Exist" else touch $filename echo "[*] File has been created!" fi echo , insert () ( echo echo “Enter the number of records to insert. read n for ({ isO;ic-$n-1;it+ )) do echo “Enter Info Like" echo "Roline Name EMailID ead a echo $a >> $filename done echo echo , ] Data Inserted! display () ( echo echo "[*] Address Book data is as follows" echo "Rolin Name EMailrD cat $filename echo , search() i echo echo "Enter keyword to search! read keyword (cat $filename | grep -w "Skeyword") -2 "Sout" ] echo "Records Not Found!" echo "Records Found" echo Sout city’ city" delete() ( echo echo "Enter record to delete" read recordtodelete cat $filename | grep -v "$recordtodelete" > file? ;mv file? $filename echo "Record has been deleted!" echo » modify () fi echo echo "Enter roll no to modify" read s echo echo read r sed -i "/Ss/c §r" $filename echo "Record is modified! echo , Name EMailID city" until [[ "Sch" do echo echo "[1] Create Address Book echo "[2] Insert data into Address Book! echo "[3] Display Address Book" echo "[4] Search record in Address Book" echo *[5] Delete a record” echo "[6] Modify a record” echo "(7] Exit echo echo “[*] Enter your choice ead ch case $ch in lj create; ; 2)insert;; 3) display. 4) search; ; 5)delete;; 6)modity:: Nes done Output: getty eI [1] create Address Book! [2] Insert data into Address Book! [3] Display Address Book [4] Search record in Address Book [5] Delete a record [6] Modify a record (71 exit [+] Enter your choice 1 Enter Filename to create: addressbook [+] File has been created! fay create address Books [2] insert dats inco aaaress Sook! [3] display address soak {4} Search recore in Aaaress sook {5} oetete a record Tey modify 2 Fe 0) ene [+] enter your choice Enter the rimber of records to insert Rolin awe east cay Fe wii many Pane Enter info Like Roti. nome etait city 39 Prasad pay Pane f+) bata ansertedt fa) create address soon: [2] Insert date into aaaress sookt (5) Display Address Book fe) Seater ecors tn address [5] Delete a record (6) moaity 2 record U7 eae foi eaeer your choice fo) aaaress took data ta as fottows Rolie. nine ‘a9 ay 50 Abhishek abassy Pune 35 Aika nkBsey Bone 29 Prasad psas-y Pune [1] Create Address Book! [2] Insert data into address Book! [3] Display Address Book [4] Search record in Address Book [5] Delete a record [6] modify a record O71 exit [+] Enter your choice 4 Enter keyword to search! 5 Records Found 15 Nikhil nkax.y Pune [1] Create Address Book! [2] Insert data into Address Book! 3] Display Address Book [4] Search record in Address Book [5] Delete a record [6] Modify a record (7) exit [+] Enter your choice 5 Enter record to delete 29 Record has been deleted! [1] Create Address Book! [2] Insert data into Address Book! [3] Display Address Book [4] Search record in Address Book [5] Delete a record [6] modify a record (7) exit [+] Enter your choice 6 Enter roll no to modify 15 Enter new record Roltno ‘Name emait1D city 6 Nikhil nkhlax.y Pune Record is modified! Assignment 3B : Round Robin fincludecstdio.h> void main() { inti, j, n, bu{10], wa[10], tat[ 10], t, ct[10], max; float awt = 0, att = 0, temp = 0; clrser(); printf("Enter the no of processes : "); scanf("96d", &n); for(i=0; ten; i++) { printf("\n€nter Burst Time for process %d : ", #1); scanf("96d", &bulil); t{i] = bulil; } printf("\nEnter the size of time slice :"); scanf("%d", &t); max = bul]; for(i=1; icn; i++) if{max < bull) max = buli]; for(j=0; j<(max/t}+1; j++) forli=0; ier; H+) if{buli != 0) if(bufi] <= t) { tatli] = temp + bulil; temp = temp + buf; bufi] = 0; 1 else { buf] = bul; temp =temp +t; for(i=0; ten; t+) { wali] = tatfi] - ctl att += tat[i]; awt += wali]; printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\TURNAROUND TIME\n"); forli=0; i int mainO { int AT[10], BT[10], temp[10]; int i, small, cnt = 0, t, limit; double wait_time = 0, turnaround_time = 0, end; float avg_wt, avg_tat; printi(’\nEnter the (otal number of Processor: "); scanf("%d", &limit); printi("\nEnter Details of %d Processes\n", limit); i+Dy printf("Enter Burst Time of P%d : ", i+1); scanf("%d", &BTLi]); templi] = BT[i]; } // Printing Table int j; printf("\nProcess Arrival Time Burst Time\n"); for(j = 0; j < limit; j++) { printf(" P%d = %d %d_ \n", j+1, ATU). BTLD: } // shortest Job First Algorithm 999; cnt != limit; t+) { < limit; +4) ( if(AT[i] <= t && BT[i] < BT[small] && BT[i] > 0) { small = i; } } BT[small]--; if(BT [small] == 0) { cnt++; end=t+ 1; wait_time = wait_time + end - AT[small] - temp[small]; turnaround_time = turnaround_time + end - AT[small]; J } // Printing Average TAT and WT avg_wt = wait_time / limit; avg_tat = turnaround_time / limit; Average Waiting Time: %If", avg_wt) printf("\nAverage Turnaround Time: If \n", avg_tat); return 0; } Output: Prete ee eee cM ae ee aed Peta e eM Arrival Time of P1: 1 COG et ane ears Cae eC ane ay Burst Time of P3 eC ane! Burst Time of P4 eC ane aa ae eC rocess Arrival Time Burst Time iat 18 cz re) I cy verage Waiting Time: 5.000000 Atte a MG ee COMP) Assignment 2A: Process Control System Calls (Zombie Process) #include #include #include void bubblesort(int al], int n); void selectionsort{int all, int n); int main() { int af10], n, i pid_t id; printf("Enter the number of elements seanf("%d", &n); printf("\nEnter the elements :"); forli=0; icn; i++) scanf("%d", 8a); id = fork(); switchtid) { case -1 : printf("\nError in Fork"); break; ": case 0 : printf("\nChild process ID : %d",getpid()); printf("\nMy Parent Process ID : %d",getppid()); bubblesort(a,n); printf("\nl am Zombie. break; default : printf("\nParent process ID : %d",getpid()); selectionsort(a,n}; printf("\n"); sleep(3); } return 0; void bubblesort(int af], int n) { inti, j, temp; for(i=O; i aGa)){ temp = alj); ali] = alj+4}; alj+1] = temp; } printf("\nAscending order :"); for(i=0; icn; i+) printf("%d ",alil); void selectionsort(int al], int n) { int i,j, min, temp; forli: in; i++) { for(j=it4; jen; j++) { if(ali] > almin]) { min = j; } temp = ali); ali] = almin]; a{min] = temp; } printf("\nDescending order : "); for(i=0; icn; i++) printf("%d ", ali); Output: ent process ID : SO [arc oe Assignment 2C: Process Control System Calls (BinarySearch) ftinclude include include Hinclude void bubblesort(int al] int n); pid_t id; void main() { int a[10], n, i; char *argv(12]; forli=0; i<12; i++) { argv[i] = (char *) malloc(20); } printf("\n\nEnter the number of elements in the array: "}; scant("%d", &n}; printf("Enter the elements: "); forli=0; i alj+4) ¢ temp = alj]; alj+1] = temp; } printf("\n"); BinarySearch.c Hinclude include Hinclude include Hinclude void binarysearch(long int [10], int n); void main(int argc, char *arev[],char *envpl]) { inti, n=0; long af10]; for(i=1; arguil!=NULL; i++) { ali] = strtol(argv{i], NULL, 10); nt; } printf("\n"); printf("\nin second child process. Process id is : %d", getpidl)); printf("\nMy parent's process id is : %d", getppid)); binarysearch(a,n); void binarysearch(long int a[10] int n) { inti=0,j=n-1, mid, =-1, temp; printf("\nEnter the number to search scanf("séd", &temp); printf("\nin Binary Search Program\n"); while(i<=j && a[mid]!=temp) { mid = (i+j) >> 1; if{temp == almid]) ¢= 1; if(temp > almid]) i= mid+1; else j= mid-1; } iffe A) printf("The given number %d is not found." temp)) else printf("The given number %d is found " temp); } Output: pc-0S@pc05-optiPlex-3090 as er Tee sata Pear) SE ee sate ee lee ey Ce Ee nter the elements: 2 8 46 3 2 cess Sec Ree crs) Sa este CR eC er Eco Re eCR eraee SC cee Pr eae e eer t) Caer accu ee ror iat? Assignment 4A : Producer Consumer #include #include #include #include #include #include void *consumer(); void *producer() pthread_t p, ¢; sem_t empty, full; pthread_mutex_t mutex; pthread_attr_t attr; int counter = 0; int buffer{5); initialize all variables void init() { pthread_attr_init(attr) sem_init(&full, 0, 0); sem_init(&empty, 0, 5); pthread_mutex_init(&mutex, NULL); i int main() { int np,nc,i; init; printf("Enter no of producers scant("%d", &np); printf("Enter no of consumers : "); scanf("%d", &nc); for(i=0; isnp; i++) { pthread_create(&p, &attr, producer, NULL); i for(i=0; ine; i++) { pthread_create(&c, &attr, consumer, NULL); } for(i=0; i=5) printf("buffer full..Errorin"); sem_wait(&empty); pthread_mutex_lock(&mutex); if(counter<5) { buffer{counter] = item; counter++ printf("Producer produced %d\n", item) } pthread_mutex_unlock(&mutex) sem_post(&{ull); sleep(1); pthread_exit(0); void *consumer() { } int item: printf("buffer empty. Errorin"); return 0; } sem_wait(8ull); pthread_mutex_lock(&mutex); if(counter>0) { item = buffer{counter-1); counter-- printf("Consumer consumed %d\n", item); } pthread_mutex_unlock(&mutex), sem_post(&empty); sleep(1); pthread_exit(0) OUTPUT lpc-27@pc27-Veriton-M200-A58:~/D of producers : 8 of consumers : 8 produced 83 Ate et produced 77 produced 15 produced 93 ca lle fete eu e) produced oT LAs) produced Memeo) ETL mee ele) Sem ee eee te) See tee) buffer empty. .Erro Producer produced 92 Carn) Cnr) Cee) WAInS Assignment 2B: Process Control System Calls (Orphan Process) Hinclude Hinclude Hincludesunistd.h> void bubblesort(int al], int n); void selectionsort(int al], int n); int main() { int a[20}, n, i pid_tid; printf("Enter the number of elements : "); scanf("séd", &n); print{("\nEnter the elements :"); forli=0; cn; i++) scanf("%d", Bali); id= fork(); switch(id) { case -1:: printf("\nError in Fork"); break; case 0: printf("\nChild process ID : %d",getpid\)}; printf("\nMy Parent Process ID : %d",getppial); bubblesort(a, n); printf("\n"); sleep(5); printf(*\nl am Orphan."); printf("\nMy parent ID is : %d",getppid()); break; default : printi("\nParent process ID : %d",getpid()); selectionsort(a, n); printf("\n"); sleep(2); break; } return 0; void bubblesort(int af], int n) { int, j, temp; for{i=0; fen; i++) { for(j=0; j ali+4]) { temp = alj]; ali] = alj+1]; alj+1] = temp; } printf("\nAscending order forli=0; in; i++) printf("%d ", ali); void selectionsort(int al}, int n) { inti, j, min, temp; for(i=0; in 4) { min =i; for(i=it]; jn; j++) { iflali] > almin}) min=j; } temp = ail; ali] = almin]; almin] = temp; printf("\nDescending order :"); forli=O;icn;i++) printf("94d ", alil); } Output: Gems CMT) hea Tran eter OO Roe Assignment 4B : Reader Writer #include Hinclude #include #include pthread_mutex_t x, wsem; pthread_tr, ws int readcount; int writecount; void intialize() { pthread_mutex_init(x, NULL); pthread_mutex_init(&wsem, NULL); readcount writecount } void* reader(void* param) { int waittime; waittime = rand() % 5; printf("\nReader is trying to enter"); pthread_mutex_lock(&x); readcount++; if(readcount == 1) pthread_mutex_lock(wsem); printf("\n\t%d Reader is inside.. ", readcount); pthread_mutex_unlock(&x); sleep(waittime); pthread_mutex_lock(&x); pthread_mutex_unlock(&wsem); pthread_mutex_unlock(&x); printf("\n\t\t%d Reader is Leaving..", readcount+1); } void* writer(void* param) { int waittime; waittime = rand() % 3; printf("\nWriter is trying to enter.."); writecount++; pthread_mutex_lock(&wsem); f("\n%d Writer has entered”, writecount); sleep(waittime); pthread_mutex_unlock(&wsem); writecount--; print{("\n\t%d Writer is leaving..", writecount+1); sleep(5);, exit(0); } int main() { int n1,n2,i; printf("\n Reader/Writer Problem"); printf("\nEnter the no of readers: "); scanf("%d", Sl); printf(""\nEnter the no of writers: "); scanf("%d", &n2); for(i=0; i #include // Display Output void finalOutput(int arr{J[10}, int n, int m) { inti, js for(i=; icn; i++) { printf("\n"); for(j=0; j WIO]TD) £2 = 15 ) if(f2 == 0 && Fi for(j FLil=1; fl++; ot Jsm; j++) WIOIIj] += ATL]; return 0; } 1/ Resource Request Algorithm int requestAlgo(int A[10}{10}, int N[10]{10}, int B[1OJ[10}, int pid, int K) { int rmat{1][10}; int i; printf("\nEnter additional request : \n"); for(i=0; i NIpidLiD) { printf("\n***"***Error encountered******\n"); exit(0); K; i++) { if(emat[O][i] > BLOM) { printf("\n**"""*Resources unavailable*****\n"); exit(0); )s ISK; i++) { ‘B(O] [i] -= rmat[O]{i); Alpid]li] += nmat{Ollil; N(pid]{i] -= rmat(O][i]; } int banker(int A{][20}, int N(J(20), int W{2](0}, int n, int m) { int j, i, af 10]; j = safetyAlgo(A, N, W, n, m, a); ifG != 0) { printf("\n\n"); printi("\nA safety sequence has been "" detected.\n"); fort ++) printf("P%d", ali); printf("\n"); return 1; } else { printf(""\nDeadlock has occured.\n"); return 0; i } int main() { int AN[10]{10], Max{10][10], Need[10][10}, W[1][10]; int n, m, pid, cr; printf("\n *********DEADLOCK AVOIDANCE USING""BANKER'S ALGORITHM**"####9\3"): bankerAlgo(All, Need, Max, W, &n, &m); r= banker(All, Need, W, n, m); if(e = 0) { printi("\nDo you want make an additional request for any of the process ? (1=Yes| 0=No)"); scanf("%d", 8c); if(c == 1) { printf("\nEnter process number : scanf("%d", &pid); requestAlgo(All, Need, W, pid -1, m); inker(All, Need, W, n, m); Ot exit(0); } } } else exit(0); return 0; a Cer ery aerse iheledabebateletel 12.01 \ Ue) OU (a RUD (eT dt MCV RCO);S lalallala a eo) ol ae he ee eee Allocation for resource 1 Maximum for resource 1: 7 ale aCe ead Maximum for resource 2: 5 COR eae el Maximum for resource 3 : 3 eee Ole ate eee Maximum for resource 1 : 3 COR Te tate ee tt) ed Maximum for resource 2 : 2 rl aCe ed aU ce ln a ed eee) (NOR a ee ee Maximum for resource 1: 9 re aC ee ed Maximum for resource 2 : 0 Ole at ee ead Maximum for resource 3 : 2 eee! Allocation for resource 1 : Maximum for resource 1 : 2 POR Te tae ee) ed aU ieee lk ed a aCe eed Cea Cel ed ela ey Allocation for resource 1: 0 Maximum for resource 1: 4 Allocation for resource 2 : 0 et Ll Allocation for resource 3 : 2 Dea tT eee tl Available resources : Resource 1 : 3 Gel kel a) Resource 3 : 2 Pererrer eA SEPT CM ta heeee etree ri C 0 C) A C elebeiebebete CSUR Ge uC a La © Slededeteieietalel 5 Fa to) Fa Fy prretrrrer rere aceet tet steed A safety sequence has been detected. P1 P3 P4 PO P2 COS eC Sas Cue ens CORR USC RE eau Ue ee ee ee CLO ET CN ete ee) Caer ea ae seca gee Request for resource 2 : Coen ge ed babel tl Ella Assignment 6 : Page Replacement Algorithms #include int n, nf, in[100}, p[SO], hit = 0; int, j, k, pgfauleent void getData() { printf("\nEnter length of page reference sequence: "); scanf("%d", &n); printf("\nEnter the page reference sequence: "); for(i=0; i max) { max = near{j); repindex } } plrepindex] = in{i); pgfaultcnt+; disPages(); ) else printf("Page hit"); } dispPgFaultCnt(); // Least Recently Used (LRU) Algorithm void Iru) { init(); int least[50); for(i=0; i

You might also like