48
Ex.No : 1a //PROGRAM USING PROCESS SYSTEM CALLS #include<stdio.h> void main(int argc,char *argv[]) { int pid; pid=fork(); if(pid<0) { fprintf(stderr,"Fork Failed"); exit(-1); } else if(pid==0) { execlp("/bin/ls","ls",NULL); } else { wait(NULL); printf("child complete"); exit(0); } } OUTPUT : [2k9cs003@oxfordoec ~]$ cc call.c [2k9cs003@oxfordoec ~]$ ./a.out aaaa.c dir.c fork.c opwrite.c sema.c akshay.txt fac.c free.c priority.c semaphore aksmi fact.c getpid.c producer.c sjf.c alospace.c fcfs.c grep.c rename.c stat.c

OS Lab Prg.(2010-2011) NEW-UG

Embed Size (px)

Citation preview

Page 1: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 1a//PROGRAM USING PROCESS SYSTEM CALLS

#include<stdio.h>void main(int argc,char *argv[]){ int pid; pid=fork();

if(pid<0) { fprintf(stderr,"Fork Failed"); exit(-1); } else if(pid==0) { execlp("/bin/ls","ls",NULL); } else { wait(NULL); printf("child complete"); exit(0); }}

OUTPUT :

[2k9cs003@oxfordoec ~]$ cc call.c [2k9cs003@oxfordoec ~]$ ./a.outaaaa.c dir.c fork.c opwrite.c sema.cakshay.txt fac.c free.c priority.c semaphoreaksmi fact.c getpid.c producer.c sjf.calospace.c fcfs.c grep.c rename.c stat.ca.out fib.c HOSTNAME=localhost.localdomain reopen.c wc.cbankers.c fifo.c ipc.c rewrite.ccall.c file.c memoryscheme.c rorobin.ccat.c firstfit.c opendir.c round.cconfifile.c fopen.c oprewrite.c samp.cchild complete

Page 2: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :1b//FORK,GETPID,GETPPID,EXIT

#include<stdio.h>#include<sys/types.h>int main(){ pid_t pid; printf("\n\n\t Before Fork\n"); pid=fork(); if(pid<0) { printf("\n Parent_ID\t:%d,\n PPID\t\t:%d\nchild_ID\t:%d\n",getpid(),getppid(),pid); } else if(pid==0) { printf("\nchild_pid\t:%d\nppid\t\t:%d\n",getpid(),getppid()); } else { printf("\n\t Fork Error\n"); exit(1); } printf("\n Both Process Continuous From Here\n"); exit(0); }OUTPUT :[2k9cs003@oxfordoec ~]$ cc getpid.c [2k9cs003@oxfordoec ~]$ ./a.out

Before Fork

child_pid :4606ppid :4605

Fork Error

Both Process Continuous From Here

Page 3: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :1c//STAT SYSTEM CALLS

#include<stdio.h>#include<sys/stat.h>main(){ int fd; char pathl[10]; struct stat *nfile; nfile=(struct stat*)malloc(sizeof(struct stat)); printf("\n Enter The Filename:\t"); scanf("%s",pathl); stat(pathl,nfile); printf("Program inodeno:%d\n",nfile->st_ino); printf("Program size:%d\n",nfile->st_blksize); printf("Access Time:%s\n",ctime(&nfile->st_atime)); printf("Modified Time:%s\n",ctime(&nfile->st_mtime)); printf("Protection:%d\n",nfile->st_mode); printf("Userid:%d\n",nfile->st_uid); printf("Groupid:%d\n",nfile->st_gid); printf("Device Driver No:%d\n",nfile->st_dev); printf("No Of Links:%d\n",nfile->st_nlink); return 0;}

OUTPUT : [2k9cs003@oxfordoec ~]$ cc stat.c [2k9cs003@oxfordoec ~]$ ./a.out

Enter The Filename: aksmiProgram inodeno:15274483Program size:4096Access Time:Fri Jan 28 09:58:27 2011

Modified Time:Tue Jan 4 16:32:51 2011

Protection:16877Userid:1122Groupid:100Device Driver No:64768No Of Links:2

Page 4: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 1d//OPENDIR,READDIR SYSTEM CALLS

#include<stdio.h>#include<dirent.h>main(int argc,char *argv[]){ DIR *dir; struct dirent *direntry; dir=opendir(argv[1]); while((direntry=readdir(dir))!=NULL) printf("%d%s\n",direntry->d_ino,direntry->d_name); closedir(dir);}

