Posts

slip.7

1........ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void make_toks(char *s, char *tok[]) { int i=0; char *p; p = strtok(s," "); while(p!=NULL) { tok[i++]=p; p=strtok(NULL," "); } tok[i]=NULL; } void search(char *fn, char op, char *pattern) { int fh,count=0,i=0,j=0; char buff[255],c,*p; fh = open(fn,O_RDONLY); if(fh==-1) { printf("File %s Not Found\n",fn); return; } switch(op) { case 'f': while(read(fh,&c,1)) { buff[j++]=c; if(c=='\n') { buff[j]='\0'; j=0; i++; if(strstr(buff,pattern)) { printf("%d: %s",i,buff); break; } } } break; case 'c': while(read(fh,&c,1)) { buff[j++]=c; if(c=='\n') { buff[j]='\0'; j=0; p = buff; while(p=strstr(p,pattern)) { count++; p++; } } } printf("Total No.of Occurrences = %d\n",count); break; case 'a': while(read(fh,&c,1)) { bu...

slip.24

1......... #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void make_toks(char *s, char *tok[]) { int i=0; char *p; p = strtok(s," "); while(p!=NULL) { tok[i++]=p; p=strtok(NULL," "); } tok[i]=NULL; } void count(char *fn, char op) { int fh,cc=0,wc=0,lc=0; char c; fh = open(fn,O_RDONLY); if(fh==-1) { printf("File %s not found.\n",fn); return; } while(read(fh,&c,1)>0) { if(c==' ') wc++; else if(c=='\n') { wc++; lc++; } cc++; } close(fh); switch(op) { case 'c': printf("No.of characters:%d\n",cc-1); break; case 'w': printf("No.of words:%d\n",wc); break; case 'l': printf("No.of lines:%d\n",lc+1); break; } } int main() { char buff[80],*args[10]; int pid; while(1) { printf("myshell$ "); fflush(stdin); fgets(buff,80,stdin); buff[strlen(buff)-1]='\0'; make_toks(b...

slip.23

1........ #include<stdio.h> int main() { int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0; printf("Enter number of frames: "); scanf("%d", &no_of_frames); printf("Enter number of pages: "); scanf("%d", &no_of_pages); printf("Enter page reference string: "); for(i = 0; i < no_of_pages; ++i){ scanf("%d", &pages[i]); } for(i = 0; i < no_of_frames; ++i){ frames[i] = -1; } for(i = 0; i < no_of_pages; ++i){ flag1 = flag2 = 0; for(j = 0; j < no_of_frames; ++j){ if(frames[j] == pages[i]){ flag1 = flag2 = 1; break; } } if(flag1 == 0){ for(j = 0; j < no_of_frames; ++j){ if(frames[j] == -1){ faults++; frames[j] = pages[i]; flag2 = 1; break; } } } if(flag2 == 0){ flag3 =0; for(j = 0; j < no_of_frames; ++j){ temp[j] = -1; for(k = i + 1; k < no_of_pages; ++k){ if(frames[j] == pages[k]){ temp[j] = k; break; } } } for(j = 0; j < no_of_f...

slip.22

1....... #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct process_info { char pname[20]; int at,bt,ct,bt1,p; struct process_info *next; }NODE; int n; NODE *first,*last; void accept_info() { NODE *p; int i; printf("Enter no.of process:"); scanf("%d",&n); for(i=0;i<n;i++) { p = (NODE*)malloc(sizeof(NODE)); printf("Enter process name:"); scanf("%s",p->pname); printf("Enter arrival time:"); scanf("%d",&p->at); printf("Enter first CPU burst time:"); scanf("%d",&p->bt); printf("Enter priority:"); scanf("%d",&p->p); p->bt1 = p->bt; p->next = NULL; if(first==NULL) first=p; else last->next=p; last = p; } } void print_output() { NODE *p; float avg_tat=0,avg_wt=0; printf("pname\tat\tbt\tp\ttct\ttat\twt\n"); p = first; while(p!=NULL) { int tat = p->ct-p->at; int wt = tat-p->bt; avg_tat+=tat; avg_wt+...

slip.21

1.......... #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { // fork() Create a child process int pid = fork(); if (pid > 0) { printf("I am Parent process\n"); printf("ID : %d\n\n", getpid()); } else if (pid == 0) { printf("I am Child process\n"); printf("ID: %d\n", getpid()); } else { printf("Failed to create child process"); } return 0; } 2...... #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct process_info { char pname[20]; int at,bt,ct,bt1,p; struct process_info *next; }NODE; int n; NODE *first,*last; void accept_info() { NODE *p; int i; printf("Enter no.of process:"); scanf("%d",&n); for(i=0;i<n;i++) { p = (NODE*)malloc(sizeof(NODE)); printf("Enter process name:"); scanf("%s",p->pname); printf("Enter arrival time:"); scanf("%d",&p->at); printf("Enter first CPU burst...

slip.20

1....... #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> int make_toks(char *s, char *tok[]) { int i = 0; char *p; p = strtok(s, " "); while(p != NULL) { tok[i++] = p; p = strtok(NULL, " "); } tok[i] = NULL; return i; } void typeline(char *op, char *fn) { int fh, i, j, n; char c; fh = open(fn, O_RDONLY); if(fh == -1) { printf("File %s not found.\n", fn); return; } if(strcmp(op, "a") == 0) { while(read(fh, &c, 1) > 0) printf("%c", c); close(fh); return; } n = atoi(op); if(n > 0) { i = 0; while(read(fh, &c, 1) > 0) { printf("%c", c); if(c == '\n') i++; if(i == n) break; } } if(n < 0) { i = 0; while(read(fh, &c, 1) > 0) { if(c == '\n') i++; } lseek(fh, 0, SEEK_SET); j = 0; while(read(fh, &c, 1) > 0) { if(c == '\n') j++; if(j == i+n+1) br...

slip.19

1...... #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <dirent.h> void make_toks(char *s, char *tok[]) { int i=0; char *p; p = strtok(s," "); while(p!=NULL) { tok[i++]=p; p=strtok(NULL," "); } tok[i]=NULL; } void list(char *dn, char op) { DIR *dp; struct dirent *entry; int dc=0,fc=0; dp = opendir(dn); if(dp==NULL) { printf("Dir %s not found.\n",dn); return; } switch(op) { case 'f': while(entry=readdir(dp)) { if(entry->d_type==DT_REG) printf("%s\n",entry->d_name); } break; case 'n': while(entry=readdir(dp)) { if(entry->d_type==DT_DIR) dc++; if(entry->d_type==DT_REG) fc++; } printf("%d Dir(s)\t%d File(s)\n",dc,fc); break; case 'i': while(entry=readdir(dp)) { if(entry->d_type==DT_REG) printf("%s\t%d\n",entry->d_name,entry->d_fileno); } } closedir(dp); } int mai...

slip.18

1. #include<stdio.h> #define MAX 20 int frames[MAX],ref[MAX],mem[MAX][MAX],faults, sp,m,n,time[MAX]; void accept() { int i; printf("Enter no.of frames:"); scanf("%d", &n); printf("Enter no.of references:"); scanf("%d", &m); printf("Enter reference string:\n"); for(i=0;i<m;i++) { printf("[%d]=",i); scanf("%d",&ref[i]); } } void disp() { int i,j; for(i=0;i<m;i++) printf("%3d",ref[i]); printf("\n\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(mem[i][j]) printf("%3d",mem[i][j]); else printf(" "); } printf("\n"); } printf("Total Page Faults: %d\n",faults); } int search(int pno) { int i; for(i=0;i<n;i++) { if(frames[i]==pno) return i; } return -1; } int get_lru() { int i,min_i,min=9999; for(i=0;i<n;i++) { if(time[i]<min) { min = time[i]; min_i = i; } } return min_i; } void lru() { int i,j,k; for(i=0;i<m && sp<n;i...

slip.17

1........... #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct process_info { char pname[20]; int at,bt,ct,bt1; struct process_info *next; }NODE; int n; NODE *first,*last; void accept_info() { NODE *p; int i; printf("Enter no.of process:"); scanf("%d",&n); for(i=0;i<n;i++) { p = (NODE*)malloc(sizeof(NODE)); printf("Enter process name:"); scanf("%s",p->pname); printf("Enter arrival time:"); scanf("%d",&p->at); printf("Enter first CPU burst time:"); scanf("%d",&p->bt); p->bt1 = p->bt; p->next = NULL; if(first==NULL) first=p; else last->next=p; last = p; } } void print_output() { NODE *p; float avg_tat=0,avg_wt=0; printf("pname\tat\tbt\tct\ttat\twt\n"); p = first; while(p!=NULL) { int tat = p->ct-p->at; int wt = tat-p->bt; avg_tat+=tat; avg_wt+=wt; printf("%s\t%d\t%d\t%d\t%d\t%d\n", p->pname,p->at,p-...

slip.16

1. ....... #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void make_toks(char *s, char *tok[]) { int i=0; char *p; p = strtok(s," "); while(p!=NULL) { tok[i++]=p; p=strtok(NULL," "); } tok[i]=NULL; } void count(char *fn, char op) { int fh,cc=0,wc=0,lc=0; char c; fh = open(fn,O_RDONLY); if(fh==-1) { printf("File %s not found.\n",fn); return; } while(read(fh,&c,1)>0) { if(c==' ') wc++; else if(c=='\n') { wc++; lc++; } cc++; } close(fh); switch(op) { case 'c': printf("No.of characters:%d\n",cc-1); break; case 'w': printf("No.of words:%d\n",wc); break; case 'l': printf("No.of lines:%d\n",lc+1); break; } } int main() { char buff[80],*args[10]; int pid; while(1) { printf("myshell$ "); fflush(stdin); fgets(buff,80,stdin); buff[strlen(buff)-1]='\0'; make_toks(b...

slip.15

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <dirent.h> void make_toks(char *s, char *tok[]) { int i=0; char *p; p = strtok(s," "); while(p!=NULL) { tok[i++]=p; p=strtok(NULL," "); } tok[i]=NULL; } void list(char *dn, char op) { DIR *dp; struct dirent *entry; int dc=0,fc=0; dp = opendir(dn); if(dp==NULL) { printf("Dir %s not found.\n",dn); return; } switch(op) { case 'f': while(entry=readdir(dp)) { if(entry->d_type==DT_REG) printf("%s\n",entry->d_name); } break; case 'n': while(entry=readdir(dp)) { if(entry->d_type==DT_DIR) dc++; if(entry->d_type==DT_REG) fc++; } printf("%d Dir(s)\t%d File(s)\n",dc,fc); break; case 'i': while(entry=readdir(dp)) { if(entry->d_type==DT_REG) printf("%s\t%d\n",entry->d_name,entry->d_fileno); } } closedir(dp); } int main() { ch...

slip.14

1...... #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> int make_toks(char *s, char *tok[]) { int i = 0; char *p; p = strtok(s, " "); while(p != NULL) { tok[i++] = p; p = strtok(NULL, " "); } tok[i] = NULL; return i; } void typeline(char *op, char *fn) { int fh, i, j, n; char c; fh = open(fn, O_RDONLY); if(fh == -1) { printf("File %s not found.\n", fn); return; } if(strcmp(op, "a") == 0) { while(read(fh, &c, 1) > 0) printf("%c", c); close(fh); return; } n = atoi(op); if(n > 0) { i = 0; while(read(fh, &c, 1) > 0) { printf("%c", c); if(c == '\n') i++; if(i == n) break; } } if(n < 0) { i = 0; while(read(fh, &c, 1) > 0) { if(c == '\n') i++; } lseek(fh, 0, SEEK_SET); j = 0; while(read(fh, &c, 1) > 0) { if(c == '\n') j++; if(j == i+n+1) bre...

slip.13

#include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> int make_toks(char *s, char *tok[]) { int i = 0; char *p; p = strtok(s, " "); while(p != NULL) { tok[i++] = p; p = strtok(NULL, " "); } tok[i] = NULL; return i; } void typeline(char *op, char *fn) { int fh, i, j, n; char c; fh = open(fn, O_RDONLY); if(fh == -1) { printf("File %s not found.\n", fn); return; } if(strcmp(op, "a") == 0) { while(read(fh, &c, 1) > 0) printf("%c", c); close(fh); return; } n = atoi(op); if(n > 0) { i = 0; while(read(fh, &c, 1) > 0) { printf("%c", c); if(c == '\n') i++; if(i == n) break; } } if(n < 0) { i = 0; while(read(fh, &c, 1) > 0) { if(c == '\n') i++; } lseek(fh, 0, SEEK_SET); j = 0; while(read(fh, &c, 1) > 0) { if(c == '\n') j++; if(j == i+n+1) break; } wh...

slio.12

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <dirent.h> void make_toks(char *s, char *tok[]) { int i=0; char *p; p = strtok(s," "); while(p!=NULL) { tok[i++]=p; p=strtok(NULL," "); } tok[i]=NULL; } void list(char *dn, char op) { DIR *dp; struct dirent *entry; int dc=0,fc=0; dp = opendir(dn); if(dp==NULL) { printf("Dir %s not found.\n",dn); return; } switch(op) { case 'f': while(entry=readdir(dp)) { if(entry->d_type==DT_REG) printf("%s\n",entry->d_name); } break; case 'n': while(entry=readdir(dp)) { if(entry->d_type==DT_DIR) dc++; if(entry->d_type==DT_REG) fc++; } printf("%d Dir(s)\t%d File(s)\n",dc,fc); break; case 'i': while(entry=readdir(dp)) { if(entry->d_type==DT_REG) printf("%s\t%d\n",entry->d_name,entry->d_fileno); } } closedir(dp); } int main() { ch...

slip.10

1....... #include<stdio.h> #define MAX 20 int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n; void accept() { int i; printf("Enter no.of frames:"); scanf("%d", &n); printf("Enter no.of references:"); scanf("%d", &m); printf("Enter reference string:\n"); for(i=0;i<m;i++) { printf("[%d]=",i); scanf("%d",&ref[i]); } } void disp() { int i,j; for(i=0;i<m;i++) printf("%3d",ref[i]); printf("\n\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(mem[i][j]) printf("%3d",mem[i][j]); else printf(" "); } printf("\n"); } printf("Total Page Faults: %d\n",faults); } int search(int pno) { int i; for(i=0;i<n;i++) { if(frames[i]==pno) return i; } return -1; } void fifo() { int i,j; for(i=0;i<m;i++) { if(search(ref[i])==-1) { frames[sp] = ref[i]; sp = (sp+1)%n; faults++; for(j=0;j<n;j++) mem[j][i] = frames[j]; } } } int main() { accept(); fif...

slip.9

1........ #include<stdio.h> #define MAX 20 int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n; void accept() { int i; printf("Enter no.of frames:"); scanf("%d", &n); printf("Enter no.of references:"); scanf("%d", &m); printf("Enter reference string:\n"); for(i=0;i<m;i++) { printf("[%d]=",i); scanf("%d",&ref[i]); } } void disp() { int i,j; for(i=0;i<m;i++) printf("%3d",ref[i]); printf("\n\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(mem[i][j]) printf("%3d",mem[i][j]); else printf(" "); } printf("\n"); } printf("Total Page Faults: %d\n",faults); } int search(int pno) { int i; for(i=0;i<n;i++) { if(frames[i]==pno) return i; } return -1; } void fifo() { int i,j; for(i=0;i<m;i++) { if(search(ref[i])==-1) { frames[sp] = ref[i]; sp = (sp+1)%n; faults++; for(j=0;j<n;j++) mem[j][i] = frames[j]; } } } int main() { accept(); fi...

slip.8

1...... #include<stdio.h> #define MAX 20 int frames[MAX],ref[MAX],mem[MAX][MAX],faults, sp,m,n,time[MAX]; void accept() { int i; printf("Enter no.of frames:"); scanf("%d", &n); printf("Enter no.of references:"); scanf("%d", &m); printf("Enter reference string:\n"); for(i=0;i<m;i++) { printf("[%d]=",i); scanf("%d",&ref[i]); } } void disp() { int i,j; for(i=0;i<m;i++) printf("%3d",ref[i]); printf("\n\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(mem[i][j]) printf("%3d",mem[i][j]); else printf(" "); } printf("\n"); } printf("Total Page Faults: %d\n",faults); } int search(int pno) { int i; for(i=0;i<n;i++) { if(frames[i]==pno) return i; } return -1; } int get_lru() { int i,min_i,min=9999; for(i=0;i<n;i++) { if(time[i]<min) { min = time[i]; min_i = i; } } return min_i; } void lru() { int i,j,k; for(i=0;i<m && sp...