slip.1

1.
#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults, 
 sp,m,n,count[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_lfu(int sp) 
int i,min_i,min=9999; 
 i=sp; 
do
 { 
 if(count[i]<min) 
 { 
 min = count[i]; 
 min_i = i; 
 } 
 i=(i+1)%n; 
 }while(i!=sp); 
return min_i; 
void lfu() 
int i,j,k; 
for(i=0;i<m && sp<n;i++) 
 { 
 k=search(ref[i]); 
 if(k==-1) 
 { 
frames[sp]=ref[i]; 
 count[sp]++; 
 faults++; 
 sp++; 
 for(j=0;j<n;j++) 
 mem[j][i]=frames[j]; 
 } 
 else
 count[k]++; 
 
 } 
 sp=0; 
for(;i<m;i++) 
 { 
 k = search(ref[i]); 
 if(k==-1) 
 { 
 sp = get_lfu(sp); 
 frames[sp] = ref[i]; 
 count[sp]=1; 
 faults++; 
 sp = (sp+1)%n; 
 for(j=0;j<n;j++) 
 mem[j][i] = frames[j]; 
 } 
 else
 count[k]++; 
 } 
 
int main() 
accept(); 
lfu(); 
disp(); 
return 0; 
}

2.
#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;
}
while(read(fh, &c, 1) > 0) {
printf("%c", c);
}
}
close(fh);
}
int main() {
char buff[80],
*args[10];
while(1) {
printf ("\n");
printf("\nmyshell$ ");
fgets(buff, 80, stdin);
buff[strlen(buff)-1] = '\0';
int n = make_toks(buff, args);
switch (n) {
case 1:
if(strcmp(args[0], "exit") == 0)
exit(1);
if (!fork())
execlp (args [0], args[0], NULL);
break;
case 2:
if (!fork ())
execlp (args [0], args[0], args[1], NULL);
break;
case 3:
if (strcmp(args[0], "typeline") == 0)
typeline (args[1], args[2]);
else {
if (!fork ())
execlp (args [0], args[0], args[1], args[2], NULL);
}
break;
case 4:
if (!fork ())
execlp (args [0], args [0], args [1], args [2], args [3], NULL);
break;
}
}
return 0;
}
o/p:
create text file after compile for text file type vim text.txt insert data in text file press esc
:wq now run your program using ./a.out
myshell$ typeline a text.txt
pune
kolkata
doremon
mumbai
vadapav
chandigarh
pune
prisonbreak
pogo
misalpav
gogo
pune
\0
myshell$ typeline 3 text.txt
pune
kolkata
doremon
myshell$ typeline -5 text.txt
pogo
misalpav
gogo
pune

Comments

Popular posts from this blog

slip.15

slip.24

slip.23