OUTPUT :[2k9cs003@oxfordoec ~]$ cc reopen.c [2k9cs003@oxfordoec ~]$ ./a.out aksmi15270184..15274490laksmi15274502guna15274286sanju15274483.

Page 5: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :2a //OPEN,READ,WRITE SYSTEM CALLS

#include<stdio.h>main(int argc,char *argv[]){ int f,d,n; char buff[2048]; if(argc!=3) { printf("Argument Not Matched"); } f=creat(argv[2],0777); d=open(argv[1],0); while((n=read(d,buff,sizeof(buff)))>0) write(f,buff,n);}

OUTPUT : [2k9cs003@oxfordoec ~]$ cc opwrite.c[2k9cs003@oxfordoec ~]$ ./a.out reopen.c akshay.txt[2k9cs003@oxfordoec ~]$ cat akshay.txt#include<stdio.h>#include<dirent.h>main(int argc,char *argv[]){ DIR *dir; struct dirent *direntry; dir=opendir(argv[1]); while((direntry=readdir(dir))!=NULL) printf("%d%s\n",direntry->d_ino,direntry->d_name); closedir(dir);}

Page 6: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 2b//FOPEN AND FCLOSE SYSTEM CALLS

#include<stdio.h>main (int argc,char *argv[]){ int ch,nc=0; FILE *fp; fp=fopen(argv[1],”r”); while((ch=getc(fp))!=EOF) if(ch=argv[2][0]) nc++; printf(“\n Number Of Occurrence %d”,nc); exit(0); fclose(fp);}

OUTPUT :[2k9cs003@oxfordoec ~]$ cc ffopen.c [2k9cs003@oxfordoec ~]$ ./a.out fork.c a Number Of Occurrence 172 [2k9cs003@oxfordoec ~]$ cat fork.c#include<stdio.h>#include<unistd.h>#include<sys/types.h>main(){ int pid; pid=fork(); printf(“Hai\n”); if(pid==0) execl(“/home/2k9cs003/samp.c”,”samp.c”,NULL);}

Page 7: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 3a//GREP IN UNIX COMMANDS

#include<string.h>#include<stdio.h>main(int argc,char *argv[]){ FILE *fp; int n,j,t,q,m=0,i=80,rd=0; char ch[50],msg[50],temp[50]; fp=fopen(argv[2],"r"); m=strlen(argv[1]); if(argc==3) { while(!feof(fp)) { fgets(msg,i,fp); q=strlen(msg); for(n=0;n<q-m;n++) { for(j=0,t=n;j<m;j++) { temp[j]=msg[t++]; } temp[j]='\0'; if(strcmp(temp,argv[1])==0) { if(feof(fp)) goto e; rd=1; } } if(rd==1) { printf(msg); rd=0; } } e: fclose(fp);}}

Page 8: OS Lab Prg.(2010-2011) NEW-UG

OUTPUT :[2k9cs003@oxfordoec ~]$ cc grep.c[2k9cs003@oxfordoec ~]$ ./a.out msg grep.c char ch[50],msg[50],temp[50]; fgets(msg,i,fp); q=strlen(msg); temp[j]=msg[t++]; printf(msg);

Page 9: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :3b//WC COMMAND IMPLEMENTATION

#include<stdio.h>main(int argc,char *argv[]){ int ch,nc=0,chrc=0,lc=0,wc=0; FILE *fp; fp=fopen(argv[1],"r"); while((ch=getc(fp))!=EOF) { chrc++; if(ch==' '||ch=='\n'||ch=='\t') { wc++; chrc--; } if(ch=='\n') lc++; } printf("\nlines:%d words:%d characters:%d",lc,wc,chrc);}

OUTPUT :[2k9cs003@oxfordoec ~]$ cc wc.c[2k9cs003@oxfordoec ~]$ ./a.out grep.c

lines:38 words:129 characters:389

Page 10: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 3c//CAT IN UNIX COMMANDS

#include<stdio.h>#include<string.h>main(int argc,char *argv[]){ int f,d,n=0; char b[1000]; if(argc>3) { printf("arguments not matched"); exit(0); } else { if(argc>2) { f=creat(argv[2],0777); while(1) { scanf("%c",&b[n++]); if(b[n-1]=='E') break; } write(f,b,n); } if(argc<=2) { f=open(argv[1],0); n=read(f,b,sizeof(b)); for(d=0;d<n;d++) printf("%c",b[d]); } }}

