├── .DS_Store ├── Algorithms Lab ├── Week 1 │ ├── adjacency_list1.c │ ├── prg1 │ ├── prg1.c │ ├── prg2 │ └── prg2.c ├── Week 10 │ ├── p1.c │ ├── p2.c │ ├── p3.c │ └── readme.md ├── Week 11 │ └── huffman.c ├── Week 12 │ ├── p1.c │ ├── p2.c │ └── readme.md ├── Week 2 │ ├── gcd_middle_school.c │ ├── p1.c │ ├── p2_efficient │ ├── p2_efficient.c │ └── readme.md ├── Week 3 │ ├── p1.c │ ├── p2.c │ ├── partition.c │ ├── prg3 │ ├── prg3.c │ ├── prg3_eff │ ├── prg3_eff.c │ ├── readme.md │ ├── selectionsort │ └── selectionsort.c ├── Week 4 │ ├── AssignmentBrute.c │ ├── assignment2.c │ ├── p2.c │ ├── p3.c │ └── readme.md ├── Week 5 │ ├── p1_1.c │ ├── p1_2.c │ ├── p2.c │ └── readme.md ├── Week 6 │ ├── prg1.c │ ├── prg2.c │ └── prg3.c ├── Week 7 │ ├── avl_final.c │ ├── prg2 │ ├── prg2.c │ └── readme.md ├── Week 8 │ ├── p1.c │ ├── p2.c │ └── readme.md ├── Week 9 │ ├── p1.c │ ├── p2.c │ ├── p3.c │ └── readme.md ├── algorithm lab three labs..pdf └── readme.md ├── Books ├── CA │ ├── (CS6143) Computer Architecture - A Quantitative Approach 5e.pdf │ └── Computer Organisation and Architecture 8e by William Stallings.pdf ├── DC │ ├── (9th Edition) (William Stallings Books on Computer and Data Communications) William Stallings-Computer Organization and Architecture-Prentice Hall (2012).pdf │ └── Data Communications and Networking By Behrouz A.Forouzan.pdf ├── OS │ └── Operating_System_Concepts,_8th_Edition[A4].pdf └── SE │ ├── RPL-7th_ed_software_engineering_a_practitioners_approach_by_roger_s._pressman_.pdf │ └── fundamentals-of-software-engineering-fourth-edition-rajib-mall.pdf ├── CN Lab ├── Lab No 1.pdf ├── Week 1 │ ├── filecopy.c │ └── readme.md ├── Week 10 │ ├── README.md │ ├── q1.tcl │ ├── q2.tcl │ └── q3.tcl ├── Week 11 │ ├── addn_p.tcl │ ├── l11qadd.awk │ ├── p1.awk │ ├── p1.tcl │ └── readme.md ├── Week 2 │ ├── Example │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c │ ├── Q1 │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c │ ├── Q2 │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c │ └── Q3 │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c ├── Week 3 │ ├── Example │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c │ ├── Q1 │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c │ ├── Q2 │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c │ └── Q3 │ │ ├── client │ │ ├── client.c │ │ ├── server │ │ └── server.c ├── Week 4 │ ├── Q1 │ │ ├── README.md │ │ ├── client.c │ │ └── server.c │ └── README.md ├── Week 5 │ ├── p1_c.c │ ├── p1_s.c │ ├── p2_c.c │ ├── p2_s.c │ └── readme.md └── readme.md ├── OS Lab ├── OS Lab Manual 2017_modified.pdf ├── OS Lab Manual 2017_modified.docx ├── Week 1 │ ├── grep │ ├── grep.c │ └── script1.sh ├── Week 10 │ ├── p1.c │ ├── p2.c │ └── readme.md ├── Week 11 │ ├── p1_1.c │ ├── p1_2.c │ ├── p1_3.c │ └── readme.md ├── Week 12 │ ├── p1_1.c │ ├── p1_2.c │ ├── p1_3.c │ ├── p1_4.c │ └── readme.md ├── Week 2 │ ├── q1.sh │ ├── q2.sh │ ├── q3.sh │ ├── q4.sh │ ├── q5.sh │ └── q6.sh ├── Week 3 │ ├── p1.sh │ ├── p2.sh │ ├── p3.sh │ ├── p4.sh │ └── readme.md ├── Week 4 │ ├── copyfile │ ├── newfile │ ├── orphan │ ├── orphan.c │ ├── prg1 │ ├── prg1.c │ ├── prg2 │ ├── prg2.c │ ├── prg3 │ ├── prg3.c │ ├── prg4 │ ├── prg4.c │ ├── prg5 │ ├── prg5.c │ ├── prg6 │ └── prg6.c ├── Week 5 │ ├── README.md │ ├── p1.c │ ├── p2.c │ ├── p3.c │ ├── q1.c │ ├── q2.c │ └── q3.c ├── Week 6 │ ├── p1_1.c │ ├── p1_2.c │ ├── p1_3.c │ ├── p1_4.c │ └── readme.md ├── Week 7 │ ├── p1.c │ ├── p3.c │ ├── q2.c │ ├── q2r.c │ └── readme.md ├── Week 8 │ ├── p.c │ └── readme.md ├── Week 9 │ ├── p1.c │ ├── prg2.c │ └── readme.md └── readme.md └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/.DS_Store -------------------------------------------------------------------------------- /Algorithms Lab/Week 1/adjacency_list1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct node 5 | { 6 | int val; 7 | struct node * next; 8 | }NODE; 9 | int main() 10 | { 11 | int v,e1,e2; 12 | int i,j; 13 | NODE ** graph; 14 | scanf("%d",&v); 15 | int adj_matrix[v][v]; 16 | graph = (NODE **) calloc(v+1,sizeof(NODE *)); 17 | for(i=0;ival=i+1; 21 | graph[i]->next=NULL; 22 | } 23 | int ch; 24 | while(1) 25 | { 26 | scanf("%d",&e1); 27 | scanf("%d",&e2); 28 | adj_matrix[e1-1][e2-1]=1; 29 | NODE * head = graph[e1-1]; 30 | while(head->next!=NULL) 31 | { 32 | head=head->next; 33 | } 34 | NODE *n = (NODE *) malloc(sizeof(NODE)); 35 | n->val=e2; 36 | head->next=n; 37 | n->next=NULL; 38 | 39 | head = graph[e2-1]; 40 | while(head->next!=NULL) 41 | head=head->next; 42 | 43 | NODE * n2 = (NODE *) malloc(sizeof(NODE)); 44 | n2->val = e1; 45 | head->next=n2; 46 | n2->next=NULL; 47 | 48 | printf("Enter more?\n"); 49 | scanf("%d",&ch); 50 | if(ch==0) 51 | break; 52 | } 53 | 54 | for(i=0;i",head->val); 65 | head=head->next; 66 | } 67 | printf("NULL\n"); 68 | } 69 | 70 | for(i=0;i 2 | #include 3 | 4 | typedef struct node{ 5 | int val; 6 | struct node * left; 7 | struct node * right; 8 | } * Node; 9 | 10 | Node add (Node root,int a) 11 | { 12 | 13 | Node cur=root; 14 | Node prev=cur; 15 | while(cur) 16 | { 17 | prev=cur; 18 | if(a>cur->val) 19 | cur=cur->right; 20 | else 21 | cur=cur->left; 22 | } 23 | Node temp = malloc(sizeof(struct node)); 24 | temp->val =a; 25 | temp->left =NULL; 26 | temp->right =NULL; 27 | if(!root) 28 | return temp; 29 | if(a>prev->val) 30 | prev->right=temp; 31 | else 32 | prev->left=temp; 33 | return root; 34 | } 35 | 36 | void inorder(Node root) 37 | { 38 | if(!root) 39 | return; 40 | inorder(root->left); 41 | printf("%d ",root->val); 42 | inorder(root->right); 43 | } 44 | 45 | void preorder(Node root) 46 | { 47 | if(!root) 48 | return; 49 | printf("%d ",root->val); 50 | preorder(root->left); 51 | preorder(root->right); 52 | } 53 | 54 | void postorder(Node root) 55 | { 56 | if(!root) 57 | return; 58 | postorder(root->left); 59 | postorder(root->right); 60 | printf("%d ",root->val); 61 | } 62 | 63 | 64 | Node insert(Node root) 65 | { 66 | int n,a; 67 | printf("%s","Enter number of nodes: " ); 68 | scanf("%d",&n); 69 | printf("%s", "Nodes: "); 70 | for(int i=0;ival) 87 | {printf("%s\n", "Key Found"); 88 | return;} 89 | else if(a>cur->val) 90 | cur=cur->right; 91 | else 92 | cur=cur->left; 93 | } 94 | printf("%s\n", "Not Found, but inserted"); 95 | Node temp = malloc(sizeof(struct node)); 96 | temp->val =a; 97 | temp->left =NULL; 98 | temp->right =NULL; 99 | if(a>prev->val) 100 | prev->right=temp; 101 | else 102 | prev->left=temp; 103 | return; 104 | } 105 | 106 | 107 | void main() 108 | { 109 | Node root=NULL; 110 | int choice; 111 | 112 | while(1) 113 | {printf("%s","\n\n1. Insert\n2. Search\n3. Inorder\n4. Preorder\n5. Postorder\nEnter choice (1-5) : " ); 114 | scanf("%d",&choice); 115 | switch(choice) 116 | { 117 | case 1:root=insert(root); 118 | break; 119 | case 2:search(root); 120 | break; 121 | case 3:inorder(root); 122 | break; 123 | case 4:preorder(root); 124 | break; 125 | case 5:postorder(root); 126 | break; 127 | default: exit(0); 128 | } 129 | } 130 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 1/prg2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Algorithms Lab/Week 1/prg2 -------------------------------------------------------------------------------- /Algorithms Lab/Week 1/prg2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct node{ 5 | char val; 6 | struct node * next; 7 | }* Node; 8 | 9 | 10 | typedef struct graph{ 11 | int v; 12 | Node * array; 13 | } Graph; 14 | 15 | 16 | Graph addEdge(Graph graph,char src, char dst) 17 | { 18 | 19 | Node cur=graph.array[src-'A']; 20 | while(cur->next) 21 | cur=cur->next; 22 | cur->next=(Node) malloc(sizeof(struct node)); 23 | cur=cur->next; 24 | cur->val=dst; 25 | cur->next=NULL; 26 | cur=graph.array[dst-'A']; 27 | while(cur->next) 28 | cur=cur->next; 29 | cur->next=(Node) malloc(sizeof(struct node)); 30 | cur=cur->next; 31 | cur->val=src; 32 | cur->next=NULL; 33 | return graph; 34 | } 35 | 36 | Graph createGraph() 37 | { 38 | Graph graph; 39 | int v,e; 40 | printf("%s", "Enter number of vertices: "); 41 | scanf("%d",&v); 42 | graph.v=v; 43 | graph.array= (Node *) calloc(v,sizeof(Node)); 44 | printf("%s", "Enter number of edges: "); 45 | scanf("%d",&e); 46 | for(int i=0;ival= 'A'+i; 50 | graph.array[i]->next=NULL; 51 | } 52 | for(int i=0;inext) 71 | {printf("%c-->",cur->val); 72 | cur=cur->next;} 73 | printf("%c\n",cur->val ); 74 | } 75 | } 76 | 77 | int ** createMatrix(Graph graph) 78 | { 79 | int ** matrix = (int **) calloc(graph.v,sizeof(int *)); 80 | for(int i=0;inext) 86 | {cur=cur->next; 87 | matrix[i][cur->val-'A']=1;} 88 | } 89 | return matrix; 90 | } 91 | 92 | void displayMatrix(int ** matrix,int v) 93 | { 94 | printf("%s\n","Adjacency Matrix"); 95 | for(int i=0;i 2 | #include 3 | 4 | void main () 5 | { 6 | int i, j, k, n, opc = 0; 7 | int a[20][20], b[20][20]; 8 | printf( "\nEnter number of vertices" ); 9 | scanf( "%d",&n ); 10 | printf("\nEnter Adjacency Graph" ); 11 | for ( i = 0; i < n; i++ ) 12 | { 13 | for ( j = 0; j < n; j++ ) 14 | { 15 | scanf( "%d", &a[i][j] ); 16 | b[i][j] = a[i][j]; 17 | } 18 | } 19 | for ( k = 0; k < n; k++ ) 20 | { 21 | for ( i = 0; i < n; i++ ) 22 | { 23 | for ( j = 0; j < n; j++ ) 24 | { 25 | if ( b[i][j] == 0 ) 26 | { 27 | opc++; 28 | b[i][j] = b[i][j] || ( b[i][k] && b[k][j] ); 29 | } 30 | } 31 | } 32 | } 33 | printf("\n The resultant matrix is :\n" ); 34 | for ( i = 0; i < n; i++ ) 35 | { 36 | for ( j = 0; j < n; j++ ) 37 | { 38 | printf( "%d\t", b[i][j] ); 39 | } 40 | printf( "\n" ); 41 | } 42 | printf("\nOperation Count : %d", opc ); 43 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 10/p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int min ( int a, int b ) 5 | { 6 | return a >= b ? b : a; 7 | } 8 | void main () 9 | { 10 | int i, j, k, n, opc = 0; 11 | int a[20][20], b[20][20]; 12 | printf( "\nEnter number of vertices" ); 13 | scanf( "%d",&n ); 14 | printf("\nEnter Weight Matrix( 999 for INFINITY )" ); 15 | for ( i = 0; i < n; i++ ) 16 | { 17 | for ( j = 0; j < n; j++ ) 18 | { 19 | scanf( "%d", &a[i][j] ); 20 | b[i][j] = a[i][j]; 21 | } 22 | } 23 | for ( k = 0; k < n; k++ ) 24 | { 25 | for ( i = 0; i < n; i++ ) 26 | { 27 | for ( j = 0; j < n; j++ ) 28 | { 29 | opc++; 30 | b[i][j] = min( b[i][j], ( b[i][k] + b[k][j] ) ); 31 | 32 | } 33 | } 34 | } 35 | printf("\n The resultant matrix is :\n" ); 36 | for ( i = 0; i < n; i++ ) 37 | { 38 | for ( j = 0; j < n; j++ ) 39 | { 40 | printf( "%d\t", b[i][j] ); 41 | } 42 | printf( "\n" ); 43 | } 44 | printf( "\n Operation Count : %d", opc ); 45 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 10/p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int opc; 5 | int max ( int a, int b ) 6 | { 7 | return a >= b ? a : b; 8 | } 9 | 10 | 11 | int knap_sack ( int cap, int w[], int val[], int n ) 12 | { 13 | opc++; 14 | if ( n == 0 || cap == 0 ) 15 | { 16 | return 0; 17 | } 18 | else if ( w[n - 1] > cap ) 19 | { 20 | return knap_sack ( cap, w, val, n - 1 ); 21 | } 22 | else 23 | { 24 | return max( val[n - 1] + knap_sack ( cap - w[n - 1], w, val, n - 1 ), knap_sack ( cap, w, val, n - 1) ); 25 | } 26 | } 27 | 28 | void main () 29 | { 30 | int n, w[20], val[20], cap, i; 31 | printf("\n Enter no of items " ); 32 | scanf( "%d", &n ); 33 | printf("\n Enter Knapsack capacity " ); 34 | scanf( "%d", &cap ); 35 | printf("\n Enter weight and value for each item " ); 36 | for ( i = 0; i < n; i++ ) 37 | { 38 | scanf( "%d", &w[i] ); 39 | scanf( "%d", &val[i] ); 40 | } 41 | printf("\nThe optimum solution is : %d ", knap_sack (cap, w, val, n ) ); 42 | printf( "\nOperation Count : %d", opc ); 43 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 10/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 10. 2 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 11/huffman.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | char data; 6 | float val; 7 | struct node * left; 8 | struct node *right; 9 | }; 10 | 11 | int find_min(struct node *a[],int n){ 12 | int pos; 13 | float min=100.0; 14 | for(int i=(n+1)/2-1;ivalval; 18 | } 19 | } 20 | printf(" min index%d\n", pos); 21 | return pos; 22 | } 23 | 24 | void get_codes(struct node *root,char *p,int *i){ 25 | if(root){ 26 | p[(*i)++]='0'; 27 | get_codes(root->left,p,i) ; 28 | (*i)--; 29 | if(root->data!= ' '){ 30 | printf(" %c ",root->data ); 31 | for(int x=0;x<*i;x++){ 32 | printf("%c",p[x] ); 33 | } 34 | printf("\n"); 35 | 36 | return; 37 | } 38 | p[(*i)++]='1'; 39 | get_codes(root->right,p,i); 40 | (*i)--; 41 | } 42 | } 43 | 44 | void levelorder(struct node *root){ 45 | struct node *Q[30]; 46 | int f=0; 47 | int r=0; 48 | Q[f]=root; 49 | Q[r]=root; 50 | Q[++r]=NULL; 51 | struct node *temp; 52 | while(f<=r){ 53 | temp = Q[f++]; 54 | if(temp==NULL){ 55 | if(f%f ",temp->data,temp->val); 63 | if(temp->left){ 64 | Q[++r]=temp->left; 65 | } 66 | if(temp->right){ 67 | Q[++r]=temp->right; 68 | } 69 | } 70 | } 71 | } 72 | 73 | void heapify(struct node *a[],int n,int i){ 74 | int l=2*i+1; 75 | int r=2*i+2; 76 | int m=i; 77 | if(lval>a[m]->val) 78 | m=l; 79 | if(rval>a[m]->val) 80 | m=r; 81 | if(m!=i){ 82 | struct node *t=a[i]; 83 | a[i]=a[m]; 84 | a[m]=t; 85 | heapify(a,n,m); 86 | } 87 | } 88 | 89 | void makeheap(struct node *a[],int n){ 90 | for(int i=n/2-1;i>=0;i--) 91 | heapify(a,n,i); 92 | for(int i=0;ival,a[i]->data ); 94 | } 95 | printf("\n"); 96 | } 97 | 98 | struct node * generate_tree(struct node *a[],int n){ 99 | makeheap(a,n); 100 | 101 | while(n>0){ 102 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 103 | /* temp->left = a[n-1]; 104 | temp->right = a[n-2]; 105 | a[n-1]=NULL; 106 | a[n-2]=NULL; 107 | n--;*/ 108 | int pos = find_min(a,n); 109 | struct node *abc = a[pos]; 110 | a[pos]=a[n-1]; 111 | a[n-1] = abc; 112 | temp->left = a[n-1]; 113 | a[n-1]=NULL; 114 | abc=NULL; 115 | n--; 116 | if(n==0){ 117 | break; 118 | } 119 | makeheap(a,n); 120 | 121 | //n--; 122 | pos = find_min(a,n); 123 | abc = a[pos]; 124 | a[pos]=a[n-1]; 125 | a[n-1] = abc; 126 | temp->right = a[n-1]; 127 | a[n-1]=NULL; 128 | n--; 129 | abc = NULL; 130 | 131 | n++; 132 | temp->val = temp->left->val + temp->right->val; 133 | temp->data = ' '; 134 | a[n-1] = temp; 135 | //levelorder(temp); 136 | char *p = (char *)malloc(sizeof(char)*10); 137 | int i=0; 138 | get_codes(temp,p,&i); 139 | free(p); 140 | 141 | printf("\n"); 142 | makeheap(a,n); 143 | } 144 | return a[0]; 145 | } 146 | 147 | int main(){ 148 | printf("enter no of elements\n"); 149 | int n; 150 | scanf("%d",&n); 151 | struct node *a[10]; 152 | for(int i=0;ileft=NULL; 155 | a[i]->right=NULL; 156 | } 157 | char ch; 158 | float x; 159 | for(int i=0;idata=ch; 164 | a[i]->val=x; 165 | } 166 | 167 | for(int i=0;idata,a[i]->val ); 169 | } 170 | struct node *y=NULL; 171 | y = generate_tree(a,n); 172 | printf("\n\n"); 173 | levelorder(y); 174 | } 175 | 176 | //Contributed by Dattatreya Nayak 177 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 12/p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int n; 5 | int findSubSet(int array[], int subset[], int sum, int index, int subSetIndex) { 6 | if (index == n) { 7 | return 0; 8 | } 9 | 10 | if (array[index] == sum) { 11 | subset[subSetIndex] = array[index]; 12 | return 1; 13 | } 14 | 15 | for (int i = index; i < n; i++) { 16 | if (array[i] > sum) { 17 | continue; 18 | } 19 | 20 | if (array[i] == sum) { 21 | subset[subSetIndex] = array[i]; 22 | return 1; 23 | } 24 | 25 | subset[subSetIndex] = array[i]; 26 | if (findSubSet(array, subset, sum - array[i], i + 1, subSetIndex + 1)) { 27 | return 1; 28 | } 29 | 30 | } 31 | return 0; 32 | } 33 | void findSumSubSet(int array[], int sum) { 34 | 35 | int subset[100] ; 36 | 37 | int isSubSetFound = findSubSet(array, subset, sum, 0, 0); 38 | if (isSubSetFound) { 39 | printf("\nSum subset for :%d\n",sum); 40 | 41 | for (int temp = 0, i = 0; temp != sum; temp += subset[i], i++) { 42 | printf("%d ->",subset[i]); 43 | } 44 | printf("\n"); 45 | } else { 46 | printf("\nSubset not found for sum : %d\n",sum); 47 | } 48 | } 49 | 50 | 51 | int main() { 52 | int array[] = {1 ,2 ,5 ,6 ,8}; 53 | n=5; 54 | findSumSubSet(array, 9); 55 | } 56 | 57 | 58 | //Contributed by Ajay Hegde -------------------------------------------------------------------------------- /Algorithms Lab/Week 12/p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define max 10 4 | 5 | int w[max],i,j,p[max]; 6 | int n,m; 7 | float unit[max]; 8 | int y[max],x[max],fp=-1,fw; 9 | void get() 10 | { 11 | printf("\n Enter total number of items: "); 12 | scanf("%d",&n); 13 | printf("\n Enter the Maximum capacity of the Sack: "); 14 | scanf("%d",&m); 15 | for(i=0;i fp) && ( k == n)) 90 | { 91 | fp = cp+p[k]; 92 | fw = cw+w[k]; 93 | for(j=0;j<=k;j++) 94 | x[j] = y[j]; 95 | } 96 | } 97 | if(bound(cp,cw,k) >= fp) 98 | { 99 | y[k] = 0; 100 | if( k <= n) 101 | knapsack(k+1,cp,cw); 102 | if((cp > fp) && (k == n)) 103 | { 104 | fp = cp; 105 | fw = cw; 106 | for(j=0;j<=k;j++) 107 | x[j] = y[j]; 108 | } 109 | } 110 | } 111 | 112 | int main() 113 | { 114 | get(); 115 | printf("\n The Sack is arranged in the order\n"); 116 | sort(); 117 | knapsack(0,0.0,0.0); 118 | show(); 119 | 120 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 12/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 12. 2 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 2/gcd_middle_school.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define min(x,y) ((x) <= (y)) ? (x) : (y) 5 | 6 | int main(int argc, char const *argv[]) 7 | { 8 | int x,y,i; 9 | scanf("%d%d",&x,&y); 10 | int m = min(x,y); 11 | int gcd = 1; 12 | int op=0; 13 | if(x==0) 14 | gcd = y; 15 | else if(y==0) 16 | gcd = x; 17 | else 18 | for(i=2;i<=m;) 19 | { 20 | op++; 21 | if(x%i==0 && y%i==0) 22 | { 23 | gcd = gcd * i; 24 | x=x/i; 25 | y=y/i; 26 | } 27 | else 28 | { 29 | if(i==2) 30 | i=3; 31 | else if(i>=3) 32 | i=i+2; 33 | if(x 2 | #include 3 | 4 | void main() 5 | { 6 | int m,n,t,res,opc = 0; 7 | printf("\nEnter two numbers\n"); 8 | scanf("%d %d", &m,&n); 9 | if( m > n ) 10 | t = m; 11 | else 12 | t = n; 13 | while( t != 0 ) 14 | { 15 | opc++; 16 | if( m%t == 0 ) 17 | { 18 | if( n%t == 0 ) 19 | { 20 | res = t; 21 | break; 22 | } 23 | } 24 | t--; 25 | } 26 | if( m == 0 ) 27 | res = n; 28 | if( n == 0 ) 29 | res = m; 30 | printf("The gcd of the numbers is : %d\n",res ); 31 | printf("The operation count is : %d\n", opc); 32 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 2/p2_efficient: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Algorithms Lab/Week 2/p2_efficient -------------------------------------------------------------------------------- /Algorithms Lab/Week 2/p2_efficient.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define min(x,y) ((x) <= (y)) ? (x) : (y) 5 | 6 | int main(int argc, char const *argv[]) 7 | { 8 | int x,y,i; 9 | scanf("%d%d",&x,&y); 10 | int m = min(x,y); 11 | int gcd = 1; 12 | int op=0; 13 | if(!x) 14 | { 15 | gcd=y; 16 | goto end; 17 | } 18 | if(!y) 19 | { 20 | gcd=x; 21 | goto end; 22 | } 23 | for(i=2;i<=m;) 24 | { 25 | m=min(x,y); 26 | if(x%i==0 && y%i==0) 27 | { 28 | op++; 29 | gcd = gcd * i; 30 | x=x/i; 31 | y=y/i; 32 | } 33 | else 34 | { 35 | if(i==2) 36 | i=3; 37 | else if(i>=3) 38 | i=i+2; 39 | } 40 | } 41 | end:printf("%d\n",gcd ); 42 | printf("%d\n",op); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 2/readme.md: -------------------------------------------------------------------------------- 1 | Programs of lab 2. 2 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 3/p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int bubble_sort ( int a[50], int n ) 5 | { 6 | int i,j,opc = 0; 7 | for ( i = 0; i < n-1; i++ ) 8 | { 9 | for ( j = 0; j < n-i-1; j++ ) 10 | { 11 | opc++; 12 | if( a[j] > a[j+1] ) 13 | { 14 | a[j]+= a[j+1]; 15 | a[j+1] = a[j] - a[j+1]; 16 | a[j]-= a[j+1]; 17 | } 18 | } 19 | } 20 | return opc; 21 | } 22 | 23 | 24 | void main() 25 | { 26 | int a[50],n,i,opc = 0; 27 | printf("\nEnter limit\n"); 28 | scanf("%d",&n); 29 | printf("\nEnter numbers : \n"); 30 | for ( i = 0; i < n; i++ ) 31 | { 32 | scanf( "%d",&a[i] ); 33 | } 34 | opc = bubble_sort( a, n ); 35 | printf("\nThe sorted array is : " ); 36 | for ( i = 0; i < n; i++ ) 37 | { 38 | printf( "\t%d\t",a[i] ); 39 | } 40 | printf("\nThe opeation count is : %d",opc ); 41 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 3/p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int string_matching ( char t[50], char p[50] ) 7 | { 8 | int i,l_t,l_p,j,opc = 0; 9 | l_t = strlen(t); 10 | l_p = strlen(p); 11 | for ( i = 0; i < l_t - l_p; i++ ) 12 | { 13 | opc++; 14 | j = 0; 15 | while ( j < l_p && p[j] == t[i+j] ) 16 | { 17 | j++; 18 | opc++; 19 | } 20 | 21 | if ( j == l_p ) 22 | { 23 | printf( "\nOperation count : %d",opc ); 24 | return i; 25 | } 26 | } 27 | printf( "\nOperation count : %d",opc ); 28 | return -1; 29 | } 30 | 31 | void main () 32 | { 33 | char p[50],t[50]; 34 | int res; 35 | printf("\nEnter text\n"); 36 | gets(t); 37 | printf("\nEnter pattern to search\n"); 38 | gets(p); 39 | res = string_matching( t,p ); 40 | if ( res != -1 ) 41 | { 42 | printf("\nMatch found at pos %d in text\n",res+1 ); 43 | } 44 | else 45 | { 46 | printf("\nNo match found\n"); 47 | } 48 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 3/partition.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int sum(int arr[],int n) 6 | { 7 | int sum =0; 8 | for(int i=0;i",i); 48 | display(pow_set[power_set_size-i-1],size); 49 | display(pow_set[i],size); 50 | } 51 | } 52 | printf("power set:\n"); 53 | for(i=1;i",i); 56 | for(j=0;j 2 | #include 3 | 4 | int *a; 5 | int n; 6 | 7 | int sum(int x) 8 | { 9 | int i=0,s=0; 10 | while(n!=i) 11 | { 12 | int f=1< 2 | #include 3 | 4 | int *a; 5 | int n; 6 | 7 | float sum(int x) 8 | { 9 | int i=0; 10 | float s=0.0; 11 | while(n!=i) 12 | { 13 | int f=1< 2 | #include 3 | void selectionSort(int *a, unsigned int n) 4 | { 5 | unsigned int i,j,min; 6 | int temp; 7 | int opcount=0; // introduce opcount 8 | for(i= 0;i 2 | #include 3 | 4 | void recurSum(int *arr,int n,int *ind,int i,int *sum,int *finalInd){ 5 | if(i==n){ 6 | int t=0; 7 | for(int a=0;a=t){ 11 | *sum=t; 12 | //printf("Index\n"); 13 | for(int b=0;b 2 | #include 3 | 4 | int c=0; 5 | int arr[100][100]; 6 | int min=10000; 7 | char answer[100]; 8 | int size=0; 9 | void swap(char *x, char *y) 10 | { 11 | char temp; 12 | temp = *x; 13 | *x = *y; 14 | *y = temp; 15 | } 16 | void permute(char *a, int l, int r) 17 | { 18 | int i; 19 | if (l == r) 20 | { 21 | int sum=0; 22 | for(int i=0;i 2 | #include 3 | 4 | int visited[100]; 5 | int push[20]; 6 | int pop[20]; 7 | int push_i,pop_i; 8 | 9 | void dfs(int a[][100],int i,int v) 10 | { 11 | visited[i]=1; 12 | int j=0; 13 | for(j=0;j 2 | #include 3 | 4 | void main() 5 | { 6 | printf("Enter the number of vertices\n"); 7 | int vertices; 8 | scanf("%d",&vertices); 9 | int arr[vertices][vertices]; 10 | int queue[1000]; 11 | for(int i=0;i<=999;i+=1) 12 | queue[i]=0; 13 | int f=-1,r=-1; 14 | for(int i=0;ir) 43 | break; 44 | int a=queue[f]; 45 | printf("%d ",a+1); 46 | for(int j=0;j 2 | 3 | int visited[100]; 4 | int push[20]; 5 | int pop[20]; 6 | int push_i,pop_i; 7 | 8 | void dfs(int a[][100],int i,int v) 9 | { 10 | visited[i]=1; 11 | int j=0; 12 | for(j=0;j= 0; i--) 71 | { 72 | 73 | printf("%d ",pop[i] + 1); 74 | } 75 | printf("\n"); 76 | 77 | 78 | 79 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 5/p1_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | int src_rem(int arr[][100],int v) 3 | { 4 | int no[v]; 5 | int k=0; 6 | for(int i=0;i 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | 10 | struct node *create(){ 11 | struct node *root = (struct node *)malloc(sizeof(struct node)); 12 | printf("Enter data(-1 for no data)"); 13 | int x; 14 | scanf("%d",&x); 15 | if(x==-1){ 16 | return NULL; 17 | } 18 | root->data=x; 19 | printf("\nEnter left child of %d",x); 20 | root->left = create(); 21 | printf("\nEnter right child of %d",x); 22 | root->right = create(); 23 | } 24 | 25 | int max(int a,int b){ 26 | return a>b?a:b; 27 | } 28 | int height(struct node *root){ 29 | if(root==NULL){ 30 | return 0; 31 | } 32 | return 1+max(height(root->left),height(root->right)); 33 | } 34 | 35 | int diameter(struct node *root){ 36 | if(root==NULL){ 37 | return 0; 38 | } 39 | int lheight=height(root->left); 40 | int rheight=height(root->right); 41 | int ldiam=diameter(root->left); 42 | int rdiam=diameter(root->right); 43 | return max(1+lheight+rheight,max(ldiam,rdiam)); 44 | } 45 | 46 | int main() 47 | { 48 | struct node *root = (struct node *)malloc(sizeof(struct node)); 49 | root=NULL; 50 | root=create(); 51 | printf("The diameter is %d\n",diameter(root) ); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 5/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 5. 2 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 6/prg1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int opcount=0; 5 | 6 | typedef struct node{ 7 | int data; 8 | struct node *left,*right; 9 | }* Node; 10 | 11 | Node create(){ 12 | Node root = (Node) malloc(sizeof(struct node)); 13 | //printf("Enter data: "); 14 | int x; 15 | scanf("%d",&x); 16 | if(x==-1){ 17 | return NULL; 18 | } 19 | root->data=x; 20 | //printf("Left child of %d\n",x); 21 | root->left = create(); 22 | //printf("Right child of %d\n",x); 23 | root->right = create(); 24 | } 25 | 26 | int count(Node root) 27 | { 28 | opcount++; 29 | if (!root) 30 | return 0; 31 | else 32 | return(count(root->left) + 1 + count(root->right)); 33 | } 34 | 35 | void main() 36 | { 37 | Node root=NULL; 38 | printf("Creating Tree (-1 for NULL)\nEnter data\n"); 39 | root=create(); 40 | int c=count(root); 41 | printf("Count is %d\n",c); 42 | printf("Opcount is %d\n",opcount); 43 | } 44 | 45 | // 0 1 2 -1 -1 3 4 -1 -1 5 -1 -1 6 -1 7 -1 8 9 10 -1 -1 11 -1 -1 12 -1 -1 46 | // 0 1 2 3 -1 -1 4 5 6 -1 -1 7 -1 -1 -1 8 -1 9 10 -1 -1 11 -1 12 -1 -1 13 -1 14 -1 -1 47 | // 0 1 -1 -1 2 3 -1 -1 -1 -------------------------------------------------------------------------------- /Algorithms Lab/Week 6/prg2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int opcount=0; 5 | 6 | void swap(int* a, int* b) 7 | { 8 | int t = *a; 9 | *a = *b; 10 | *b = t; 11 | } 12 | 13 | int partition (int a[], int l, int r) 14 | { 15 | int p=a[l]; 16 | int i=l,j=r+1; 17 | while(!(i>=j)) 18 | { 19 | while(!(a[++i]>=p)) 20 | opcount++; 21 | opcount++; 22 | while(!(a[--j]<=p)) 23 | opcount++; 24 | opcount++; 25 | swap(&a[i],&a[j]); 26 | } 27 | swap(&a[i],&a[j]); 28 | swap(&a[l],&a[j]); 29 | return j; 30 | } 31 | 32 | void quickSort(int a[], int l, int r) 33 | { 34 | if (l < r) 35 | { 36 | int s=partition(a,l,r); 37 | quickSort(a,l,s-1); 38 | quickSort(a,s+1,r); 39 | } 40 | } 41 | 42 | void main() 43 | { 44 | int n=5,i=0,j=0,k=0; 45 | 46 | for(int k=0;k<5;k++) 47 | { 48 | i=0; 49 | opcount=0; 50 | int * a = (int *) malloc(n*sizeof(int)); 51 | while(i 2 | #include 3 | #include 4 | #include 5 | 6 | int opcount=0; 7 | 8 | void display(int a[],int n) 9 | { 10 | for(int i=0;i1) 36 | { 37 | int c=ceil(n/2.0),f=floor(n/2.0); 38 | int B[f],C[c]; 39 | memcpy(B,A,sizeof(int)*f); 40 | memcpy(C,A+f,sizeof(int)*(n-f)); 41 | mergeSort(B,f); 42 | mergeSort(C,c); 43 | merge(B,f,C,c,A); 44 | } 45 | } 46 | 47 | void main() 48 | { 49 | int n=5,i=0,j=0,k=0; 50 | 51 | for(int k=0;k<5;k++) 52 | { 53 | i=0; 54 | opcount=0; 55 | int * a = (int *) malloc(n*sizeof(int)); 56 | while(i 3 | #include 4 | 5 | // An AVL tree node 6 | struct Node 7 | { 8 | int key; 9 | struct Node *left; 10 | struct Node *right; 11 | int height; 12 | }; 13 | 14 | // A utility function to get maximum of two integers 15 | int max(int a, int b); 16 | 17 | // A utility function to get height of the tree 18 | int height(struct Node *N) 19 | { 20 | if (N == NULL) 21 | return 0; 22 | return N->height; 23 | } 24 | 25 | // A utility function to get maximum of two integers 26 | int max(int a, int b) 27 | { 28 | return (a > b)? a : b; 29 | } 30 | 31 | /* Helper function that allocates a new node with the given key and 32 | NULL left and right pointers. */ 33 | struct Node* newNode(int key) 34 | { 35 | struct Node* node = (struct Node*) 36 | malloc(sizeof(struct Node)); 37 | node->key = key; 38 | node->left = NULL; 39 | node->right = NULL; 40 | node->height = 1; // new node is initially added at leaf 41 | return(node); 42 | } 43 | 44 | // A utility function to right rotate subtree rooted with y 45 | // See the diagram given above. 46 | struct Node *rightRotate(struct Node *y) 47 | { 48 | struct Node *x = y->left; 49 | struct Node *T2 = x->right; 50 | 51 | // Perform rotation 52 | x->right = y; 53 | y->left = T2; 54 | 55 | // Update heights 56 | y->height = max(height(y->left), height(y->right))+1; 57 | x->height = max(height(x->left), height(x->right))+1; 58 | 59 | // Return new root 60 | return x; 61 | } 62 | 63 | // A utility function to left rotate subtree rooted with x 64 | // See the diagram given above. 65 | struct Node *leftRotate(struct Node *x) 66 | { 67 | struct Node *y = x->right; 68 | struct Node *T2 = y->left; 69 | 70 | // Perform rotation 71 | y->left = x; 72 | x->right = T2; 73 | 74 | // Update heights 75 | x->height = max(height(x->left), height(x->right))+1; 76 | y->height = max(height(y->left), height(y->right))+1; 77 | 78 | // Return new root 79 | return y; 80 | } 81 | 82 | // Get Balance factor of node N 83 | int getBalance(struct Node *N) 84 | { 85 | if (N == NULL) 86 | return 0; 87 | return height(N->left) - height(N->right); 88 | } 89 | 90 | // Recursive function to insert key in subtree rooted 91 | // with node and returns new root of subtree. 92 | struct Node* insert(struct Node* node, int key) 93 | { 94 | /* 1. Perform the normal BST insertion */ 95 | if (node == NULL) 96 | return(newNode(key)); 97 | 98 | if (key < node->key) 99 | node->left = insert(node->left, key); 100 | else if (key > node->key) 101 | node->right = insert(node->right, key); 102 | else // Equal keys are not allowed in BST 103 | return node; 104 | 105 | /* 2. Update height of this ancestor node */ 106 | node->height = 1 + max(height(node->left), 107 | height(node->right)); 108 | 109 | /* 3. Get the balance factor of this ancestor 110 | node to check whether this node became 111 | unbalanced */ 112 | int balance = getBalance(node); 113 | 114 | // If this node becomes unbalanced, then 115 | // there are 4 cases 116 | 117 | // Left Left Case 118 | if (balance > 1 && key < node->left->key) 119 | return rightRotate(node); 120 | 121 | // Right Right Case 122 | if (balance < -1 && key > node->right->key) 123 | return leftRotate(node); 124 | 125 | // Left Right Case 126 | if (balance > 1 && key > node->left->key) 127 | { 128 | node->left = leftRotate(node->left); 129 | return rightRotate(node); 130 | } 131 | 132 | // Right Left Case 133 | if (balance < -1 && key < node->right->key) 134 | { 135 | node->right = rightRotate(node->right); 136 | return leftRotate(node); 137 | } 138 | 139 | /* return the (unchanged) node pointer */ 140 | return node; 141 | } 142 | 143 | // A utility function to print preorder traversal 144 | // of the tree. 145 | // The function also prints height of every node 146 | void preOrder(struct Node *root) 147 | { 148 | if(root != NULL) 149 | { 150 | printf("%d ", root->key); 151 | preOrder(root->left); 152 | preOrder(root->right); 153 | } 154 | } 155 | 156 | /* Drier program to test above function*/ 157 | int main() 158 | { 159 | struct Node *root = NULL; 160 | 161 | /* Constructing tree given in the above figure */ 162 | root = insert(root, 10); 163 | root = insert(root, 20); 164 | root = insert(root, 30); 165 | root = insert(root, 40); 166 | root = insert(root, 50); 167 | root = insert(root, 25); 168 | 169 | /* The constructed AVL Tree would be 170 | 30 171 | / \ 172 | 20 40 173 | / \ \ 174 | 10 25 50 175 | */ 176 | 177 | printf("Preorder traversal of the constructed AVL" 178 | " tree is \n"); 179 | preOrder(root); 180 | 181 | return 0; 182 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 7/prg2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Algorithms Lab/Week 7/prg2 -------------------------------------------------------------------------------- /Algorithms Lab/Week 7/prg2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define M 3 4 | 5 | struct node { 6 | int n; 7 | int keys[M-1]; 8 | struct node *p[M]; 9 | }*root=NULL; 10 | 11 | enum KeyStatus { Duplicate,Success,InsertIt }; 12 | 13 | void insert(int); 14 | void display(struct node *,int); 15 | void search(int x); 16 | enum KeyStatus ins(struct node *, int , int* , struct node** ); 17 | int searchPos(int ,int *, int ); 18 | void inorder(struct node *); 19 | 20 | 21 | int main() 22 | { 23 | int key; 24 | int choice; 25 | printf("Creation of 2-3 tree for M=%d\n",M); 26 | int x; 27 | printf("Enter values (-1 to end)\n"); 28 | while(1) 29 | { 30 | scanf("%d",&x); 31 | if(x==-1) 32 | break; 33 | insert(x); 34 | } 35 | 36 | printf("2-3tree is :\n"); 37 | display(root,0); 38 | return 0; 39 | } 40 | 41 | void insert(int key) 42 | { 43 | struct node *newnode; 44 | int upKey; 45 | enum KeyStatus value; 46 | value = ins(root, key, &upKey, &newnode); 47 | if (value == Duplicate) 48 | printf("Key already available\n"); 49 | if (value == InsertIt) 50 | { 51 | struct node *uproot = root; 52 | root=malloc(sizeof(struct node)); 53 | root->n = 1; 54 | root->keys[0] = upKey; 55 | root->p[0] = uproot; 56 | root->p[1] = newnode; 57 | } 58 | } 59 | 60 | enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node **newnode) 61 | { 62 | struct node *newPtr, *lastPtr; 63 | int pos, i, n,splitPos; 64 | int newKey, lastKey; 65 | enum KeyStatus value; 66 | if (ptr == NULL) 67 | { 68 | *newnode = NULL; 69 | *upKey = key; 70 | return InsertIt; 71 | } 72 | n = ptr->n; 73 | pos = searchPos(key, ptr->keys, n); 74 | if (pos < n && key == ptr->keys[pos]) 75 | return Duplicate; 76 | value = ins(ptr->p[pos], key, &newKey, &newPtr); 77 | if (value != InsertIt) 78 | return value; 79 | 80 | if (n < M - 1) 81 | { 82 | pos = searchPos(newKey, ptr->keys, n); 83 | /*Shifting the key and pointer right for inserting the new key*/ 84 | for (i=n; i>pos; i--) 85 | { 86 | ptr->keys[i] = ptr->keys[i-1]; 87 | ptr->p[i+1] = ptr->p[i]; 88 | } 89 | /*Key is inserted at exact location*/ 90 | ptr->keys[pos] = newKey; 91 | ptr->p[pos+1] = newPtr; 92 | ++ptr->n; /*incrementing the number of keys in node*/ 93 | return Success; 94 | }/*End of if */ 95 | /*If keys in nodes are maximum and position of node to be inserted is last*/ 96 | if (pos == M - 1) 97 | { 98 | lastKey = newKey; 99 | lastPtr = newPtr; 100 | } 101 | else /*If keys in node are maximum and position of node to be inserted is not last*/ 102 | { 103 | lastKey = ptr->keys[M-2]; 104 | lastPtr = ptr->p[M-1]; 105 | for (i=M-2; i>pos; i--) 106 | { 107 | ptr->keys[i] = ptr->keys[i-1]; 108 | ptr->p[i+1] = ptr->p[i]; 109 | } 110 | ptr->keys[pos] = newKey; 111 | ptr->p[pos+1] = newPtr; 112 | } 113 | splitPos = (M - 1)/2; 114 | (*upKey) = ptr->keys[splitPos]; 115 | 116 | (*newnode)=malloc(sizeof(struct node));/*Right node after split*/ 117 | ptr->n = splitPos; /*No. of keys for left splitted node*/ 118 | (*newnode)->n = M-1-splitPos;/*No. of keys for right splitted node*/ 119 | for (i=0; i < (*newnode)->n; i++) 120 | { 121 | (*newnode)->p[i] = ptr->p[i + splitPos + 1]; 122 | if(i < (*newnode)->n - 1) 123 | (*newnode)->keys[i] = ptr->keys[i + splitPos + 1]; 124 | else 125 | (*newnode)->keys[i] = lastKey; 126 | } 127 | (*newnode)->p[(*newnode)->n] = lastPtr; 128 | return InsertIt; 129 | } 130 | 131 | void display(struct node *ptr, int blanks) 132 | { 133 | if (ptr) 134 | { 135 | int i; 136 | for(i=1; i<=blanks; i++) 137 | printf(" "); 138 | for (i=0; i < ptr->n; i++) 139 | printf("%d ",ptr->keys[i]); 140 | printf("\n"); 141 | for (i=ptr->n; i >=0; i--) 142 | display(ptr->p[i], blanks+10); 143 | } 144 | } 145 | 146 | 147 | int searchPos(int key, int *key_arr, int n) 148 | { 149 | int pos=0; 150 | while (pos < n && key > key_arr[pos]) 151 | pos++; 152 | return pos; 153 | } 154 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 7/readme.md: -------------------------------------------------------------------------------- 1 | ## DAA Lab week 7 codes 2 | -------------------------------------------------------------------------------- /Algorithms Lab/Week 8/p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int callback(int arr[],int i) 5 | { 6 | int m=i; 7 | int n=(i-1)/2; 8 | while(arr[m]>arr[n]) 9 | { 10 | int a=arr[m]; 11 | arr[m]=arr[n]; 12 | arr[n]=a; 13 | m=(m-1)/2; 14 | n=(m-1)/2; 15 | if(m<0 || n<0) 16 | break; 17 | } 18 | } 19 | int main() 20 | { 21 | int c = 1, ins = 0; 22 | int arr[1000] = { 0 }; 23 | while(1) 24 | { 25 | int ele; 26 | printf("Enter the data\n"); 27 | scanf("%d",&ele); 28 | if(ins==0 && arr[ins]==-1) 29 | { 30 | arr[ins]=ele; 31 | } 32 | else 33 | { 34 | if(arr[2*ins+1]==-1) 35 | { 36 | arr[2*ins+1]=ele; 37 | callback(arr,2*ins+1); 38 | } 39 | else 40 | { 41 | arr[2*ins+2]=ele; 42 | callback(arr,2*ins+2); 43 | ins+=1; 44 | } 45 | } 46 | printf("Continue( 0 - No 1 - Yes)\n"); 47 | scanf("%d",&ele); 48 | if(ele==0) 49 | break; 50 | else 51 | c++; 52 | } 53 | printf("The elements are\n"); 54 | for(int i=0;i 2 | 3 | 4 | void heapify(int a[],int n,int i){ 5 | int l=2*i+1; 6 | int r=2*i+2; 7 | int m=i; 8 | if(la[m]) 9 | m=l; 10 | if(ra[m]) 11 | m=r; 12 | if(m!=i){ 13 | int t=a[i]; 14 | a[i]=a[m]; 15 | a[m]=t; 16 | heapify(a,n,m); 17 | } 18 | } 19 | 20 | void makeheap(int a[],int n){ 21 | for(int i=n/2-1;i>=0;i--) 22 | heapify(a,n,i); 23 | } 24 | 25 | void heapsort(int a[],int n){ 26 | makeheap(a,n); 27 | 28 | for(int i=n-1;i>=0;i--){ 29 | int t=a[0]; 30 | a[0]=a[i]; 31 | a[i]=t; 32 | heapify(a,i,0); 33 | } 34 | 35 | } 36 | 37 | int main(){ 38 | int a[100]; 39 | int n; 40 | printf("Enter number of elements\n"); 41 | scanf("%d",&n); 42 | printf("Enter elements:\n"); 43 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | void shifttable(char p[],int table[],int size){ 6 | 7 | int m=strlen(p); 8 | for(int i=m-2;i>=0;i--){ 9 | char c=p[i]; 10 | int sh=m-i-1; 11 | int pos; 12 | /*if(c>='A'&&c<='Z') 13 | pos = (int)c - 65;//only for capital letters 14 | else if(c>='a'&&c<='z') 15 | pos = (int)c - 90;//only for capital letters*/ 16 | pos = (int)c - 65;//only for capital letters 17 | //printf("pos = %d , shift = %d\n",pos,sh ); 18 | if(table[pos]>sh) 19 | table[pos]=sh; 20 | } 21 | } 22 | 23 | 24 | int is_present(char p[],char c,int len){ 25 | 26 | for(int i=0;i 2 | #include 3 | #define SIZE 7 4 | 5 | 6 | struct link 7 | { 8 | int val; 9 | struct link * l; 10 | }; 11 | 12 | void main () 13 | { 14 | int n, num, index, i, f = 0, search, key; 15 | struct link * a[ SIZE ]; 16 | printf( "\nEnter limit\n" ); 17 | scanf( "%d", &n ); 18 | printf( "\nEnter values" ); 19 | for ( i = 0; i < SIZE ; i ++ ) 20 | { 21 | a[i] = NULL; 22 | } 23 | for ( i = 0; i < n; i++ ) 24 | { 25 | scanf( "%d", &num ); 26 | index = num % SIZE; 27 | if ( a[index] == NULL ) 28 | { 29 | struct link *temp = ( struct link * )malloc ( sizeof ( struct link ) ); 30 | temp -> val = num; 31 | temp -> l = NULL; 32 | a[index] = temp; 33 | } 34 | else 35 | { 36 | struct link * temp = a[ index ]; 37 | while ( temp -> l != NULL ) 38 | { 39 | temp = temp -> l; 40 | } 41 | struct link * t = ( struct link * )malloc ( sizeof ( struct link ) ); 42 | t -> val = num; 43 | t -> l = NULL; 44 | temp -> l = t; 45 | } 46 | 47 | 48 | } 49 | printf("\n The entered table is : \n "); 50 | for ( i = 0; i < SIZE - 1; i++ ) 51 | { 52 | struct link * t = a[i]; 53 | while ( t != NULL ) 54 | { 55 | printf("Key : %d, Value : %d\t ",i % SIZE , t -> val ); 56 | t = t -> l; 57 | } 58 | printf( "\n" ); 59 | } 60 | printf( "\n Enter value to search " ); 61 | scanf ( "%d", &search ); 62 | key = search % SIZE; 63 | if ( a[key] == NULL ) 64 | { 65 | printf( "\n No entry " ); 66 | 67 | } 68 | else 69 | { 70 | struct link * temp = a[ key ]; 71 | while ( temp != NULL ) 72 | { 73 | if ( temp -> val == search ) 74 | { 75 | f = 1; 76 | break; 77 | } 78 | temp = temp -> l; 79 | } 80 | } 81 | if ( f == 1 ) 82 | { 83 | printf ( "\nValue found at index %d", key + 1 ); 84 | 85 | } 86 | else 87 | { 88 | printf ("No match found " ); 89 | } 90 | 91 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 9/p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 7 4 | 5 | 6 | struct link 7 | { 8 | int val; 9 | }; 10 | 11 | void main () 12 | { 13 | int n, num, index, i, f = 0, search, key, c, opc = 0; 14 | struct link a[ SIZE ]; 15 | printf( "\nEnter limit\n" ); 16 | scanf( "%d", &n ); 17 | printf( "\nEnter values" ); 18 | for ( i = 0; i < SIZE ; i ++ ) 19 | { 20 | a[i].val = 999; 21 | } 22 | for ( i = 0; i < n; i++ ) 23 | { 24 | scanf( "%d", &num ); 25 | index = num % SIZE; 26 | if ( a[index].val == 999 ) 27 | { 28 | a[index].val = num; 29 | } 30 | else 31 | { 32 | c = index; 33 | index = ( index + 1 ) % SIZE; 34 | while ( index != c ) 35 | { 36 | if ( a[ index ].val == 999 ) 37 | { 38 | a[ index ].val = num; 39 | break; 40 | } 41 | else 42 | { 43 | index = ( index + 1 ) % SIZE; 44 | } 45 | } 46 | } 47 | } 48 | printf("\n The entered table is : \n"); 49 | for ( i = 0; i < SIZE; i++ ) 50 | { 51 | if ( a[i].val != 999 ) 52 | { 53 | printf("Key : %d, Value : %d\t ",i % SIZE , a[i].val ); 54 | 55 | } 56 | 57 | printf( "\n" ); 58 | } 59 | printf( "\n Enter value to search " ); 60 | scanf ( "%d", &search ); 61 | key = search % SIZE; 62 | if ( a[key].val == 999 ) 63 | { 64 | printf( "\n No entry " ); 65 | 66 | } 67 | else 68 | { 69 | for ( i = 0; i < SIZE; i++ ) 70 | { 71 | opc++; 72 | if ( a[i].val == search ) 73 | { 74 | f = 1; 75 | key = i; 76 | break; 77 | } 78 | 79 | } 80 | } 81 | if ( f == 1 ) 82 | { 83 | printf ( "\nValue found at index %d", key ); 84 | 85 | } 86 | else 87 | { 88 | printf ("No match found " ); 89 | } 90 | printf( "\n The operation count is : %d ",opc ); 91 | 92 | } -------------------------------------------------------------------------------- /Algorithms Lab/Week 9/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 9. 2 | -------------------------------------------------------------------------------- /Algorithms Lab/algorithm lab three labs..pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Algorithms Lab/algorithm lab three labs..pdf -------------------------------------------------------------------------------- /Algorithms Lab/readme.md: -------------------------------------------------------------------------------- 1 | Contains manual and code for algorithms lab. 2 | -------------------------------------------------------------------------------- /Books/CA/(CS6143) Computer Architecture - A Quantitative Approach 5e.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/CA/(CS6143) Computer Architecture - A Quantitative Approach 5e.pdf -------------------------------------------------------------------------------- /Books/CA/Computer Organisation and Architecture 8e by William Stallings.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/CA/Computer Organisation and Architecture 8e by William Stallings.pdf -------------------------------------------------------------------------------- /Books/DC/(9th Edition) (William Stallings Books on Computer and Data Communications) William Stallings-Computer Organization and Architecture-Prentice Hall (2012).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/DC/(9th Edition) (William Stallings Books on Computer and Data Communications) William Stallings-Computer Organization and Architecture-Prentice Hall (2012).pdf -------------------------------------------------------------------------------- /Books/DC/Data Communications and Networking By Behrouz A.Forouzan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/DC/Data Communications and Networking By Behrouz A.Forouzan.pdf -------------------------------------------------------------------------------- /Books/OS/Operating_System_Concepts,_8th_Edition[A4].pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/OS/Operating_System_Concepts,_8th_Edition[A4].pdf -------------------------------------------------------------------------------- /Books/SE/RPL-7th_ed_software_engineering_a_practitioners_approach_by_roger_s._pressman_.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/SE/RPL-7th_ed_software_engineering_a_practitioners_approach_by_roger_s._pressman_.pdf -------------------------------------------------------------------------------- /Books/SE/fundamentals-of-software-engineering-fourth-edition-rajib-mall.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/Books/SE/fundamentals-of-software-engineering-fourth-edition-rajib-mall.pdf -------------------------------------------------------------------------------- /CN Lab/Lab No 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Lab No 1.pdf -------------------------------------------------------------------------------- /CN Lab/Week 1/filecopy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | char buf[BUFSIZ]; 8 | int n, input, output; 9 | input = open ("f1.txt", O_RDONLY ); 10 | output = open ("f1copy.txt", O_WRONLY | O_CREAT, 0644 ); 11 | 12 | while (( n = read( input, buf, BUFSIZ)) > 0) 13 | { 14 | write ( output, buf, n); 15 | } 16 | close ( input ); 17 | close ( output ); 18 | return 0; 19 | 20 | 21 | // Contributed by Ayush Garg. 22 | } -------------------------------------------------------------------------------- /CN Lab/Week 1/readme.md: -------------------------------------------------------------------------------- 1 | filecopy.c : A C program to copy contents of one file of unknown size using system commands. 2 | -------------------------------------------------------------------------------- /CN Lab/Week 10/README.md: -------------------------------------------------------------------------------- 1 | NS2 solutions for Week 10 2 | -------------------------------------------------------------------------------- /CN Lab/Week 10/q1.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | $ns color 1 green 3 | $ns color 2 Red 4 | 5 | set nt [open simulate.nam w] 6 | $ns namtrace-all $nt 7 | 8 | set tr [open simulate.tr w] 9 | $ns trace-all $tr 10 | 11 | proc finish {} { 12 | global ns nt tr 13 | $ns flush-trace 14 | #Close the NAM trace file 15 | close $nt 16 | close $tr 17 | exec nam simulate.nam & 18 | exit 0 19 | } 20 | 21 | set n0 [$ns node] 22 | set n1 [$ns node] 23 | 24 | $ns duplex-link $n0 $n1 10Mb 10ms DropTail 25 | $ns queue-limit $n0 $n1 10 26 | $ns duplex-link-op $n0 $n1 orient right-down 27 | 28 | $ns duplex-link-op $n0 $n1 queuePos 0.5 29 | 30 | set udp [new Agent/UDP] 31 | $ns attach-agent $n0 $udp 32 | 33 | set null [new Agent/Null] 34 | $ns attach-agent $n1 $null 35 | $ns connect $udp $null 36 | $udp set fid_ 1 37 | 38 | set cbr [new Application/Traffic/CBR] 39 | $cbr attach-agent $udp 40 | $cbr set type_ CBR 41 | $cbr set packet_size_ 1000 42 | $cbr set rate_ 1mb 43 | $cbr set random_ false 44 | 45 | $ns at 0.1 "$cbr start" 46 | $ns at 4.0 "$cbr stop" 47 | $ns at 4.5 "finish" 48 | 49 | $ns run 50 | -------------------------------------------------------------------------------- /CN Lab/Week 10/q2.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | $ns color 1 green 3 | $ns color 2 Red 4 | 5 | set nt [open simulate.nam w] 6 | $ns namtrace-all $nt 7 | 8 | set tr [open simulate.tr w] 9 | $ns trace-all $tr 10 | 11 | proc finish {} { 12 | global ns nt tr 13 | $ns flush-trace 14 | #Close the NAM trace file 15 | close $nt 16 | close $tr 17 | exec nam simulate.nam & 18 | exit 0 19 | } 20 | 21 | set n0 [$ns node] 22 | set n1 [$ns node] 23 | 24 | $ns duplex-link $n0 $n1 10Mb 10ms DropTail 25 | $ns queue-limit $n0 $n1 10 26 | $ns duplex-link-op $n0 $n1 orient right-down 27 | 28 | $ns duplex-link-op $n0 $n1 queuePos 0.5 29 | 30 | set tcp [new Agent/TCP] 31 | $tcp set class_ 1 32 | $ns attach-agent $n0 $tcp 33 | 34 | 35 | set sink [new Agent/TCPSink] 36 | $ns attach-agent $n1 $sink 37 | $ns connect $tcp $sink 38 | $tcp set fid_ 2 39 | 40 | set ftp [new Application/FTP] 41 | $ftp attach-agent $tcp 42 | $ftp set type_ FTP 43 | 44 | $ns at 0.1 "$ftp start" 45 | $ns at 4.0 "$ftp stop" 46 | $ns at 4.5 "finish" 47 | 48 | $ns run 49 | -------------------------------------------------------------------------------- /CN Lab/Week 10/q3.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | $ns color 1 green 3 | $ns color 2 Red 4 | $ns color 3 yellow 5 | 6 | set nt [open simulate.nam w] 7 | $ns namtrace-all $nt 8 | 9 | set tr [open simulate.tr w] 10 | $ns trace-all $tr 11 | 12 | proc finish {} { 13 | global ns nt tr 14 | $ns flush-trace 15 | #Close the NAM trace file 16 | close $nt 17 | close $tr 18 | exec nam simulate.nam & 19 | exit 0 20 | } 21 | 22 | set n1 [$ns node] 23 | set n2 [$ns node] 24 | set n3 [$ns node] 25 | set n4 [$ns node] 26 | 27 | $ns duplex-link $n1 $n2 2Mb 20ms DropTail 28 | $ns duplex-link $n3 $n4 1Mb 10ms DropTail 29 | $ns duplex-link $n1 $n4 3Mb 10ms DropTail 30 | 31 | $ns queue-limit $n1 $n2 10 32 | $ns queue-limit $n3 $n4 10 33 | $ns duplex-link-op $n1 $n2 orient down 34 | $ns duplex-link-op $n3 $n4 orient up 35 | $ns duplex-link-op $n1 $n4 orient right 36 | 37 | 38 | set tcp [new Agent/TCP] 39 | $tcp set class_ 1 40 | $ns attach-agent $n1 $tcp 41 | 42 | set sink [new Agent/TCPSink] 43 | $ns attach-agent $n2 $sink 44 | $ns connect $tcp $sink 45 | $tcp set fid_ 2 46 | 47 | set ftp [new Application/FTP] 48 | $ftp attach-agent $tcp 49 | $ftp set type_ FTP 50 | 51 | set udp [new Agent/UDP] 52 | $ns attach-agent $n3 $udp 53 | 54 | set null [new Agent/Null] 55 | $ns attach-agent $n4 $null 56 | $ns connect $udp $null 57 | $udp set fid_ 1 58 | 59 | set cbr [new Application/Traffic/CBR] 60 | $cbr attach-agent $udp 61 | $cbr set type_ CBR 62 | $cbr set packet_size_ 1000 63 | $cbr set rate_ 1mb 64 | $cbr set random_ false 65 | 66 | set tcp1 [new Agent/TCP] 67 | $tcp1 set class_ 2 68 | $ns attach-agent $n1 $tcp1 69 | 70 | set sink1 [new Agent/TCPSink] 71 | $ns attach-agent $n4 $sink1 72 | $ns connect $tcp1 $sink1 73 | $tcp1 set fid_ 3 74 | 75 | set ftp1 [new Application/FTP] 76 | $ftp1 attach-agent $tcp1 77 | $ftp1 set type_ FTP 78 | 79 | 80 | $ns at 0.1 "$ftp start" 81 | $ns at 4.0 "$ftp stop" 82 | $ns at 0.2 "$ftp1 start" 83 | $ns at 4.0 "$ftp1 stop" 84 | $ns at 0.1 "$cbr start" 85 | $ns at 4.0 "$cbr stop" 86 | $ns at 4.5 "finish" 87 | 88 | $ns run 89 | -------------------------------------------------------------------------------- /CN Lab/Week 11/addn_p.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | 3 | $ns color 1 red 4 | $ns color 2 green 5 | 6 | set nt [open simulate.nam w] 7 | $ns namtrace-all $nt 8 | 9 | set tr [open simulate.tr w] 10 | $ns trace-all $tr 11 | 12 | proc finish {} { 13 | global ns nt tr 14 | $ns flush-trace 15 | close $nt 16 | close $tr 17 | exec nam simulate.nam & 18 | exit 0 19 | } 20 | 21 | set n0 [$ns node] 22 | set n1 [$ns node] 23 | set n2 [$ns node] 24 | set n3 [$ns node] 25 | 26 | $ns duplex-link $n0 $n2 2Mb 10ms DropTail 27 | $ns duplex-link $n1 $n2 2Mb 10ms DropTail 28 | $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail 29 | 30 | $ns queue-limit $n0 $n2 5 31 | 32 | 33 | $ns duplex-link-op $n2 $n3 orient right 34 | $ns duplex-link-op $n2 $n0 orient left-up 35 | $ns duplex-link-op $n2 $n1 orient left-down 36 | 37 | 38 | 39 | $ns duplex-link-op $n0 $n2 queuePos 0.5 40 | 41 | set udp [new Agent/UDP] 42 | $ns attach-agent $n1 $udp 43 | 44 | set null [new Agent/Null] 45 | $ns attach-agent $n3 $null 46 | $ns connect $udp $null 47 | $udp set fid_ 1 48 | 49 | set tcp [new Agent/TCP] 50 | $tcp set class_ 1 51 | $ns attach-agent $n0 $tcp 52 | 53 | 54 | set sink [new Agent/TCPSink] 55 | $ns attach-agent $n3 $sink 56 | $ns connect $tcp $sink 57 | $tcp set fid_ 2 58 | 59 | set ftp [new Application/FTP] 60 | $ftp attach-agent $tcp 61 | $ftp set type_ FTP 62 | 63 | 64 | 65 | set cbr [new Application/Traffic/CBR] 66 | $cbr attach-agent $udp 67 | $cbr set type_ CBR 68 | $cbr set packet_size_ 1000 69 | $cbr set rate_ 1mb 70 | $cbr set random_ false 71 | 72 | $ns at 0.1 "$cbr start" 73 | $ns at 4.5 "$cbr stop" 74 | $ns at 1.0 "$ftp start" 75 | $ns at 4.0 "$ftp stop" 76 | $ns at 5.0 "finish" 77 | 78 | $ns run -------------------------------------------------------------------------------- /CN Lab/Week 11/l11qadd.awk: -------------------------------------------------------------------------------- 1 | BEGIN{ 2 | highest_packet_id=0; 3 | numFs=0; 4 | numRv=0; 5 | numDrop=0; 6 | } 7 | { 8 | action=$1; 9 | time=$2; 10 | from=$3; 11 | to=$4; 12 | type=$5; 13 | flow_id=$8; 14 | src=$9; 15 | dst=$10; 16 | seq_no=$11; 17 | packet_id=$12; 18 | if(packet_id>highest_packet_id) 19 | highest_packet_id; 20 | if(start_time[packet_id]==0) 21 | start_time[packet_id]=time; 22 | if(action!="d"){ 23 | if(action=="r"){ 24 | end_time[packet_id]=time; 25 | } 26 | }else{ 27 | end_time[packet_id]=-1; 28 | } 29 | if(action=="+") 30 | numFs++; 31 | if(action=="r") 32 | numRv++; 33 | if(action=="d") 34 | numDrop++; 35 | } 36 | END{ 37 | for(packet_id=0;packet_id<=highest_packet_id;packet_id++) 38 | { 39 | start=start_time[packet_id]; 40 | end=end_time[packet_id]; 41 | packet_duration=end-start; 42 | if(start<=end)printf("%f %f \n",start,packet_duration); 43 | } 44 | printf("Packet Delivery Ratio=%f",numRv/(numFs+numDrop)); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /CN Lab/Week 11/p1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | fsDrop_udp = 0; 4 | numFs_udp = 0; 5 | fsDrop_tcp = 0; 6 | numFs_tcp = 0; 7 | } 8 | { 9 | action = $1; 10 | time = $2; 11 | from = $3; 12 | to = $4; 13 | type = $5; 14 | pktsize = $6; 15 | flow_id = $8; 16 | src = $9; 17 | dst = $10; 18 | seq_no = $11; 19 | packet_id = $12; 20 | 21 | if(from==0 && to==1 && action=="+") 22 | numFs_udp++; 23 | if(flow_id==2 && action=="d") 24 | fsDrops_udp++; 25 | if(from==0 && to==2 && action=="+") 26 | numFs_tcp++; 27 | if(flow_id==1 && action=="d") 28 | fsDrops_tcp++; 29 | } 30 | END{ 31 | printf("UDP\t") 32 | printf("Number of packets sent: %d lost: %d\n",numFs_udp,fsDrops_udp); 33 | printf("Packet delivery ratio: %f\n",((numFs_udp-fsDrops_udp)/numFs_udp)); 34 | printf("TCP\t") 35 | printf("Number of packets sent: %d lost: %d\n",numFs_tcp,fsDrops_tcp); 36 | printf("Packet delivery ratio: %f",((numFs_tcp-fsDrops_tcp)/numFs_tcp)); 37 | } -------------------------------------------------------------------------------- /CN Lab/Week 11/p1.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | 3 | $ns color 1 red 4 | $ns color 2 green 5 | 6 | set nt [open simulate.nam w] 7 | $ns namtrace-all $nt 8 | 9 | set tr [open simulate.tr w] 10 | $ns trace-all $tr 11 | 12 | proc finish {} { 13 | global ns nt tr 14 | $ns flush-trace 15 | close $nt 16 | close $tr 17 | exec nam simulate.nam & 18 | exit 0 19 | } 20 | 21 | set n0 [$ns node] 22 | set n1 [$ns node] 23 | set n2 [$ns node] 24 | 25 | $ns duplex-link $n0 $n1 2Mb 10ms DropTail 26 | $ns duplex-link $n0 $n2 2Mb 10ms DropTail 27 | 28 | 29 | $ns queue-limit $n0 $n2 5 30 | 31 | 32 | $ns duplex-link-op $n0 $n1 orient right 33 | $ns duplex-link-op $n0 $n2 orient right-down 34 | 35 | 36 | $ns duplex-link-op $n0 $n1 queuePos 0.5 37 | 38 | set udp [new Agent/UDP] 39 | $ns attach-agent $n0 $udp 40 | 41 | set null [new Agent/Null] 42 | $ns attach-agent $n1 $null 43 | $ns connect $udp $null 44 | $udp set fid_ 1 45 | 46 | set tcp [new Agent/TCP] 47 | $tcp set class_ 1 48 | $ns attach-agent $n0 $tcp 49 | 50 | 51 | set sink [new Agent/TCPSink] 52 | $ns attach-agent $n2 $sink 53 | $ns connect $tcp $sink 54 | $tcp set fid_ 2 55 | 56 | set ftp [new Application/FTP] 57 | $ftp attach-agent $tcp 58 | $ftp set type_ FTP 59 | 60 | 61 | 62 | set cbr [new Application/Traffic/CBR] 63 | $cbr attach-agent $udp 64 | $cbr set type_ CBR 65 | $cbr set packet_size_ 1000 66 | $cbr set rate_ 1mb 67 | $cbr set random_ false 68 | 69 | $ns at 0.1 "$cbr start" 70 | $ns at 4.5 "$cbr stop" 71 | $ns at 1.0 "$ftp start" 72 | $ns at 2.0 "$ftp stop" 73 | $ns at 5.0 "finish" 74 | 75 | $ns run -------------------------------------------------------------------------------- /CN Lab/Week 11/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 11. 2 | -------------------------------------------------------------------------------- /CN Lab/Week 2/Example/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Example/client -------------------------------------------------------------------------------- /CN Lab/Week 2/Example/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() 9 | { 10 | int len,result,sockfd,n=1; 11 | struct sockaddr_in address; 12 | char ch[256],buf[256]; 13 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 14 | address.sin_family=AF_INET; 15 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 16 | address.sin_port=htons(10200); 17 | len = sizeof(address); 18 | result=connect(sockfd,(struct sockaddr *)&address,len); 19 | if(result==-1){ 20 | perror("\nCLIENT ERROR"); 21 | exit(1); 22 | } 23 | printf("\nENTER STRING\t"); 24 | gets(ch); 25 | ch[strlen(ch)]='\0'; 26 | write(sockfd,ch,strlen(ch)); 27 | printf("STRING SENT BACK FROM SERVER IS ....."); 28 | while(n){ 29 | n=read(sockfd,buf,sizeof(buf)); 30 | puts(buf); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CN Lab/Week 2/Example/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Example/server -------------------------------------------------------------------------------- /CN Lab/Week 2/Example/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define PORTNO 10200 7 | int main() 8 | { 9 | int sockfd,newsockfd,portno,clilen,n=1; 10 | char buf[256]; 11 | struct sockaddr_in seraddr,cliaddr; 12 | int i,value; 13 | sockfd = socket(AF_INET,SOCK_STREAM,0); 14 | seraddr.sin_family = AF_INET; 15 | seraddr.sin_addr.s_addr = INADDR_ANY; 16 | seraddr.sin_port = htons(PORTNO);bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 17 | listen(sockfd,1); 18 | clilen = sizeof(clilen); 19 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 20 | n = read(newsockfd,buf,sizeof(buf)); 21 | printf(" \nMessage from Client %s \n",buf); 22 | n = write(newsockfd,buf,sizeof(buf)); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /CN Lab/Week 2/Q1/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Q1/client -------------------------------------------------------------------------------- /CN Lab/Week 2/Q1/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() 9 | { 10 | int len,result,sockfd,n=1; 11 | struct sockaddr_in address; 12 | char time[500]; 13 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 14 | address.sin_family=AF_INET; 15 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 16 | address.sin_port=htons(12245); 17 | len = sizeof(address); 18 | result=connect(sockfd,(struct sockaddr *)&address,len); 19 | if(result==-1){ 20 | perror("\nCLIENT ERROR"); 21 | exit(1); 22 | } 23 | 24 | n=recv(sockfd,time,100,0); 25 | printf("Server time is : %s",time); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /CN Lab/Week 2/Q1/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Q1/server -------------------------------------------------------------------------------- /CN Lab/Week 2/Q1/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define PORTNO 12245 8 | 9 | void main() 10 | { 11 | 12 | // printf ( "Current local time and date: %s", asctime (localtime(&t)) ); 13 | 14 | int sockfd,newsockfd,portno,clilen,n=1; 15 | char * servertime; 16 | struct sockaddr_in seraddr,cliaddr; 17 | int i,value; 18 | sockfd = socket(AF_INET,SOCK_STREAM,0); 19 | seraddr.sin_family = AF_INET; 20 | seraddr.sin_addr.s_addr = INADDR_ANY; 21 | seraddr.sin_port = htons(PORTNO); 22 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 23 | 24 | //For Concurrent Server 25 | // while(1) 26 | // { 27 | time_t t; 28 | time(&t); 29 | listen(sockfd,1); 30 | clilen = sizeof(clilen); 31 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 32 | servertime = asctime (localtime(&t)); 33 | n = send(newsockfd,servertime,100,0); 34 | //} 35 | 36 | } -------------------------------------------------------------------------------- /CN Lab/Week 2/Q2/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Q2/client -------------------------------------------------------------------------------- /CN Lab/Week 2/Q2/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() 9 | { 10 | int len,result,sockfd,n=0,m=0; 11 | struct sockaddr_in address; 12 | char srvmsg[500]; 13 | char clntmsg[500]; 14 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 15 | address.sin_family=AF_INET; 16 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 17 | address.sin_port=htons(11111); 18 | len = sizeof(address); 19 | result=connect(sockfd,(struct sockaddr *)&address,len); 20 | if(result==-1){ 21 | perror("\nCLIENT ERROR"); 22 | exit(1); 23 | } 24 | 25 | 26 | printf("%s\n","Chat Start" ); 27 | 28 | 29 | do 30 | {printf("Client: "); 31 | gets(clntmsg); 32 | send(sockfd,clntmsg,100,0); 33 | if(strcmp(clntmsg,"QUIT")==0) 34 | break; 35 | n=recv(sockfd,srvmsg,100,0); 36 | printf("Server: %s\n",srvmsg ); 37 | if(strcmp(srvmsg,"QUIT")==0) 38 | break; 39 | 40 | }while(1); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /CN Lab/Week 2/Q2/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Q2/server -------------------------------------------------------------------------------- /CN Lab/Week 2/Q2/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define PORTNO 11111 8 | 9 | void main() 10 | { 11 | 12 | 13 | 14 | int sockfd,newsockfd,portno,clilen,n=1; 15 | 16 | struct sockaddr_in seraddr,cliaddr; 17 | int i,value; 18 | sockfd = socket(AF_INET,SOCK_STREAM,0); 19 | seraddr.sin_family = AF_INET; 20 | seraddr.sin_addr.s_addr = INADDR_ANY; 21 | seraddr.sin_port = htons(PORTNO); 22 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 23 | 24 | char srvmsg[500]; 25 | char clntmsg[500]; 26 | listen(sockfd,1); 27 | clilen = sizeof(clilen); 28 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 29 | 30 | printf("%s\n","Chat Start" ); 31 | 32 | 33 | do 34 | {n=recv(newsockfd,clntmsg,100,0); 35 | printf("Client: %s",clntmsg ); 36 | if(strcmp(clntmsg,"QUIT")==0) 37 | break; 38 | printf("\nServer: "); 39 | gets(srvmsg); 40 | send(newsockfd,srvmsg,100,0); 41 | if(strcmp(srvmsg,"QUIT")==0) 42 | break; 43 | }while(1); 44 | 45 | 46 | } -------------------------------------------------------------------------------- /CN Lab/Week 2/Q3/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Q3/client -------------------------------------------------------------------------------- /CN Lab/Week 2/Q3/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() 9 | { 10 | int len,result,sockfd,n=0,m=0; 11 | struct sockaddr_in address; 12 | 13 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 14 | address.sin_family=AF_INET; 15 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 16 | address.sin_port=htons(11130); 17 | len = sizeof(address); 18 | result=connect(sockfd,(struct sockaddr *)&address,len); 19 | if(result==-1){ 20 | perror("\nCLIENT ERROR"); 21 | exit(1); 22 | } 23 | 24 | 25 | char sentence[100]; 26 | printf("Sentence : "); 27 | gets(sentence); 28 | send(sockfd,sentence,100,0); 29 | char word[100]; 30 | printf("Word : "); 31 | gets(word); 32 | send(sockfd,word,100,0); 33 | char replacer[100]; 34 | printf("Replacing Word : "); 35 | gets(replacer); 36 | send(sockfd,replacer,100,0); 37 | char newsentence[100]; 38 | recv(sockfd,newsentence,100,0); 39 | printf("New Sentence : %s\n",newsentence); 40 | 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /CN Lab/Week 2/Q3/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 2/Q3/server -------------------------------------------------------------------------------- /CN Lab/Week 2/Q3/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define PORTNO 11130 8 | 9 | void main() 10 | { 11 | int sockfd,newsockfd,portno,clilen,n=1; 12 | 13 | struct sockaddr_in seraddr,cliaddr; 14 | sockfd = socket(AF_INET,SOCK_STREAM,0); 15 | seraddr.sin_family = AF_INET; 16 | seraddr.sin_addr.s_addr = INADDR_ANY; 17 | seraddr.sin_port = htons(PORTNO); 18 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 19 | 20 | 21 | listen(sockfd,1); 22 | clilen = sizeof(clilen); 23 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 24 | 25 | char sentence[100],word[100],replacer[100],newsentence[100]; 26 | recv(newsockfd,sentence,100,0); 27 | recv(newsockfd,word,100,0); 28 | recv(newsockfd,replacer,100,0); 29 | 30 | int len = strlen(replacer); 31 | int pos = strstr(sentence,word)-sentence; 32 | int i,j,k; 33 | int c=0; 34 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() 9 | { 10 | int len,result,sockfd,n=1; 11 | struct sockaddr_in address; 12 | char ch[256],buf[256]; 13 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 14 | address.sin_family=AF_INET; 15 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 16 | address.sin_port=htons(12270); 17 | len = sizeof(address); 18 | result=connect(sockfd,(struct sockaddr *)&address,len); 19 | if(result==-1){ 20 | perror("\nCLIENT ERROR"); 21 | exit(1); 22 | } 23 | printf("\nENTER STRING\t"); 24 | gets(ch); 25 | ch[strlen(ch)]='\0'; 26 | write(sockfd,ch,strlen(ch)); 27 | printf("STRING SENT BACK FROM SERVER IS ....."); 28 | while(n){ 29 | n=read(sockfd,buf,sizeof(buf)); 30 | puts(buf); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CN Lab/Week 3/Example/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 3/Example/server -------------------------------------------------------------------------------- /CN Lab/Week 3/Example/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define PORTNO 12270 8 | 9 | int main() 10 | { 11 | int sockfd, newsockfd, portno, clilen, n=1; 12 | char buf[256]; 13 | struct sockaddr_in seraddr, cliaddr; 14 | int i,value; 15 | sockfd = socket(AF_INET,SOCK_STREAM,0); 16 | seraddr.sin_family = AF_INET; 17 | seraddr.sin_addr.s_addr = INADDR_ANY; 18 | seraddr.sin_port = htons(PORTNO); 19 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 20 | listen(sockfd,1); 21 | clilen=sizeof(clilen); 22 | while(1) 23 | { 24 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 25 | if(fork()==0){ 26 | n=read(newsockfd,buf,sizeof(buf)); 27 | printf("\nMessage from Client %s \n",buf); 28 | n=write(newsockfd,buf,sizeof(buf)); 29 | } 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /CN Lab/Week 3/Q1/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 3/Q1/client -------------------------------------------------------------------------------- /CN Lab/Week 3/Q1/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | int main() 11 | { 12 | int sockfd,n=1; 13 | char buf[200]; 14 | struct sockaddr_in address; 15 | sockfd=socket(AF_INET,SOCK_STREAM,0); 16 | address.sin_family=AF_INET; 17 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 18 | address.sin_port=htons(11122); 19 | connect(sockfd,(struct sockaddr *)& address,sizeof(address)); 20 | fflush(stdout); 21 | int choice; 22 | char ch='a'; 23 | char file[256]="a"; 24 | FILE * fp; 25 | 26 | while(1) 27 | { strcpy(file,"a"); 28 | recv(sockfd,buf,200,0); 29 | printf("%s", buf ); 30 | printf("\nEnter choice: "); 31 | scanf("%d",&choice); 32 | send(sockfd,&choice,sizeof(int),0); 33 | switch(choice){ 34 | case 1: printf("Enter File Name: "); 35 | scanf("%s",file); 36 | printf("Downloading %s into \"newfile_c\"\n",file ); 37 | send(sockfd,file,256,0); 38 | fp=fopen("newfile_c","w"); 39 | int size,i=0,c=0; 40 | recv(sockfd,&size,sizeof(int),0); 41 | while(i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define PORTNO 11122 9 | 10 | void main() 11 | { 12 | int sockfd,newsockfd,clilen; 13 | char list[200]="1. Download\n2. Upload\n3. Display"; 14 | struct sockaddr_in seraddr,cliaddr; 15 | sockfd=socket(AF_INET,SOCK_STREAM,0); 16 | seraddr.sin_family=AF_INET; 17 | seraddr.sin_addr.s_addr=INADDR_ANY; 18 | seraddr.sin_port=htons(PORTNO); 19 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 20 | listen(sockfd,1); 21 | clilen=sizeof(clilen); 22 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 23 | int choice,c; 24 | char fname[256]="a"; 25 | char buf[200]; 26 | int end=-1; 27 | DIR * p; 28 | struct dirent *d; 29 | FILE * fp; 30 | while(1) 31 | { 32 | send(newsockfd,list,200,0); 33 | recv(newsockfd,&choice,sizeof(int),0); 34 | switch(choice) 35 | { 36 | case 1: recv(newsockfd,fname,256,0); 37 | fp = fopen(fname,"r"); 38 | if(fp) 39 | { 40 | fseek(fp, 0L, SEEK_END); 41 | int size = ftell(fp); 42 | send(newsockfd,&size,sizeof(int),0); 43 | rewind(fp); 44 | while ((c = fgetc(fp)) != EOF) 45 | send(newsockfd,&c,sizeof(int),0); 46 | fclose(fp); 47 | } 48 | else 49 | printf("File is not present\n"); 50 | break; 51 | case 2: fp=fopen("newfile_s","w"); 52 | int size,i=0,c=0; 53 | recv(newsockfd,&size,sizeof(int),0); 54 | while(id_name,256,0); 66 | send(newsockfd,"",256,0); 67 | break; 68 | default: exit(0); 69 | } 70 | } 71 | 72 | 73 | 74 | 75 | return; 76 | } 77 | -------------------------------------------------------------------------------- /CN Lab/Week 3/Q2/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 3/Q2/client -------------------------------------------------------------------------------- /CN Lab/Week 3/Q2/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | int main() 10 | { 11 | int len,result,sockfd,n=1; 12 | struct sockaddr_in address; 13 | char time[500]; 14 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 15 | address.sin_family=AF_INET; 16 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 17 | address.sin_port=htons(12241); 18 | len = sizeof(address); 19 | result=connect(sockfd,(struct sockaddr *)&address,len); 20 | if(result==-1){ 21 | perror("\nCLIENT ERROR"); 22 | exit(1); 23 | } 24 | 25 | n=recv(sockfd,time,100,0); 26 | printf("Server time is : %s",time); 27 | int pid; 28 | recv(sockfd,&pid,sizeof(int),0); 29 | printf("Pid is %d\n", pid ); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /CN Lab/Week 3/Q2/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 3/Q2/server -------------------------------------------------------------------------------- /CN Lab/Week 3/Q2/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define PORTNO 12241 9 | 10 | void main() 11 | { 12 | 13 | int sockfd,newsockfd,portno,clilen,n=1; 14 | char * servertime; 15 | struct sockaddr_in seraddr,cliaddr; 16 | int i,value; 17 | sockfd = socket(AF_INET,SOCK_STREAM,0); 18 | seraddr.sin_family = AF_INET; 19 | seraddr.sin_addr.s_addr = INADDR_ANY; 20 | seraddr.sin_port = htons(PORTNO); 21 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 22 | listen(sockfd,1); 23 | clilen = sizeof(clilen); 24 | while(1) 25 | { 26 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 27 | if(fork()==0){ 28 | int p=getpid(); 29 | printf("Pid is %d", p); 30 | time_t t; 31 | time(&t); 32 | servertime = asctime (localtime(&t)); 33 | printf("\n"); 34 | n = send(newsockfd,servertime,100,0); 35 | send(newsockfd,&p,sizeof(p),0); 36 | 37 | } 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /CN Lab/Week 3/Q3/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 3/Q3/client -------------------------------------------------------------------------------- /CN Lab/Week 3/Q3/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() 9 | { 10 | int len,result,sockfd,n=0,m=0; 11 | struct sockaddr_in address; 12 | 13 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 14 | address.sin_family=AF_INET; 15 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 16 | address.sin_port=htons(11138); 17 | len = sizeof(address); 18 | result=connect(sockfd,(struct sockaddr *)&address,len); 19 | if(result==-1){ 20 | perror("\nCLIENT ERROR"); 21 | exit(1); 22 | } 23 | 24 | 25 | char sentence[100]; 26 | printf("Sentence : "); 27 | gets(sentence); 28 | send(sockfd,sentence,100,0); 29 | char word[100]; 30 | printf("Word : "); 31 | gets(word); 32 | send(sockfd,word,100,0); 33 | char replacer[100]; 34 | printf("Replacing Word : "); 35 | gets(replacer); 36 | send(sockfd,replacer,100,0); 37 | char newsentence[100]; 38 | recv(sockfd,newsentence,100,0); 39 | printf("New Sentence : %s\n",newsentence); 40 | int pid; 41 | recv(sockfd,&pid,sizeof(int),0); 42 | printf("Pid is %d\n", pid); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /CN Lab/Week 3/Q3/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/CN Lab/Week 3/Q3/server -------------------------------------------------------------------------------- /CN Lab/Week 3/Q3/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define PORTNO 11138 8 | 9 | void main() 10 | { 11 | int sockfd,newsockfd,portno,clilen,n=1; 12 | 13 | struct sockaddr_in seraddr,cliaddr; 14 | sockfd = socket(AF_INET,SOCK_STREAM,0); 15 | seraddr.sin_family = AF_INET; 16 | seraddr.sin_addr.s_addr = INADDR_ANY; 17 | seraddr.sin_port = htons(PORTNO); 18 | bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); 19 | 20 | 21 | listen(sockfd,1); 22 | clilen = sizeof(clilen); 23 | while(1) 24 | { 25 | newsockfd=accept(sockfd,(struct sockaddr *)&cliaddr,&clilen); 26 | if(fork()==0){ 27 | char sentence[100],word[100],replacer[100],newsentence[100]; 28 | recv(newsockfd,sentence,100,0); 29 | recv(newsockfd,word,100,0); 30 | recv(newsockfd,replacer,100,0); 31 | 32 | int len = strlen(replacer); 33 | int pos = strstr(sentence,word)-sentence; 34 | int i,j,k; 35 | int c=0; 36 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | int sd, i, j; 13 | char buf[25], buf1[25]; 14 | struct sockaddr_in address; 15 | struct sockaddr_in sadd, cadd; 16 | sd=socket(AF_INET, SOCK_DGRAM, 0); 17 | address.sin_family=AF_INET; 18 | address.sin_addr.s_addr=inet_addr("127.0.0.1"); 19 | address.sin_port=htons(9704); 20 | printf("Enter integer1, integer2, and operator (space separated):\n"); 21 | scanf("%d %d %s", &i, &j, buf); 22 | int len=sizeof(address); 23 | char str1[25], str2[25]; 24 | sprintf(str1, "%d", i); 25 | sprintf(str2, "%d", j); 26 | int m= sendto(sd, str1, sizeof(str1), 0, (struct sockaddr*)&address, len); 27 | int a= sendto(sd, str2, sizeof(str2), 0, (struct sockaddr*)&address, len); 28 | int b= sendto(sd, buf, sizeof(buf), 0, (struct sockaddr*)&address, len); 29 | int n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&cadd, &len); 30 | printf("The operation yields:\n"); 31 | puts(buf); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CN Lab/Week 4/Q1/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | int main() 11 | { 12 | int sd, ans, i, j; 13 | char buf[25], str1[25], str2[25]; 14 | struct sockaddr_in sadd, cadd; 15 | sd=socket(AF_INET, SOCK_DGRAM, 0); 16 | sadd.sin_family=AF_INET; 17 | sadd.sin_addr.s_addr=inet_addr("127.0.0.1"); 18 | sadd.sin_port=htons(9704); 19 | int result=bind(sd, (struct sockaddr*)&sadd, sizeof(sadd)); 20 | int len=sizeof(cadd); 21 | int m= recvfrom(sd, str1, sizeof(buf), 0, (struct sockaddr*)&cadd, &len); 22 | int a= recvfrom(sd, str2, sizeof(buf), 0, (struct sockaddr*)&cadd, &len); 23 | int b= recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&cadd, &len); 24 | printf("The client sent:\n"); 25 | i=atoi(str1); 26 | j=atoi(str2); 27 | printf("%d %d %s", i, j, buf); 28 | 29 | if(buf[0]=='+') 30 | ans=i+j; 31 | else if(buf[0]=='-') 32 | ans=i-j; 33 | else if(buf[0]=='/') 34 | ans=i/j; 35 | else if(buf[0]=='*') 36 | ans=i*j; 37 | sprintf(buf, "%d", ans); 38 | 39 | 40 | int n = sendto(sd, buf, sizeof(buf), 0, (struct sockaddr*)&cadd, len); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /CN Lab/Week 4/README.md: -------------------------------------------------------------------------------- 1 | Adding Q1 of Week 4 2 | -------------------------------------------------------------------------------- /CN Lab/Week 5/p1_c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void main(){ 10 | 11 | int len,result,sockfd,n=1; 12 | struct sockaddr_in address; 13 | char ch[256],buf[256]; 14 | sockfd = socket(AF_INET,SOCK_STREAM,0); 15 | address.sin_family = AF_INET; 16 | address.sin_addr.s_addr = inet_addr("127.0.0.1"); 17 | address.sin_port = htons(9736); 18 | len = sizeof(address); 19 | result = connect(sockfd,(struct sockaddr *)&address,len); 20 | if(result==-1){ 21 | printf("Client Error\n"); 22 | exit(1); 23 | } 24 | printf("Send Time\n");; 25 | gets(ch); 26 | ch[strlen(ch)]='\0'; 27 | write(sockfd,ch,strlen(ch)); 28 | //printf("All uppercase \n"); 29 | while(n){ 30 | n=read(sockfd,buf,sizeof(buf)); 31 | puts(buf); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /CN Lab/Week 5/p1_s.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #define PORTNO 9736 10 | 11 | void main(){ 12 | int server_sockfd,client_sockfd; 13 | int server_len,client_len; 14 | struct sockaddr_in server_address,client_address; 15 | int result; 16 | fd_set readfds,testfds; 17 | server_sockfd = socket(AF_INET,SOCK_STREAM,0); 18 | server_address.sin_family=AF_INET; 19 | server_address.sin_addr.s_addr=htonl(INADDR_ANY); 20 | server_address.sin_port=htons(PORTNO); 21 | server_len = sizeof(server_address); 22 | bind(server_sockfd,(struct sockaddr *)&server_address,server_len); 23 | listen(server_sockfd,5); 24 | FD_ZERO(&readfds); 25 | FD_SET(server_sockfd,&readfds); 26 | while(1){ 27 | char ch; 28 | int fd; 29 | int nread; 30 | testfds = readfds; 31 | printf("server waiting\n"); 32 | result = select(FD_SETSIZE,&testfds,(fd_set *)0,(fd_set *)0,(struct timeval *)0); 33 | if(result<1){ 34 | perror("server 5"); 35 | exit(1); 36 | } 37 | int c=0; 38 | char buf[200],d[200]; 39 | for(fd=0;fd 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void main(){ 10 | int len,result,sockfd,n=1; 11 | struct sockaddr_in address; 12 | char ch[256],buf[256]; 13 | sockfd = socket(AF_INET,SOCK_STREAM,0); 14 | address.sin_family = AF_INET; 15 | address.sin_addr.s_addr = inet_addr("127.0.0.1"); 16 | address.sin_port = htons(9738); 17 | len = sizeof(address); 18 | result = connect(sockfd,(struct sockaddr *)&address,len); 19 | if(result==-1){ 20 | printf("Client Error\n"); 21 | exit(1); 22 | } 23 | printf("Enter string\n");; 24 | gets(ch); 25 | ch[strlen(ch)]='\0'; 26 | write(sockfd,ch,strlen(ch)); 27 | printf("All uppercase \n"); 28 | while(n){ 29 | n=read(sockfd,buf,sizeof(buf)); 30 | puts(buf); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CN Lab/Week 5/p2_s.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define PORTNO 9738 11 | 12 | void to_uppercase(char d[256]){ 13 | int len = strlen(d); 14 | for(int i=0;i=97&&d[i]<=122){ 16 | d[i]=d[i]-32; 17 | } 18 | } 19 | } 20 | 21 | void main(){ 22 | 23 | int server_sockfd,client_sockfd; 24 | int server_len,client_len; 25 | struct sockaddr_in server_address,client_address; 26 | int result; 27 | fd_set readfds,testfds; 28 | server_sockfd = socket(AF_INET,SOCK_STREAM,0); 29 | server_address.sin_family=AF_INET; 30 | server_address.sin_addr.s_addr=htonl(INADDR_ANY); 31 | server_address.sin_port=htons(PORTNO); 32 | server_len = sizeof(server_address); 33 | bind(server_sockfd,(struct sockaddr *)&server_address,server_len); 34 | listen(server_sockfd,5); 35 | FD_ZERO(&readfds); 36 | FD_SET(server_sockfd,&readfds); 37 | while(1){ 38 | char ch; 39 | int fd; 40 | int nread; 41 | testfds = readfds; 42 | printf("server waiting\n"); 43 | result = select(FD_SETSIZE,&testfds,(fd_set *)0,(fd_set *)0,(struct timeval *)0); 44 | if(result<1){ 45 | perror("server 5"); 46 | exit(1); 47 | } 48 | char buf[200]; 49 | for(fd=0;fd 2 | #include 3 | 4 | void main(int argc, char * argv[]) 5 | { 6 | FILE *fp; 7 | char fline[100]; 8 | char *newline; 9 | int i,count=0,occ=0; 10 | 11 | fp= fopen(argv[2],"r"); 12 | 13 | //reads till the EOF 14 | while(fgets(fline, 100, fp)!= NULL){ 15 | count++; 16 | if(newline = strchr(fline,'\n')) 17 | *newline ='\0'; 18 | if (strstr(fline,argv[1])!= NULL){ 19 | printf("%d%s\n",count, fline); 20 | occ++; 21 | } 22 | } 23 | 24 | printf("\nOccurence %d\n",occ); 25 | } -------------------------------------------------------------------------------- /OS Lab/Week 1/script1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #(i) 4 | mkdir ~/dir 5 | cd ~/dir 6 | mkdir sub1 sub2 7 | 8 | #(ii) 9 | cd sub1 10 | echo "content1" > axy 11 | echo "content2" > Abc 12 | echo "content2" > xyz 13 | 14 | #(iii) 15 | cp xyz ../sub2 16 | 17 | #(iv) 18 | echo -e "Users: `who| wc -l`\nFiles: `ls -l | grep "^-" | wc -l`" > ../sub2/number 19 | 20 | #(v) 21 | ls -p | grep -v \ | grep -E "^[aA]" 22 | 23 | #(vi) 24 | echo "No of files: `ls -l | grep "^-" | wc -l`" 25 | 26 | #(vii) 27 | cat < hello.c 28 | #include 29 | 30 | void main(){ 31 | printf("%s","Hello"); 32 | } 33 | DELIMITER 34 | gcc hello.c -o hello && ./hello -------------------------------------------------------------------------------- /OS Lab/Week 10/p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void first_fit ( int a[], int n, int p ) 5 | { 6 | int i, c = 0; 7 | for ( i = 0; i < n; i++ ) 8 | { 9 | if ( a[i] >= p ) 10 | { 11 | a[i]-= p; 12 | c = 1; 13 | break; 14 | } 15 | } 16 | if ( c == 0 ) 17 | { 18 | printf("\n No memory for process of size %d\n ", p ); 19 | } 20 | } 21 | 22 | void best_fit ( int a[], int n, int p ) 23 | { 24 | int i, c = 0, t = 0, sum = 999; 25 | for ( i = 0; i < n; i++ ) 26 | { 27 | if ( a[i] >= p ) 28 | { 29 | if ( sum > a[i] - p ) 30 | { 31 | sum = a[i] - p; 32 | t = i; 33 | c = 1; 34 | } 35 | } 36 | } 37 | if ( c == 1 ) 38 | { 39 | a[t]-= p; 40 | } 41 | else 42 | { 43 | printf( "\n No memory for process size %d\n ", p ); 44 | } 45 | } 46 | 47 | void worst_fit ( int a[], int n, int p ) 48 | { 49 | int i, c = 0, t = 0, sum = 0; 50 | for ( i = 0; i < n; i++ ) 51 | { 52 | if ( a[i] >= p ) 53 | { 54 | if ( sum < a[i] - p ) 55 | { 56 | sum = a[i] - p; 57 | t = i; 58 | c = 1; 59 | } 60 | } 61 | } 62 | if ( c == 1 ) 63 | { 64 | a[t]-= p; 65 | } 66 | else 67 | { 68 | printf( "\n No memory for process size %d\n ", p ); 69 | } 70 | } 71 | 72 | void main () 73 | { 74 | int i, n, l; 75 | printf( "\n Enter limit\n " ); 76 | scanf( "%d", &n ); 77 | printf( "\nEnter frame" ); 78 | int a[n], b[n], c[n]; 79 | for ( i = 0; i < n; i++ ) 80 | { 81 | scanf( "%d", &a[i] ); 82 | b[i] = c[i] = a[i]; 83 | } 84 | printf("\nEnter no of processes " ); 85 | scanf( "%d", &l ); 86 | int p[l]; 87 | printf(" \nEnter processes " ); 88 | for ( i = 0; i < l; i++ ) 89 | { 90 | scanf("%d", &p[i] ); 91 | } 92 | printf("First Fit\n"); 93 | for (i = 0; i < l; i++ ) 94 | { 95 | first_fit( a, n, p[i]); 96 | } 97 | for ( i = 0; i < n; i++ ) 98 | { 99 | printf( "%d\t", a[i] ); 100 | } 101 | printf( "\nBest Fit \n" ); 102 | for (i = 0; i < l; i++ ) 103 | { 104 | best_fit( b, n, p[i]); 105 | } 106 | for ( i = 0; i < n; i++ ) 107 | { 108 | printf( "%d\t", b[i] ); 109 | } 110 | printf( "\nWorst Fit\n" ); 111 | for (i = 0; i < l; i++ ) 112 | { 113 | worst_fit( c, n, p[i]); 114 | } 115 | for ( i = 0; i < n; i++ ) 116 | { 117 | printf( "%d\t", c[i] ); 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /OS Lab/Week 10/p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | int addr, p, offs, i, byte; 7 | printf("\nEnter byte size " ); 8 | scanf("%d", &byte); 9 | printf("\nEnter address " ); 10 | scanf("%d", &addr ); 11 | p = addr / byte; 12 | offs = addr % byte; 13 | printf( "Page Number : %d Offset : %d ",p,offs ); 14 | } -------------------------------------------------------------------------------- /OS Lab/Week 10/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 10. 2 | -------------------------------------------------------------------------------- /OS Lab/Week 11/p1_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define SIZE 3 5 | 6 | void main() 7 | { 8 | int a[20], b[SIZE], check[SIZE], i, j, f, k, index, n, min = 999, d; 9 | printf( "Enter length of page requests\n" ); 10 | scanf( "%d", &n ); 11 | printf( "Enter the requests\n" ); 12 | for ( i = 0; i < n; i++ ) 13 | { 14 | scanf( "%d", &a[i] ); 15 | } 16 | memset( b, -1, sizeof( b ) ); 17 | memset( check, 0, sizeof( check ) ); 18 | for ( i = 0; i < n; i++ ) 19 | { 20 | f = 0; 21 | d = 0; 22 | for ( k = 0; k < SIZE; k++ ) 23 | { 24 | if ( b[k] == -1 ) 25 | { 26 | b[k] = a[i]; 27 | printf( "\nPage %d is placed in frame %d \n", a[i], k ); 28 | check[k] = i; 29 | f = 1; 30 | break; 31 | } 32 | } 33 | if ( f == 0 ) 34 | { 35 | for ( k = 0; k < SIZE; k++ ) 36 | { 37 | if ( b[k] == a[i] ) 38 | { 39 | d = 1; 40 | break; 41 | } 42 | } 43 | } 44 | 45 | if ( f == 0 && d == 0 ) 46 | { 47 | min = 999; 48 | for ( k = 0; k < SIZE; k++ ) 49 | { 50 | if ( min > check[k] ) 51 | { 52 | min = check[k]; 53 | index = k; 54 | } 55 | } 56 | b[index] = a[i]; 57 | check[index] = i; 58 | printf( "\nPage %d is placed in frame %d\n", a[i], index ); 59 | } 60 | 61 | } 62 | } -------------------------------------------------------------------------------- /OS Lab/Week 11/p1_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int getpos(int a[],int n,int ele,int startpos){ 7 | for(int i=startpos;ib?a:b; 17 | } 18 | 19 | void optimal_cache(int a[],int n,int size){ 20 | int q[10]; 21 | int count=0; 22 | int j=0; 23 | memset(q,-1,sizeof(int)*size); 24 | for(int i=0;i 2 | #include 3 | #include 4 | #define SIZE 3 5 | 6 | void main() 7 | { 8 | int a[20], b[SIZE], check[SIZE], i, j, f, k, index, n, min = 999, d; 9 | printf( "Enter length of page requests\n" ); 10 | scanf( "%d", &n ); 11 | printf( "Enter the requests\n" ); 12 | for ( i = 0; i < n; i++ ) 13 | { 14 | scanf( "%d", &a[i] ); 15 | } 16 | memset( b, -1, sizeof( b ) ); 17 | memset( check, 0, sizeof( check ) ); 18 | for ( i = 0; i < n; i++ ) 19 | { 20 | f = 0; 21 | d = 0; 22 | for ( k = 0; k < SIZE; k++ ) 23 | { 24 | if ( b[k] == -1 ) 25 | { 26 | b[k] = a[i]; 27 | printf( "\nPage %d is placed in frame %d \n", a[i], k ); 28 | check[k] = i; 29 | f = 1; 30 | break; 31 | } 32 | } 33 | if ( f == 0 ) 34 | { 35 | for ( k = 0; k < SIZE; k++ ) 36 | { 37 | if ( b[k] == a[i] ) 38 | { 39 | check[k] = i; 40 | d = 1; 41 | break; 42 | } 43 | } 44 | } 45 | 46 | if ( f == 0 && d == 0 ) 47 | { 48 | min = 999; 49 | for ( k = 0; k < SIZE; k++ ) 50 | { 51 | if ( min > check[k] ) 52 | { 53 | min = check[k]; 54 | index = k; 55 | } 56 | } 57 | b[index] = a[i]; 58 | check[index] = i; 59 | printf( "\nPage %d is placed in frame %d\n", a[i], index ); 60 | } 61 | 62 | } 63 | } -------------------------------------------------------------------------------- /OS Lab/Week 11/readme.md: -------------------------------------------------------------------------------- 1 | codes for lab 11. 2 | -------------------------------------------------------------------------------- /OS Lab/Week 12/p1_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define start 53 4 | 5 | void main() 6 | { 7 | int a[20], i, n, mov = 0; 8 | printf( "Enter request size\n" ); 9 | scanf( "%d", &n ); 10 | printf( "Enter requests" ); 11 | for ( i = 0; i < n; i++ ) 12 | { 13 | scanf( "%d", &a[i] ); 14 | } 15 | printf( "Head starts at %d ", start ); 16 | printf( " \nHead moves to %d", a[0] ); 17 | for ( i = 1; i < n; i++ ) 18 | { 19 | mov += abs( a[i - 1] - a[i] ); 20 | printf( " \nHead moves to %d", a[i] ); 21 | } 22 | mov+= abs( start - a[0] ); 23 | printf( "\nThe total head movement is : %d tracks\n", mov ); 24 | } 25 | -------------------------------------------------------------------------------- /OS Lab/Week 12/p1_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define start 53 4 | 5 | void main() 6 | { 7 | int a[20], i, n, mov = 0, min = 1000, visited[20] = { 0 }, index, k = 0, cur = start; 8 | printf( "Enter request size\n" ); 9 | scanf( "%d", &n ); 10 | printf( "Enter requests" ); 11 | for ( i = 0; i < n; i++ ) 12 | { 13 | scanf( "%d", &a[i] ); 14 | } 15 | printf( "Head starts at %d ", start ); 16 | while ( k < n ) 17 | { 18 | for ( i = 0; i < n; i++ ) 19 | { 20 | if ( abs( cur - a[i] ) < min && visited[i] == 0 ) 21 | { 22 | min = abs( cur - a[i] ); 23 | index = i; 24 | } 25 | } 26 | mov+= min; 27 | visited[index] = 1; 28 | k++; 29 | min = 1000; 30 | cur = a[index]; 31 | printf( " \nHead moves to %d", cur ); 32 | } 33 | printf( "\nThe total head movement is : %d tracks\n", mov ); 34 | } 35 | -------------------------------------------------------------------------------- /OS Lab/Week 12/p1_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define start 53 4 | 5 | void main() 6 | { 7 | int a[20], b[20], c[20], l = 0, t, i, j, n, mov = 0, k = 0, cur = start; 8 | printf( "Enter request size\n" ); 9 | scanf( "%d", &n ); 10 | printf( "Enter requests" ); 11 | 12 | for ( i = 0; i < n; i++ ) 13 | { 14 | scanf( "%d", &a[i] ); 15 | if ( a[i] < start ) 16 | { 17 | b[k++] = a[i]; 18 | } 19 | else 20 | { 21 | c[l++] = a[i]; 22 | } 23 | } 24 | 25 | b[k++] = 0; 26 | for ( i = 0; i < k - 1; i++ ) 27 | { 28 | for ( j = 0; j < k - i - 1; j++ ) 29 | { 30 | if ( b[j] < b[j+1] ) 31 | { 32 | t = b[j]; 33 | b[j] = b[j+1]; 34 | b[j+1] = t; 35 | } 36 | } 37 | } 38 | for ( i = 0; i < l - 1; i++ ) 39 | { 40 | for ( j = 0; j < l - i - 1; j++ ) 41 | { 42 | if ( c[j] > c[j+1] ) 43 | { 44 | t = c[j]; 45 | c[j] = c[j+1]; 46 | c[j+1] = t; 47 | } 48 | } 49 | } 50 | printf( "Head starts at %d ", start ); 51 | printf( " \nHead moves to %d", b[0] ); 52 | for ( i = 1; i < k; i++ ) 53 | { 54 | mov+= abs( b[i] - b[i - 1] ); 55 | printf( " \nHead moves to %d", b[i] ); 56 | } 57 | mov+= abs( b[0] - start ); 58 | printf( " \nHead moves to %d", c[0] ); 59 | for ( i = 1; i < l; i++ ) 60 | { 61 | mov+= abs( c[i] - c[i - 1] ); 62 | printf( " \nHead moves to %d", c[i] ); 63 | } 64 | mov+= abs( b[k - 1] - c[0] ); 65 | 66 | printf( "\nThe total head movement is : %d tracks\n", mov ); 67 | } 68 | -------------------------------------------------------------------------------- /OS Lab/Week 12/p1_4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define start 53 4 | 5 | void sort ( int a[], int n ) 6 | { 7 | int i, j, t; 8 | for ( i = 0; i < n - 1; i++ ) 9 | { 10 | for ( j = 0; j < n - i - 1; j++ ) 11 | { 12 | if ( a[j] > a[j+1] ) 13 | { 14 | t = a[j]; 15 | a[j] = a[j+1]; 16 | a[j+1] = t; 17 | } 18 | } 19 | } 20 | } 21 | void main() 22 | { 23 | int a[20], b[20], c[20], l = 0, i, n, mov = 0, k = 0, cur = start; 24 | printf( "Enter request size\n" ); 25 | scanf( "%d", &n ); 26 | printf( "Enter requests" ); 27 | 28 | for ( i = 0; i < n; i++ ) 29 | { 30 | scanf( "%d", &a[i] ); 31 | if ( a[i] < start ) 32 | { 33 | b[k++] = a[i]; 34 | } 35 | else 36 | { 37 | c[l++] = a[i]; 38 | } 39 | } 40 | 41 | b[k++] = 0; 42 | c[l++] = 199; 43 | sort ( b, k ); 44 | sort ( c, l ); 45 | printf( "Head starts at %d ", start ); 46 | printf( " \nHead moves to %d", c[0] ); 47 | for ( i = 1; i < l; i++ ) 48 | { 49 | mov+= abs( c[i] - c[i - 1] ); 50 | printf( " \nHead moves to %d", c[i] ); 51 | } 52 | mov+= abs( c[0] - start ); 53 | printf( " \nHead moves to %d", b[0] ); 54 | for ( i = 1; i < k; i++ ) 55 | { 56 | mov+= abs( b[i] - b[i - 1] ); 57 | printf( " \nHead moves to %d", b[i] ); 58 | } 59 | printf( "\nThe total head movement is : %d tracks\n", mov ); 60 | } 61 | -------------------------------------------------------------------------------- /OS Lab/Week 12/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 12. 2 | -------------------------------------------------------------------------------- /OS Lab/Week 2/q1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd $1 4 | ls *.? 5 | -------------------------------------------------------------------------------- /OS Lab/Week 2/q2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd $1 4 | ls | grep -E $2 5 | 6 | 7 | or 8 | 9 | 10 | cd $1 11 | ls $2 12 | 13 | 14 | NOTE: Enter format of file in quotes 15 | -------------------------------------------------------------------------------- /OS Lab/Week 2/q3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "No of users: `who|wc -l`" -------------------------------------------------------------------------------- /OS Lab/Week 2/q4.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "No of files: `ls -p | grep -v / | wc -l`" 4 | -------------------------------------------------------------------------------- /OS Lab/Week 2/q5.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "The date today is:" `date+ %d`-`date +%m`-`date +%y` 4 | -------------------------------------------------------------------------------- /OS Lab/Week 2/q6.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cp *.$1 $2 -------------------------------------------------------------------------------- /OS Lab/Week 3/p1.sh: -------------------------------------------------------------------------------- 1 | echo `ls -p -S $1 | grep -v / | head -1` 2 | -------------------------------------------------------------------------------- /OS Lab/Week 3/p2.sh: -------------------------------------------------------------------------------- 1 | cd $1 2 | rm $(ls -p -r -t| grep -v /| head -1) 3 | -------------------------------------------------------------------------------- /OS Lab/Week 3/p3.sh: -------------------------------------------------------------------------------- 1 | for files in $1* 2 | do 3 | t=0 4 | while read input 5 | do 6 | if [ "$input" == "$files" ] 7 | then 8 | t=1 9 | fi 10 | done <"TOC.txt" 11 | if [ $t -eq 0 ] 12 | then 13 | echo $files>>"TOC.txt" 14 | fi 15 | done 16 | 17 | 18 | #contributed by Ayush Garg 19 | -------------------------------------------------------------------------------- /OS Lab/Week 3/p4.sh: -------------------------------------------------------------------------------- 1 | cat f1.txt f2.txt > "sort.txt" 2 | x=`sort ./sort.txt -u -n` 3 | echo $x>sort.txt 4 | 5 | -------------------------------------------------------------------------------- /OS Lab/Week 3/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 3 2 | -------------------------------------------------------------------------------- /OS Lab/Week 4/copyfile: -------------------------------------------------------------------------------- 1 | hello world -------------------------------------------------------------------------------- /OS Lab/Week 4/newfile: -------------------------------------------------------------------------------- 1 | hello world -------------------------------------------------------------------------------- /OS Lab/Week 4/orphan: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/orphan -------------------------------------------------------------------------------- /OS Lab/Week 4/orphan.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | pid_t pid=fork(); 10 | switch(pid) 11 | { 12 | case -1:perror("Fork Failed"); 13 | exit(1); 14 | case 0: printf("Parent pid (Child)\t%d\n",getppid()); 15 | printf("Child sleeping for 10s\n"); 16 | sleep(10); 17 | printf("Parent pid (Child)\t%d\n",getppid()); 18 | printf("Child terminated\n"); 19 | exit(0); 20 | default:printf("Parent sleeps for 5s\n"); 21 | sleep(5); 22 | printf("Parent terminated\n"); 23 | exit(0); 24 | } 25 | } -------------------------------------------------------------------------------- /OS Lab/Week 4/prg1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/prg1 -------------------------------------------------------------------------------- /OS Lab/Week 4/prg1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | int status; 10 | pid_t pid; 11 | pid=fork(); 12 | switch(pid) 13 | { 14 | case -1:perror("Fork failed"); 15 | exit(1); 16 | case 0: printf("Child sleeping for 10s\n"); 17 | sleep(10); 18 | printf("Child Terminated\n"); 19 | exit(0); 20 | default:printf("Parent process\n"); 21 | printf("Waiting ...\n"); 22 | wait(&status); 23 | printf("Child returned: %d\n",status); 24 | printf("Parent Terminating\n"); 25 | exit(0); 26 | } 27 | } -------------------------------------------------------------------------------- /OS Lab/Week 4/prg2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/prg2 -------------------------------------------------------------------------------- /OS Lab/Week 4/prg2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | pid_t pid; 10 | pid=fork(); 11 | switch(pid) 12 | { 13 | case -1:perror("Fork Failed"); 14 | exit(1); 15 | case 0: printf("Child replaced with prg1\n"); 16 | execl("prg1","prg1",NULL); 17 | exit(0); 18 | default:printf("Parent of prg2\n"); 19 | wait(NULL); 20 | exit(0); 21 | } 22 | } -------------------------------------------------------------------------------- /OS Lab/Week 4/prg3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/prg3 -------------------------------------------------------------------------------- /OS Lab/Week 4/prg3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | pid_t pid=fork(); 10 | switch(pid) 11 | { 12 | case -1:perror("Fork failed\n"); 13 | exit(1); 14 | case 0: printf("Parent pid (Child):\t%d\n",getppid()); 15 | printf("Pid (Child):\t\t%d\n",getpid()); 16 | printf("Child pid (Child):\t%d\n",pid); 17 | exit(0); 18 | default:printf("Parent pid (Parent):\t%d\n",getppid()); //prints pid of shell 19 | printf("Pid (Parent):\t\t%d\n",getpid()); 20 | printf("Child pid (Parent):\t%d\n",pid); 21 | exit(0); 22 | } 23 | } -------------------------------------------------------------------------------- /OS Lab/Week 4/prg4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/prg4 -------------------------------------------------------------------------------- /OS Lab/Week 4/prg4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | pid_t pid=fork(); 10 | switch(pid) 11 | { 12 | case -1:perror("Fork Failed"); 13 | exit(1); 14 | case 0: printf("Child sleeping for 5s\n"); 15 | sleep(5); 16 | printf("Child terminated\n"); 17 | exit(0); 18 | default:printf("Parent sleeps for 10s\n"); 19 | sleep(10); 20 | printf("Parent terminated\n"); 21 | exit(0); 22 | } 23 | } -------------------------------------------------------------------------------- /OS Lab/Week 4/prg5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/prg5 -------------------------------------------------------------------------------- /OS Lab/Week 4/prg5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main() 7 | { 8 | int fd=creat("newfile",444); 9 | char buf[50]; 10 | printf("Enter message\n"); 11 | gets(buf); 12 | write(fd,buf,strlen(buf)); 13 | } -------------------------------------------------------------------------------- /OS Lab/Week 4/prg6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MITCSE2019/Sem5/6803e47d4a3fcf2ef4afc2b0919992e9d205f20b/OS Lab/Week 4/prg6 -------------------------------------------------------------------------------- /OS Lab/Week 4/prg6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | int fd=open("newfile",444); 10 | int len=lseek(fd,0,SEEK_END); 11 | lseek(fd,-len,SEEK_CUR); 12 | char * buf = (char *)malloc(len); 13 | int fd2=creat("copyfile",444); 14 | read(fd,buf,len); 15 | write(fd2,buf,len); 16 | } -------------------------------------------------------------------------------- /OS Lab/Week 5/README.md: -------------------------------------------------------------------------------- 1 | Contains Week 5 code 2 | -------------------------------------------------------------------------------- /OS Lab/Week 5/p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int* fib_child( int* param ) 6 | { 7 | int *f = (int*)malloc(100 * sizeof(int)); 8 | f[0] = 0; 9 | f[1] = 1; 10 | //int a = (int)param; 11 | int a=*param; 12 | int i; 13 | //print("%d\t %d\t", f[0],f[1] ); 14 | for ( i = 2; i <= a; i++ ) 15 | { 16 | f[i] = f[i - 1] + f[i - 2]; 17 | } 18 | return f; 19 | } 20 | 21 | void main () 22 | { 23 | int n,i,*f; 24 | printf("\nEnter limit\n"); 25 | pthread_t thread; 26 | scanf ("%d", &n ); 27 | pthread_create ( &thread, 0, &fib_child, &n ); 28 | pthread_join(thread,&f); 29 | printf("\n The result is : "); 30 | for ( i = 0; i < n; i++ ) 31 | { 32 | printf("%d\t",f[i]); 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /OS Lab/Week 5/p2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int* summ_child( int* param ) 6 | { 7 | int a = (int)param; 8 | int i, sum = 0; 9 | for ( i = 0; a[i] != -1; i++ ) 10 | { 11 | sum+= a[i]; 12 | } 13 | return sum; 14 | } 15 | 16 | void main () 17 | { 18 | int f[100]; 19 | int n,i; 20 | int *sum; 21 | printf("\nEnter limit\n"); 22 | pthread_t thread; 23 | scanf ("%d", &n ); 24 | printf("Enter numbers :\n" ); 25 | for ( i = 0; i < n; i++ ) 26 | { 27 | scanf("%d", &f[i] ); 28 | } 29 | f[i]=-1; 30 | pthread_create ( &thread, 0, &summ_child, f ); 31 | pthread_join(thread,&sum ); 32 | printf("The result is : %d", sum ); 33 | } 34 | -------------------------------------------------------------------------------- /OS Lab/Week 5/p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | 7 | void* prime_child( int* param ) 8 | { 9 | 10 | int a, b, k = 0, f[100]; 11 | a = param[0]; 12 | b = param[1]; 13 | int i, j,check = 0, c; 14 | for ( i = a; i <= b; i++ ) 15 | { 16 | c = i; 17 | for ( j = 2; j <= i/2; j++ ) 18 | { 19 | if ( c % j == 0 ) 20 | { 21 | check = 1; 22 | break; 23 | } 24 | } 25 | if ( check == 0 ) 26 | { 27 | f[k++] = i; 28 | } 29 | check = 0; 30 | } 31 | printf("\n The result is :"); 32 | for ( i = 0; i < k; i++ ) 33 | { 34 | if ( f[i] != 1 ) 35 | { 36 | printf("%d\t", f[i] ); 37 | } 38 | } 39 | 40 | } 41 | 42 | void main () 43 | { 44 | int n,i, arr[20]; 45 | printf("\nEnter limits\n"); 46 | pthread_t thread; 47 | scanf ("%d %d", &arr[0], &arr[1] ); 48 | pthread_create ( &thread, 0, &prime_child, arr ); 49 | pthread_join(thread,0 ); 50 | 51 | } 52 | -------------------------------------------------------------------------------- /OS Lab/Week 5/q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int arr[100]; 4 | int i, k=2; 5 | void* child_thread(void *params) 6 | { 7 | int up= atoi(params); 8 | int a, b, c; 9 | arr[0]=arr[1]=1; 10 | a=arr[0]; 11 | b=arr[1]; 12 | while(up-2!=0) 13 | { 14 | c=a+b; 15 | arr[k]=c; 16 | a=arr[k-1]; 17 | b=arr[k]; 18 | up--; 19 | k++; 20 | } 21 | pthread_exit(0); 22 | } 23 | 24 | int main(int argc, char *argv[]) 25 | { 26 | pthread_t thread; 27 | pthread_attr_t attributes; 28 | pthread_attr_init(&attributes); 29 | pthread_create(&thread, &attributes, child_thread, argv[1]); 30 | pthread_join(thread, NULL); 31 | printf("The Fibonacci series is as follows:\n"); 32 | for(i=0; i 2 | #include 3 | #include 4 | int sum; 5 | void* child_thread(void *param) 6 | { 7 | int *a=(int*)param; 8 | int i, len=a[0]; 9 | sum=0; 10 | //printf("%d\n", len); 11 | for(i=1; i<=a[0]; i++) 12 | { 13 | //printf("%d\n", a[i]); 14 | sum+=a[i]; 15 | } 16 | pthread_exit(0); 17 | } 18 | int main(int argc, char *argv[]) 19 | { 20 | pthread_t thread; 21 | int i, n, a; 22 | printf("Enter no. of elements: "); 23 | scanf("%d", &n); 24 | int arr[n+1]; 25 | arr[0]=n; 26 | printf("Enter elements: \n"); 27 | for(i=1; i<=n; i++) 28 | { 29 | scanf("%d", &a); 30 | arr[i]=a; 31 | } 32 | pthread_attr_t attributes; 33 | pthread_attr_init(&attributes); 34 | int return_value; 35 | pthread_create(&thread, &attributes, child_thread, (void*)arr); 36 | pthread_join(thread, NULL); 37 | printf("Sum is %d\n", sum); 38 | } -------------------------------------------------------------------------------- /OS Lab/Week 5/q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int count; 5 | void* child_thread(void *param) 6 | { 7 | int i, j; 8 | int* arr=(int*)(param); 9 | //printf("Upper limit is %d\n", upper); 10 | for(i=arr[0]; i<=arr[1]; i++) 11 | { 12 | count=0; 13 | for(j=1; j<=i; j++) 14 | { 15 | if(i%j==0) 16 | count++; 17 | } 18 | if(count==2) 19 | printf("%d ", i); 20 | } 21 | printf("\n"); 22 | pthread_exit(0); 23 | } 24 | int main(int argc, char *argv[]) 25 | { 26 | int arr[2], i, a; 27 | printf("Enter Lower and Upper limits:\n"); 28 | for(i=0; i<2; i++) 29 | { 30 | scanf("%d", &a); 31 | arr[i]=a; 32 | } 33 | pthread_t thread; 34 | pthread_attr_t attributes; 35 | pthread_attr_init(&attributes); 36 | pthread_create(&thread, &attributes, child_thread, (void *)arr); 37 | pthread_join(thread, NULL); 38 | } -------------------------------------------------------------------------------- /OS Lab/Week 6/p1_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int n; //for no of processes 6 | 7 | typedef struct proc { 8 | int num; 9 | int ar_t; 10 | int br_t; 11 | int pri; 12 | }pr; 13 | 14 | 15 | void sort ( pr a[n] ) 16 | { 17 | for ( int i = 0; i < n - 1; i++ ) 18 | { 19 | for ( int j = 0; j < n - i - 1; j++ ) 20 | { 21 | if ( a[j].ar_t > a[j+1].ar_t ) 22 | { 23 | pr c; 24 | c = a[j]; 25 | a[j] = a[j+1]; 26 | a[j+1] = c; 27 | } 28 | } 29 | } 30 | } 31 | 32 | void print ( pr a[n], int tat[n], int wait[n] ) 33 | { 34 | for ( int i = 0; i < n; i++ ) 35 | { 36 | printf( "%d TAT : %d WAIT : %d \t", a[i].num, tat[i], wait[i] ); 37 | //printf( "\t" ); 38 | } 39 | } 40 | 41 | void comp ( pr a[n], int comp[n] ) 42 | { 43 | int sum = 0; 44 | for ( int i = 0; i < n; i++ ) 45 | { 46 | comp[i] = sum + a[i].br_t; 47 | sum+= a[i].br_t; 48 | } 49 | } 50 | void turn_wait ( int tat[n], int wait[n], int comp[n], pr a[n] ) 51 | { 52 | for ( int i = 0; i < n; i++ ) 53 | { 54 | tat[i] = comp[i] - a[i].ar_t; 55 | wait[i] = tat[i] - a[i].br_t; 56 | } 57 | } 58 | void main () 59 | { 60 | printf( "Enter no of processes\n" ); 61 | scanf( "%d", &n ); 62 | pr a[n]; 63 | int wait[20] ={ 0 }; 64 | int tat[20] = { 0 }; 65 | int com[20] = { 0 }; 66 | int i; 67 | float av_wait, av_tat; 68 | printf("Enter process name, arrival time, burst time and priority"); 69 | for ( i = 0; i < n; i++ ) 70 | { 71 | printf("\nProcess %d :\t", i + 1 ); 72 | scanf( "%d", &a[i].num ); 73 | scanf( "%d", &a[i].ar_t ); 74 | scanf( "%d", &a[i].br_t ); 75 | scanf( "%d", &a[i].pri ); 76 | } 77 | printf("\nThe result is : "); 78 | sort ( a ); 79 | comp ( a, com ); 80 | turn_wait ( tat, wait, com, a ); 81 | print ( a, tat, wait ); 82 | for ( i = 0; i < n; i++ ) 83 | { 84 | av_tat = av_tat + tat[i]; 85 | av_wait = av_wait + wait[i]; 86 | } 87 | av_wait = (float)av_wait/n; 88 | av_tat = (float)av_tat/n; 89 | printf(" \nThe avg times are : \nTAT : %f \nWAIT : %f ", av_tat, av_wait ); 90 | 91 | } 92 | -------------------------------------------------------------------------------- /OS Lab/Week 6/p1_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct p { 6 | int pid; 7 | int arr_t; 8 | int bur_t; 9 | int pri; 10 | }P; 11 | int n; 12 | 13 | int min_burst(int burst[n][3],int cur_time) 14 | { 15 | int index=0; 16 | int min_burst=burst[index][2]; 17 | for(int i=1;i=burst[i][1] && min_burst>burst[i][2] && burst[i][2]) 19 | { 20 | index=i; 21 | min_burst=burst[i][2]; 22 | } 23 | return index; 24 | } 25 | 26 | void sort(P pr[n]) 27 | { 28 | for(int i=0;i pr[j+1].arr_t) 31 | { 32 | P t = pr[j]; 33 | pr[j] = pr[j+1]; 34 | pr[j+1]=t; 35 | } 36 | } 37 | void main() 38 | { 39 | printf("Enter number of process: "); 40 | scanf("%d",&n); 41 | P pr[n]; 42 | int total_time=0,burst[n][3]; 43 | printf("Enter Burst Chart\n"); 44 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | typedef struct p { 6 | int pid; 7 | int arr_t; 8 | int bur_t; 9 | int pri; 10 | }P; 11 | int n,max_pri=0; 12 | 13 | int high_pr(P pr[n],int cur_time,int flag[n]) 14 | { 15 | int index=0; 16 | int high_pr=max_pri; 17 | for(int i=0;i=pr[i].arr_t && high_pr>=pr[i].pri && !flag[i]) 19 | { 20 | index=i; 21 | high_pr=pr[i].pri; 22 | } 23 | flag[index]=1; 24 | return index; 25 | } 26 | 27 | void sort(P pr[n]) 28 | { 29 | for(int i=0;i pr[j+1].arr_t) 32 | { 33 | P t = pr[j]; 34 | pr[j] = pr[j+1]; 35 | pr[j+1]=t; 36 | } 37 | } 38 | void main() 39 | { 40 | printf("Enter number of process: "); 41 | scanf("%d",&n); 42 | P pr[n]; 43 | int total_time=0; 44 | printf("Enter Burst Chart\n"); 45 | for(int i=0;imax_pri) 50 | max_pri=pr[i].pri; 51 | } 52 | sort(pr); 53 | int flag[n],gantt[n][3]; 54 | memset(flag,0,sizeof(flag)); 55 | int cur_time=0; 56 | int i=0; 57 | printf("Gantt Chart\n"); 58 | while(total_time-cur_time) 59 | { 60 | int index=high_pr(pr,cur_time,flag); 61 | printf("%d %d %d\n",pr[gantt[i++][0]=index].pid,gantt[i][1]=cur_time-pr[index].bur_t,gantt[i][2]=cur_time+=pr[index].bur_t); 62 | } 63 | 64 | int time[n][3]; 65 | int index=0; 66 | float avg_wt=0,avg_tt=0; 67 | printf("%s\n", "Waiting and Turnaround Time" ); 68 | for(int i=0;i 2 | #include 3 | 4 | typedef struct rr{ 5 | int pid; 6 | int AT; 7 | int BT; 8 | }pr; 9 | void sort ( pr a[],int n ){ 10 | for ( int i = 0; i < n-1; i++ ){ 11 | for ( int j = 0; j a[j+1].AT ){ 13 | pr c; 14 | c = a[j]; 15 | a[j] = a[j+1]; 16 | a[j+1] = c; 17 | } 18 | } 19 | } 20 | } 21 | void round(pr arr[],int n,int total,pr arr1[], int quan){ 22 | int x=1;//index for array...to determine when a process should enter the queue 23 | int completion_time[100]; 24 | for(int i=0;i=arr[x].AT){ 43 | q[++r]=arr[x]; 44 | x++; 45 | }else 46 | break; 47 | } 48 | pr y; 49 | if(q[f].pid==-1) 50 | break; 51 | if(q[f].BT>quan){ 52 | q[f].BT=q[f].BT-quan; 53 | y=q[f]; 54 | f++; 55 | while(x=arr[x].AT){ 57 | q[++r]=arr[x]; 58 | x++; 59 | }else 60 | break; 61 | } 62 | q[++r]=y; 63 | i=i+quan; 64 | }else{ 65 | while(x=arr[x].AT){ 67 | q[++r]=arr[x]; 68 | x++; 69 | }else 70 | break; 71 | } 72 | i=i+q[f].BT; 73 | completion_time[q[f].pid-1]=i; 74 | f++; 75 | } 76 | printf("p%d (%d-%d)\n",id,j,i ); 77 | } 78 | printf("Completion time :\n"); 79 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int pfd[2]; 10 | pid_t cpid; 11 | char buf; 12 | if(pipe(pfd)==-1) 13 | { 14 | perror("pipe"); 15 | exit("EXIT_FAILURE"); 16 | } 17 | cpid=fork(); 18 | if(cpid==0) 19 | { 20 | close(pfd[1]); 21 | while(read(pfd[0],&buf,1)>0) 22 | write(STDOUT_FILENO,&buf,1); 23 | write(STDOUT_FILENO,"\n",1); 24 | close(pfd[0]); 25 | exit(EXIT_SUCCESS); 26 | } 27 | else 28 | { 29 | close(pfd[0]); 30 | printf("writer please enter the value\n"); 31 | char buf[200]; 32 | scanf("%s",buf); 33 | write(pfd[1],buf,strlen(buf)); 34 | close(pfd[1]); 35 | wait(NULL); 36 | exit(EXIT_SUCCESS); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /OS Lab/Week 7/p3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef struct shared_use_st 10 | { 11 | char c[20]; 12 | }ss; 13 | 14 | void main ( int d, char * a[] ) 15 | { 16 | pid_t pid; 17 | int status; 18 | void * shared_memory = ( void * )0; 19 | ss *stuff; 20 | int shmid; 21 | char q; 22 | srand( ( unsigned int) getpid() ); 23 | shmid = shmget( ( key_t)1234, sizeof( struct shared_use_st ), 0666 | IPC_CREAT ); 24 | if ( shmid == -1 ) 25 | { 26 | fprintf( stderr, "shmget failure\n" ); 27 | exit( EXIT_FAILURE ); 28 | } 29 | shared_memory = shmat( shmid, ( void * )0, 0 ); 30 | 31 | if ( shared_memory == ( void * )-1 ) 32 | { 33 | fprintf( stderr,"shmat failed\n" ); 34 | exit( EXIT_FAILURE ); 35 | } 36 | 37 | 38 | stuff = ( struct shared_use_st * )shared_memory; 39 | printf( "Enter a character\n" ); 40 | scanf( " %c", &q ); 41 | stuff -> c[0] = q; 42 | 43 | pid = fork(); 44 | if ( pid == -1 ) 45 | { 46 | fprintf( stderr, "Fork Failed" ); 47 | exit ( -1 ); 48 | } 49 | else if ( pid == 0 ) 50 | { 51 | if ( stuff -> c[0] == 'z' ) 52 | { 53 | stuff -> c[0] = 'a'; 54 | exit ( 0 ); 55 | } 56 | else if (stuff -> c[0] == 'Z' ) 57 | { 58 | stuff -> c[0] = 'A'; 59 | exit ( 0 ); 60 | } 61 | ( stuff -> c[0] )++; 62 | exit ( 0 ); 63 | } 64 | else 65 | { 66 | wait ( &status ); 67 | puts( stuff -> c ); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /OS Lab/Week 7/q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | //char buf[]="HELLO\0"; 10 | int buf[4]; 11 | int main(){ 12 | int fd; 13 | char *myfifo="new"; 14 | int res=mkfifo(myfifo,0666); 15 | if(res==0) 16 | printf("file created"); 17 | else 18 | printf("failed"); 19 | fd=open(myfifo,O_RDWR); 20 | printf("%d ",fd); 21 | printf(" Enter 4 numbers"); 22 | for(int i=0;i<4;i++){ 23 | scanf("%d",&buf[i]); 24 | } 25 | //buf[5]='\0'; 26 | res=write(fd,buf,sizeof(int)*4); 27 | printf("%d ",res); 28 | close(fd); 29 | unlink(myfifo); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /OS Lab/Week 7/q2r.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | //char buf[]="HELLO\0"; 10 | int buf[4]; 11 | int main(){ 12 | int fd; 13 | char *myfifo="new"; 14 | //int res=mkfifo(myfifo,0666); 15 | //if(res==0) 16 | // printf("file created"); 17 | //else 18 | // printf("failed"); 19 | //fd=open(myfifo,O_RDWR); 20 | //printf("%d ",fd); 21 | //printf(" Enter 4 numbers"); 22 | fd=open(myfifo,O_RDONLY); 23 | read(fd,buf,sizeof(int)*4); 24 | for(int i=0;i<4;i++){ 25 | printf("%d",buf[i]); 26 | } 27 | //res=write(fd,buf,sizeof(buf)); 28 | close(fd); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /OS Lab/Week 7/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 7. 2 | Codes for question 2 and 4 can be found in manual page 96-97 and 92-93 respectively. 3 | -------------------------------------------------------------------------------- /OS Lab/Week 8/p.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define bufferBound 10 5 | sem_t semaphore,bound; 6 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 7 | int queue[bufferBound]; 8 | int queueLength; 9 | void *producer( void * param) 10 | { 11 | for ( int i=0; i<50; i++ ) 12 | { 13 | // Add item to queue 14 | sem_wait(&bound); 15 | pthread_mutex_lock( &mutex ); 16 | queue[ queueLength++ ] = i; 17 | printf("Sent %d\n", i); 18 | pthread_mutex_unlock( &mutex ); 19 | // Signal semaphore 20 | sem_post( &semaphore ); 21 | } 22 | } 23 | void *consumer(void * param) 24 | { 25 | for ( int i=0; i<50; i++ ) 26 | { 27 | int item; 28 | // Wait if nothing in queue 29 | sem_wait(&semaphore); 30 | pthread_mutex_lock( &mutex ); 31 | item = queue[ -- queueLength ]; 32 | printf("Received %i\n", item); 33 | pthread_mutex_unlock( &mutex ); 34 | 35 | sem_post(&bound); 36 | } 37 | } 38 | int main() 39 | { 40 | pthread_t threads[2]; 41 | sem_init( &semaphore, 0, 0 ); 42 | sem_init( &bound,0,bufferBound); 43 | pthread_create( &threads[0], 0, producer, 0 ); 44 | pthread_create( &threads[1], 0, consumer, 0 ); 45 | pthread_join( threads[0], 0 ); 46 | pthread_join( threads[1], 0 ); 47 | sem_destroy( &semaphore ); 48 | } 49 | -------------------------------------------------------------------------------- /OS Lab/Week 8/readme.md: -------------------------------------------------------------------------------- 1 | Codes for lab 8 2 | -------------------------------------------------------------------------------- /OS Lab/Week 9/p1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct alloc 5 | { 6 | int a,b,c; 7 | }alloc; 8 | typedef struct max 9 | { 10 | int a,b,c; 11 | }max; 12 | typedef struct avail 13 | { 14 | int a,b,c; 15 | }avail; 16 | typedef struct need 17 | { 18 | int a,b,c; 19 | }needy; 20 | void main( ) 21 | { 22 | int n,i,j; 23 | printf( "Enter number of processes\n" ); 24 | scanf("%d",&n); 25 | alloc al[n]; 26 | max m[n]; 27 | avail av; 28 | needy need[n]; 29 | printf("\n Enter allocation and max for each process\n" ); 30 | for( i = 0; i < n;i++ ) 31 | { 32 | scanf(" %d %d %d ",&al[i].a,&al[i].b,&al[i].c ); 33 | scanf(" %d %d %d ",&m[i].a,&m[i].b,&m[i].c ); 34 | need[i].a = m[i].a - al[i].a; 35 | need[i].b = m[i].b - al[i].b; 36 | need[i].c = m[i].c - al[i].c; 37 | 38 | } 39 | printf( "\nEnter availability array\n" ); 40 | scanf("%d%d%d",&av.a,&av.b,&av.c); 41 | printf("\n\n"); 42 | for( i = 0; i < n;i++ ) 43 | { 44 | printf( "%d\t%d\t%d\n",need[i].a,need[i].b,need[i].c ); 45 | } 46 | 47 | int count=0; 48 | int flag=0; 49 | int visited[n]; 50 | memset( visited, 0, sizeof(visited) ); 51 | for( i = 0; i < n; i = (i+1) % n ) 52 | { 53 | j++; 54 | if( need[i].a <= av.a && need[i].b <= av.b && need[i].c <= av.c && visited[i] == 0 ) 55 | { 56 | visited[i]=1; 57 | printf( "Process %d\t",i ); 58 | count++; 59 | av.a+= al[i].a; 60 | av.b+= al[i].b; 61 | av.c+= al[i].c; 62 | } 63 | if( count == n ) 64 | { 65 | flag = 1; 66 | break; 67 | } 68 | if( j == 3 * n ) 69 | { 70 | flag = 0; 71 | break; 72 | } 73 | } 74 | if( flag == 0) 75 | printf( "\nDeadlock\n" ); 76 | } -------------------------------------------------------------------------------- /OS Lab/Week 9/prg2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int m,n; 6 | 7 | int requestCheck(int request[n][m],int alloc[n][m],int work[m],int i) 8 | { 9 | int allow=1,count=0; 10 | for(int j=0;jwork[j]) 18 | allow=0; 19 | return allow; 20 | } 21 | 22 | void add(int work[m],int alloc[n][m],int i) 23 | { 24 | for(int j=0;j