Page 11: OS Lab Prg.(2010-2011) NEW-UG

OUTPUT :[2k9cs003@oxfordoec ~]$ cc cat.c [2k9cs003@oxfordoec ~]$ ./a.out > aaaa.c[2k9cs003@oxfordoec ~]$ lsaaaa.c dir.c fork.c oprewrite.c reopen.cakshay.txt fac.c free.c opwrite.c sema.c[2k9cs003@oxfordoec ~]$ ./a.out reopen.c#include<stdio.h>#include<dirent.h>main(int argc,char *argv[]){ DIR *dir; struct dirent *direntry; dir=opendir(argv[1]); while((direntry=readdir(dir))!=NULL) printf("%d%s\n",direntry->d_ino,direntry->d_name); closedir(dir);}

Page 12: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 4a//FCFS SCHEDULING ALGORITHM

#include<stdio.h>main(){ int i,n; int p[10],bt[10]; int wt[10],tat[10]; float ttat=0,twt=0,avgtat,avgwt; printf("Enter The No.Of Process:\n"); scanf("%d",&n); for(i=1;i<=n;i++) { p[i]=i; printf("Enter the burst time of p[%d]:",i); scanf("%d",&bt[i]); } wt[1]=0; tat[1]=bt[1]; for(i=2;i<=n;i++) { wt[i]=wt[i-1]+bt[i-1]; tat[i]=tat[i-1]+bt[i]; } for(i=1;i<=n;i++) { twt+=wt[i]; ttat+=tat[i]; } avgwt=twt/n; avgtat=ttat/n; printf("total turn around time%f\n",ttat); printf("total turn waiting time%f\n",twt); printf("avg turn around time%f\n",avgtat); printf("avg turn waiting time%f\n",avgwt); printf("\n-----------"); printf("\n"); for(i=1;i<=n;i++) { printf("p%d\t",p[i]); } printf("\n-----------"); printf("\n\n\nid\tbt\twt\ttat\n"); for(i=1;i<=n;i++) { printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]);

Page 13: OS Lab Prg.(2010-2011) NEW-UG

}}

OUTPUT :[2k9cs003@oxfordoec ~]$ cc fcfs.c[2k9cs003@oxfordoec ~]$ ./a.outEnter The No.Of Process:3Enter the burst time of p[1]:5Enter the burst time of p[2]:3Enter the burst time of p[3]:4total turn around time25.000000total turn waiting time13.000000avg turn around time8.333333avg turn waiting time4.333333

-----------p1 p2 p3-----------id bt wt tat1 5 0 52 3 5 83 4 8 12

Page 14: OS Lab Prg.(2010-2011) NEW-UG

Ex.No : 5b//SJF SCHEDULING ALGORITHM

#include<stdio.h>main(){ int i,j,n,temp; int p[10],bt[10]; int wt[10],tat[10]; float ttat=0,twt=0,avgtat,avgwt; //wt[1]=tat[1]=ttat=ttwt=0; printf("Enter The No.Of Process:\n"); scanf("%d",&n); for(i=1;i<=n;i++) { p[i]=i; printf("Enter the burst time of p[%d]:",i); scanf("%d",&bt[i]); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(bt[i]<bt[j]) { temp=p[i]; p[i]=p[j]; p[j]=temp; temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; } } } wt[1]=0; tat[1]=bt[1]; for(i=2;i<=n;i++) { wt[i]=wt[i-1]+bt[i-1]; tat[i]=tat[i-1]+bt[i]; } for(i=1;i<=n;i++) { twt+=wt[i]; ttat+=tat[i]; } avgwt=twt/n;

Page 15: OS Lab Prg.(2010-2011) NEW-UG

avgtat=ttat/n; printf("total turn around time%f\n",ttat); printf("total turn waiting time%f\n",twt); printf("avg turn around time%f\n",avgtat); printf("avg turn waiting time%f\n",avgwt); printf("\n-----------"); printf("\n"); for(i=1;i<=n;i++) { printf("p%d\t",p[i]); } printf("\n-----------"); printf("\n\n\nid\tbt\twt\ttat\n"); for(i=1;i<=n;i++) { printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]); }}

Page 16: OS Lab Prg.(2010-2011) NEW-UG

OUTPUT :[2k9cs003@oxfordoec ~]$ cc sjf.c[2k9cs003@oxfordoec ~]$ ./a.outEnter The No.Of Process:4Enter the burst time of p[1]:5Enter the burst time of p[2]:9Enter the burst time of p[3]:1Enter the burst time of p[4]:7total turn around time42.000000total turn waiting time20.000000avg turn around time10.500000avg turn waiting time5.000000

-----------p3 p1 p4 p2-----------id bt wt tat3 1 0 11 5 1 64 7 6 132 9 13 22

Page 17: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :5a//PRIORITY SCHEDULING ALGORITHM

#include<stdio.h>main(){ int i,n,j,temp,temp1,temp2; int p[10],t[10],pri[10]; int wt[10],tat[10],ttat,twt; wt[1]=tat[1]=ttat=twt=0; printf("Enter the no of processes : "); scanf("%d",&n); for (i=1;i<=n;i++) { printf("Enter The process no : "); scanf("%d",&p[i]); printf("Enter the burst time of process%d: ",p[i] ); scanf("%d",&t[i]); printf("Enter the priority of process%d : ",p[i] ); scanf("%d",&pri[i]); } printf("\n\nProcessname Bursttime Waittime TAtime Priority\n");

for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(pri[i]>pri[j]) { temp=pri[i]; pri[i]=pri[j]; pri[j]=temp; temp1=t[i]; t[i]=t[j]; t[j]=temp1; temp2=p[i]; p[i]=p[j]; p[j]=temp2; } } } for(i=1;i<=n;i++) { printf("p(%d)\t\t\t",p[i]); printf("%d\t\t",t[i]); printf("%d\t\t",wt[i]); tat[i]=t[i]+wt[i]; twt+=wt[i]; ttat+=tat[i]; wt[i+1]=wt[i]+t[i]; printf("%d\t\t",tat[i]); printf("%d\t\t\n",pri[i]);

Page 18: OS Lab Prg.(2010-2011) NEW-UG

}printf("\n\n");printf("Avg Waiting Time : %d\n",(twt/n));printf("Avg Turnaround Time : %d\n",(ttat/n));void space(int s){ for(j=1;j<=(s+2)/2;j++) printf(" ");}void gant(){ for(i=1;i<=tat[n];i++) printf("--"); printf("\n"); printf("|"); for(i=1;i<=n;i++) { space(t[i]); printf("P%d",p[i]); space(t[i]); printf("|"); } printf("\n"); for(i=1;i<=tat[n];i++) printf("--"); printf("\n"); for(i=1;i<=n;i++) { printf(" %d ",wt[i]); space(t[i]*2+2); } printf("%d",tat[n]);

printf("\n");

}gant();}

OUTPUT :

Page 19: OS Lab Prg.(2010-2011) NEW-UG

[2k9cs003@oxfordoec ~]$cc .priority.c[2k9cs003@oxfordoec ~]$./a.outEnter the no of processes : 3Enter The process no : 1Enter the burst time of process1: 2Enter the priority of process1 : 5Enter The process no : 2Enter the burst time of process2: 4Enter the priority of process2 : 3Enter The process no : 3Enter the burst time of process3: 1Enter the priority of process3 : 1

Processname Bursttime Waittime TAtime Priorityp(3) 1 0 1 1

p(2) 4 1 5 3

p(1) 2 5 7 5

Avg Waiting Time : 2Avg Turnaround Time :4 ---------------------| P3 | P2 | P1 |--------------------- 0 1 5 7

Page 20: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :6b

//ROUND ROBIN SCHEDULING ALGORITHM#include<stdio.h>main(){ int i,n; int p[10],count=0,ts,ptm=0,t[10],plst[10],flag=0,psbtm[10]; int wt[10],tat[10],pp=-1,ttat=0,twt=0,tot=0; wt[1]=tat[1]=t[1]=psbtm[1]=p[1]=0; printf(" Enter the no.of process in Ready Queue : "); scanf("%d",&n); printf("\n\n Enter the Time Slice \t: " ); scanf("%d",&ts); printf("\n\n Enter the Burst Time for the Process\n\n"); for (i=1;i<=n;i++) { printf(" Enter the Process ID : "); scanf("%d",&p[i]); printf(" Enter the TimeSlice for %d Process : ",p[i] ); scanf("%d",&t[i]); wt[i]=0; plst[i]=0; psbtm[i]=t[i]; }

printf("\n Process Synchronization : \n\n"); for(i=1;i<=n;i++) tot+=t[i]; for(i=1;i<=(tot/ts);i++) printf("------"); printf("\n|"); do { flag=0; for(i=1;i<=n;i++) if((count=t[i])>0) { flag=-1; count= (count>=ts) ? ts:count; printf(" P%d | ",p[i]); ptm+=count; t[i]-=count; if(pp!=i)

Page 21: OS Lab Prg.(2010-2011) NEW-UG

{ pp=i; wt[i]+=ptm-plst[i]-count; plst[i]=ptm; } } } while(flag); printf("\n"); for(i=1;i<=(tot/ts);i++) printf("------"); printf("\n\n ProcessID\t WaitingTime \t TurnAround Time\n\n"); for(i=1;i<=n;i++) { printf(" \t%d %d ",p[i],wt[i]); twt+=wt[i]; tat[i]= psbtm[i]+wt[i]; ttat+=tat[i]; printf(" %d \n",tat[i]); }printf("Avg Waiting Time : %d\n",(twt/n));printf("Avg Turnaround Time : %d\n",(ttat/n));}

Page 22: OS Lab Prg.(2010-2011) NEW-UG

OUTPUT :

[2k9cs003@oxfordoec ~]$ cc robin.c[2k9cs003@oxfordoec ~]$./ a.out

Enter the no.of process in Ready Queue : 3 Enter the Time Slice : 4

Enter the Burst Time for the Process Enter the Process ID : 1 Enter the TimeSlice for 1 Process : 24 Enter the Process ID : 2 Enter the TimeSlice for 2 Process : 3 Enter the Process ID : 3 Enter the TimeSlice for 3 Process : 3

Process Synchronization :

------------------------------------------------| P1 | P2 | P3 | P1 | P1 | P1 | P1 | P1 |------------------------------------------------

ProcessID WaitingTime TurnAround Time

1 6 30 2 4 7 3 7 10Avg Waiting Time : 5Avg Turnaround Time : 15

Page 23: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :6//INTER PROCESS COMMUNICATION

#include<stdio.h>int main(){int fd[2],child;char a[10];printf("Enter the string to enter into the pipe:\n");scanf("%s",a);pipe(fd);child=fork();if(!child){close(fd[0]);write(fd[1],a,5);wait(0);}else{close(fd[1]);read(fd[0],a,5);printf("\nThe string retrieved from the pipe is %s\n",a);}return 0;}

Page 24: OS Lab Prg.(2010-2011) NEW-UG

OUTPUT :

[2k9cs003@oxfordoec ~]$ cc icp.c[2k9cs003@oxfordoec ~]$ ./a.outEnter the string to enter into the pipe:hello

The string retrieved from the pipe is hello

Page 25: OS Lab Prg.(2010-2011) NEW-UG

Ex.No:7

//PRODUCER CONSUMER PROBLEM USING SEMAPHORE

#include <stdio.h>#include <semaphore.h>#include <pthread.h>

#define NBUFF 10

int nitems;

struct { int buff[NBUFF]; sem_t mutex, nempty, nstored; } shared;

void *produce (void *);void *consume (void *);

int main(int argc, char **argv){ pthread_t tid_produce, tid_consume; if(argc !=2) { printf("Useage: filename <nitems> "); return 0; } printf ("\n\n Producer - Consumer Problem using Semaphore\n"); printf (" -------------------------------------------\n\n"); nitems=atoi(argv[1]); sem_init(&shared.mutex,0,1); sem_init(&shared.nempty,0,NBUFF); sem_init(&shared.nstored,0,0); pthread_setconcurrency(2); pthread_create(&tid_produce,NULL,produce,NULL); pthread_create(&tid_consume,NULL,consume,NULL); pthread_join(tid_produce,NULL); pthread_join(tid_consume,NULL); sem_destroy(&shared.mutex); sem_destroy(&shared.nempty); sem_destroy(&shared.nstored);}

Page 26: OS Lab Prg.(2010-2011) NEW-UG

void *produce (void *arg){ int i; for(i=0;i<nitems;i++) { sem_wait(&shared.nempty); sem_wait(&shared.mutex); shared.buff[i % NBUFF]=i; printf ("\tProducer....."); printf ("buff[%d] = %d\n\n",i,shared.buff[i % NBUFF]);

sem_post(&shared.mutex); sem_post(&shared.nstored); sleep(3); } return NULL;}

void *consume (void *arg){ int i; for(i=0;i<nitems;i++) { sem_wait(&shared.nstored); sem_wait(&shared.mutex); printf("\tConsumer....."); printf("buff[%d] = %d\n\n\n",i,shared.buff[i%NBUFF]);

sem_post(&shared.mutex); sem_post(&shared.nempty); sleep(3); } return NULL;}

OUTPUT :

Page 27: OS Lab Prg.(2010-2011) NEW-UG

[2k9cs003@oxfordoec ~]$ cc -pthread producer.c[2k9cs003@oxfordoec ~]$ ./a.out 4

Producer - Consumer Problem using Semaphore -------------------------------------------

Producer.....buff[0] = 0

Consumer.....buff[0] = 0

Producer.....buff[1] = 1

Consumer.....buff[1] = 1

Producer.....buff[2] = 2

Consumer.....buff[2] = 2

Producer.....buff[3] = 3

Consumer.....buff[3] = 3

Ex.No :8

Page 28: OS Lab Prg.(2010-2011) NEW-UG

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME I(USING FIRST FIT ALLOCATION)

#include<stdio.h>#include<stdlib.h>#include "freespace.c"main(){ void insertf(); void displayf(); int deletf(int no,int s); int ch,prono,size,i,m; headf=NULL; head=NULL; printf("HOW MANY HOLES IN MEMORY..."); scanf("%d",&m); for(i=0;i<m;i++) insertf(); printf("\n\n"); do { printf("FREE SPACE LIST \n"); displayf(); printf("\n\nENTER THE PROCESS NO THAT U WANT TO LOAD INTO MEMORY :"); scanf("%d",&prono); printf("ENTER THE PROCESS SIZE :"); scanf("%d",&size); deletf(prono,size); printf("\n\nDO YOU WANT TO LOAD ANOTHER PROCESS ?(1/0) : "); scanf("%d",&ch); }while(ch==1);}

//vi freespace.c#include<stdio.h>#include<stdlib.h>#include "allospace.c"struct list{ int saddr; int eaddr; int sizeofnode; struct list *next;}*headf;

Page 29: OS Lab Prg.(2010-2011) NEW-UG

void insert(int pno,int s,int e); void display(); void delet();void insertf(){ struct list *newnode,*t; int start,end,son; printf("ENTER THE STARTING ADDRESS : "); scanf("%d",&start); printf("ENTER THE ENDING ADDRESS : "); scanf("%d",&end); son=end-start; newnode=((struct list *) malloc(sizeof(struct list))); newnode->saddr=start; newnode->eaddr=end; newnode->sizeofnode=son; newnode->next=NULL; if(headf == NULL) headf=newnode; else { t=headf; while(t->next!=NULL) t=t->next; t->next=newnode; }}void displayf(){ struct list *t; if (headf==0) { printf("THIS LIST IS EMPTY, U CAN'T LOAD ANY PROCESS\n"); exit(0); } else { printf("FREE MEMORY SPACES ARE : \n"); printf("SADDR EADDR\t SIZE \n"); for(t=headf;t!=0;t=t->next) { printf("%d-----",t->saddr); printf("%d\t ",t->eaddr); printf("%d\n",t->sizeofnode); } }

Page 30: OS Lab Prg.(2010-2011) NEW-UG

}int deletf(int prono,int chsize){ struct list *current,*previous; int element; element=chsize; current=headf; if (headf==NULL) printf("LIST IS EMPTY"); else if(headf->sizeofnode>=element) { current=headf; headf=headf->next; goto e; } else { previous=current; while(current!=NULL) { if(current->sizeofnode>=element) { previous->next=current->next; goto e; } previous=current; current=current->next; } } e: printf("THE APPROPRIATE HOLE IS DELETED FROM THE FREESPACE LIST\n"); insert(prono,current->saddr,current->eaddr); display();}

//vi allospace.c

#include<stdio.h>#include<stdlib.h>struct slist{ int pid; int saddr; int eaddr; struct slist *next;}*head;void insert(int proid,int start,int end)

Page 31: OS Lab Prg.(2010-2011) NEW-UG

{ struct slist *newnode,*t; newnode=((struct slist *) malloc(sizeof(struct slist))); newnode->pid=proid; newnode->saddr=start; newnode->eaddr=end; newnode->next=NULL; if(head == NULL) head=newnode; else { t=head; while(t->next!=NULL) t=t->next; t->next=newnode; } }void display() { struct slist *t; if (head==0) { printf("EMPTY LIST"); exit(0); } else {

printf("ALLOCATED LIST : \n"); printf("PID\tSADDR\tEADDR\n" ); for(t=head;t!=0;t=t->next) { printf("%d\t",t->pid); printf("%d---",t->saddr); printf("%d\n",t->eaddr); } } }

OUTPUT :

Page 32: OS Lab Prg.(2010-2011) NEW-UG

[2k9cs003@oxfordoec ~]$ cat mmsop.c[2k9cs003@oxfordoec ~]$ cc mainprg.c[2k9cs003@oxfordoec ~]$ a.outHOW MANY HOLES IN MEMORY...3ENTER THE STARTING ADDRESS : 100ENTER THE ENDING ADDRESS : 200ENTER THE STARTING ADDRESS : 350ENTER THE ENDING ADDRESS : 500ENTER THE STARTING ADDRESS : 525ENTER THE ENDING ADDRESS : 725

FREE SPACE LISTFREE MEMORY SPACES ARE :SADDR EADDR SIZE100-----200 100350-----500 150525-----725 200

ENTER THE PROCESS NO THAT U WANT TO LOAD INTO MEMORY :1ENTER THE PROCESS SIZE :150THE APPROPRIATE HOLE IS DELETED FROM THE FREESPACE LISTALLOCATED LIST :PID SADDR EADDR1 350---500

DO YOU WANT TO LOAD ANOTHER PROCESS ?(1/0) : 1FREE SPACE LISTFREE MEMORY SPACES ARE :SADDR EADDR SIZE100-----200 100525-----725 200

ENTER THE PROCESS NO THAT U WANT TO LOAD INTO MEMORY :2ENTER THE PROCESS SIZE :200THE APPROPRIATE HOLE IS DELETED FROM THE FREESPACE LISTALLOCATED LIST :PID SADDR EADDR1 350---5002 525---725

Page 33: OS Lab Prg.(2010-2011) NEW-UG

DO YOU WANT TO LOAD ANOTHER PROCESS ?(1/0) : 1FREE SPACE LISTFREE MEMORY SPACES ARE :SADDR EADDR SIZE100-----200 100

ENTER THE PROCESS NO THAT U WANT TO LOAD INTO MEMORY :3ENTER THE PROCESS SIZE :50THE APPROPRIATE HOLE IS DELETED FROM THE FREESPACE LISTALLOCATED LIST :PID SADDR EADDR1 350---5002 525---7253 100---200

DO YOU WANT TO LOAD ANOTHER PROCESS ?(1/0) : 1FREE SPACE LISTTHIS LIST IS EMPTY, U CAN'T LOAD ANY MORE PROCESS

Ex.No :9

Page 34: OS Lab Prg.(2010-2011) NEW-UG

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME

#include<stdio.h>main(){int n,ch,ff[10],ps[10],ps1[10],ps2[10],bs[10],bs1[10],bs2[10],bf[10],wf[10];int temp,i,j,m;printf("Enter the no of blocks:"); scanf("%d",&n);for(i=0;i<n;i++){printf("Enter the size of blocks[%d]:",i); scanf("%d",&bs[i]);bs1[i]=bs2[i]=bs[i];}printf("Enter the no of process:"); scanf("%d",&m);for(i=0;i<n;i++){printf("Enter the size of process[%d]:",i); scanf("%d",&ps[i]);ps1[i]=ps2[i]=ps[i];}for(i=0;i<m;i++)for(j=0;j<n;j++)if(ps[i]<=bs[i]){printf("\n Process size %d is allocated to block size :%d",ps[i],bs[j]);bs[j]=0;ps[i]=0;break;}for(i=0;i<m;i++)if(ps[i]!=0)printf("\n Insufficent Memory for Process %d",i);}

OUTPUT :

Page 35: OS Lab Prg.(2010-2011) NEW-UG

[2k9cs003@oxfordoec ~]$ cc fit3.c[2k9cs003@oxfordoec ~]$ ./a.outEnter the no of blocks:3Enter the size of blocks[0]:100Enter the size of blocks[1]:20Enter the size of blocks[2]:200Enter the no of process:3Enter the size of process[0]:100Enter the size of process[1]:200Enter the size of process[2]:500

Process size 100 is allocated to block size :100 Insufficent Memory for Process 1 Insufficent Memory for Process 2

Ex.No :10

Page 36: OS Lab Prg.(2010-2011) NEW-UG

//CONTIGUOUS FILE ALLOCATION

#include<stdio.h>main(){int i,sr,sd,sz,a=0;char file[15];printf("enter the filename");scanf("%s",file);printf("enter the file size");scanf("%d",&sz);sd=sz/100;sr=sz%100;printf("file name%s \n file size %d \n",file,sz);for(i=0;i<sd;i++){printf("%d memory block----->100 \n",i+1);a=i;}a=a+2;if(sr>0){printf("memory block %d ----->%d",a,sr);}}

OUTPUT:

[2k9cs003@oxfordoec ~]$ cc cont.c[2k9cs003@oxfordoec ~]$ ./a.outenter the filename ffit.center the file size398file nameffit.c file size 3981 memory block----->1002 memory block----->1003 memory block----->100memory block 4 ----->98

Ex.No : 11

Page 37: OS Lab Prg.(2010-2011) NEW-UG

//BANKERS ALGORITHM#include<stdio.h>struct process{int allocation[3];int max[3];int need[3];int finish;}p[10];int main(){int n,i,j,avail[3],work[3],flag,count=0,sequence[10],a[10],l=0,k=0;printf("enter the no of process:");scanf("%d",&n);for(i=0;i<n;i++){printf("enter the %dth process allocated resource:",i);scanf("%d%d%d",&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);printf("enter the %dth process maximum resource:",i);scanf("%d%d%d",&p[i].max[0],&p[i].max[1],&p[i].max[2]);p[i].finish=0;p[i].need[0]=p[i].max[0]-p[i].allocation[0];p[i].need[1]=p[i].max[1]-p[i].allocation[1];p[i].need[2]=p[i].max[2]-p[i].allocation[2];}printf("enter the available vector:");scanf("%d%d%d",&avail[0],&avail[1],&avail[2]);for(i=0;i<3;i++)work[i]=avail[i];while(count!=n){count=0;for(i=0;i<n;i++){flag=1;if(p[i].finish==0)if(p[i].need[0]<=work[0])if(p[i].need[1]<=work[1])if(p[i].need[2]<=work[2]){for(j=0;j<3;j++)work[j]+=p[i].allocation[j];p[i].finish=1;sequence[k++]=i;flag=0;

Page 38: OS Lab Prg.(2010-2011) NEW-UG

}if(flag==1)count++;}}count=0;for(i=0;i<n;i++){if(p[i].finish==1)count++;else{a[l++]=i;}}if(count==n){printf("\n the safe sequence is:\t");for(i=0;i<k;i++)printf("p%d \t",sequence[i]);}else{printf("system is not in a safe state due to:");for(i=0;i<1;i++)printf("p[%d] \t",a[i]);return 0;}}OUTPUT :[2k9cs003@oxfordoec ~]$ cc bankers.c[2k9cs003@oxfordoec ~]$ ./a.outenter the no of process:3enter the 0th process allocated resource:0 1 0enter the 0th process maximum resource:0 2 0enter the 1th process allocated resource:1 1 3enter the 1th process maximum resource:1 1 4enter the 2th process allocated resource:1 1 1enter the 2th process maximum resource:2 1 2enter the available vector:5 5 5

The safe sequence is: p0 p1 p2

Page 39: OS Lab Prg.(2010-2011) NEW-UG

Ex.No :12//FIFO PAGE REPLACEMENT

#include<stdio.h>int m,n,i,j,k,flag,count=-1,refer[100],page_frame[100],fault=0,min,no_frames;int main(){printf("\n Enter the number of reference");scanf("%d",&n);printf("\n Enter the number of frames");scanf("%d",&no_frames);printf("\n Enter the reference string:");for(i=0;i<no_frames;i++){page_frame[i]=-1;scanf("%d",&refer[i]);}printf("\t \t FIFO ALGORITHM \n");for(i=0;i<n;i++){flag=0;for(j=0;j<no_frames;j++){if(refer[i]==page_frame[j]){flag=1;printf("No page fault\n");}}if(flag!=1){fault++;count++;if(count==no_frames)count=0;page_frame[count]=refer[i];for(j=0;j<no_frames;j++)printf("%d",page_frame[j]);printf("\n");}}printf("\n \n Number of Page Faults %d\n",fault);}

Page 40: OS Lab Prg.(2010-2011) NEW-UG

OUTPUT :[2k9cs003@oxfordoec ~]$ cc fifo.c[2k9cs003@oxfordoec ~]$ ./a.out

Enter the number of reference4

Enter the number of frames3

Enter the reference string:1 2 3 4 FIFO ALGORITHM1-1-11 2-11 2 34 2 3

Number of Page Faults 4