Showing posts with label operating systems. Show all posts
Showing posts with label operating systems. Show all posts

Wednesday, 6 June 2012

FIFO , LRU


IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME - II (FIFO)
CODING:
#include<stdio.h>
main()
{
int frame[10],p[10],nf,np,avail,i,j,k,count=0;
printf("\nEnter the no of frames:");
scanf("%d",&nf);
printf("\nEnter the no of page numbers:");
scanf("%d",&np);
printf("\nEnter sequence of page numbers:");
for(i=0;i<np;i++)
scanf("%d",&p[i]);
printf("\n");
for(i=0;i<nf;i++)
frame[i]=-1;
j=0;
for(i=0;i<np;i++)
{
avail=0;
for(k=0;k<nf;k++)
{
if(frame[k]==p[i])
avail=1;
}
if(avail==0)
{
frame[j]=p[i];
j=(j+1)%nf;
count++;
for(k=0;k<nf;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("\nNo of page faults:%d",count);
}

OUTPUT:
Enter the no of frames:3
Enter the no of page numbers:11
Enter sequence of page numbers:7
0
1
2
0
3
0
4
2
3
0

7
7
7
2

-1
0
0
0

2
2
4
4
4
0

3
3
3
2
2
2

No of page faults:10

-1
-1
1
1

1
0
0
0
3
3

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME - II (LRU)
CODING:
#include<stdio.h>
main()
{
int frame[10],frsize,nf,np,count=0,fs[10],flag1,flag2,index,p[10],i,j,k,l,avail,i1;
printf("\nEnter the no of page frames:");
scanf("%d",&nf);
frsize=nf;
printf("\nEnter no of page nos:");
scanf("%d",&np);
printf("\nEnter sequnce of page nos:");
for(i=0;i<np;i++)
scanf("%d",&p[i]);
printf("\n");
for(i=0;i<3;i++)
frame[i]=-1;
for(j=0;j<np;j++)
{
flag1=flag2=0;
for(i=0;i<nf;i++)
{
if(frame[i]==p[j])
{
flag1=flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<nf;i++)
{
if(frame[i]==-1)
{
frame[i]=p[j];
flag2=1;
break;
}

}count++;
}
if(flag2==0)
{
for(i=0;i<nf;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<nf;i++)
{
if(frame[i]==p[k])
fs[i]=1;
}
}

for(i=0;i<nf;i++)
{
if(fs[i]==0)
index=i;
}
frame[index]=p[j];
}
for(i=0;i<nf;i++)
printf("%d\t",frame[i]);
printf("\n");
}
printf("\nNo of page faults:%d",count);
}

OUTPUT:
Enter the no of page frames:3

Enter no of page nos:8

Enter sequnce of page nos:7
0
1
2
0
3
0
4

7
7
7
2
2
2
2
4

-1
0
0
0
0
0
0
0

No of page faults:6

-1
-1
1
1
1
3
3
3

First fit, Best fit , Worst fit


IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME - I

#include<stdio.h>
main()
{
int p[10],np,b[10],nb,ch,c[10],d[10],alloc[10],flag[10],i,j;
printf("\nEnter the no of process:");
scanf("%d",&np);
printf("\nEnter the no of blocks:");
scanf("%d",&nb);
printf("\nEnter the size of each process:");
for(i=0;i<np;i++)
{
printf("\nProcess %d:",i);
scanf("%d",&p[i]);
}
printf("\nEnter the block sizes:");
for(j=0;j<nb;j++)
{
printf("\nBlock %d:",j);
scanf("%d",&b[j]);c[j]=b[j];d[j]=b[j];
}
if(np<=nb)
{
printf("\n1.First fit 2.Best fit 3.Worst fit");
do
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nFirst Fit\n");
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=b[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block:%d of size:%d",i,p[i],j,b[j]
);
flag[i]=0,b[j]=0;break;
}else flag[i]=1;
}}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}break;

case 2: printf("\nBest Fit\n");
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(c[i]>c[j])
{
int temp=c[i];c[i]=c[j];c[j]=temp;
}}}
printf("\nAfter sorting block sizes:");
for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,c[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=c[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block %d of size %d",i,p[i],j,c[j]
);
flag[i]=0,c[j]=0;break;
}else flag[i]=1;
}}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}
break;
case 3: printf("\nWorst Fit\n");
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(d[i]<d[j])
{
int temp=d[i];d[i]=d[j];d[j]=temp;
}}}
printf("\nAfter sorting block sizes:");
for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,d[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=d[j])
{

alloc[j]=p[i];printf("\n\nAlloc[%d]",alloc[j]);
printf("\n\nProcess %d of size %d is allocated in block %d of size %d",i,p[i],j,d[j]

flag[i]=0,d[j]=0;break;
}else flag[i]=1;
}}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}

break;
default:printf(“Invalid Choice…!”);break;
}
}while(ch<=3);
}}

Output
Enter the no of process:3
Enter the no of blocks:3

Enter the size of each process:
Process 0:100
Process 1:150
Process 2:200

Enter the block sizes:
Block 0:300
Block 1:350
Block 2:200

1.First fit 2.Best fit 3.Worst fit
Enter your choice:1

Alloc[100]
Process 0 of size 100 is allocated in block 0 of size 300

Alloc[150]
Process 1 of size 150 is allocated in block 1 of size 350

Alloc[200]
Process 2 of size 200 is allocated in block 2 of size 200

Enter your choice:2
Best Fit

After sorting block sizes are:
Block 0:200
Block 1:300
Block 2:350

Alloc[100]
Process 0 of size 100 is allocated in block:0 of size:200

Alloc[150]
Process 1 of size 150 is allocated in block:1 of size:300

Alloc[200]
Process 2 of size 200 is allocated in block:2 of size:350

enter your choice:3
Worst Fit

After sorting block sizes are:
Block 0:350
Block 1:300
Block 2:200

Alloc[100]
Process 0 of size 100 is allocated in block 0 of size 350

Alloc[150]
Process 1 of size 150 is allocated in block 1 of size 300

Alloc[200]
Process 2 of size 200 is allocated in block 2 of size 200

Enter your choice:6
Invalid Choice…!


IMPLEMENTATION OF FCFS,SJF,PRIORITY SCHEDULING


//IMPLEMENTATION OF FCFS SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,atime;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the arrival time:");
scanf("%d",&temp->atime);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp->atime<head->atime)
{
temp->next=head;//swapping head and temp
head=temp;
}
else if(temp->atime>head->atime)
{
tp=head;
while((tp!=NULL) && (temp->atime>tp->atime))
{

p=tp;
tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the arrival time:0
Enter the burst time:4
Press '1' to continue:1
Enter the name of the process:P2
Enter the arrival time:3
Enter the burst time:1
Press '1' to continue:1
Enter the name of the process:P3
Enter the arrival time:4
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P4
Enter the arrival time:6
Enter the burst time:3
Press '1' to continue:1
Enter the name of the process:P5
Enter the arrival time:8
Enter the burst time:2
Press '1' to continue:0

Gantt chart:
P1 P2
0
4
5

P3
7

P4
10

P5
12

// IMPLEMENTATION OF SJF SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,atime;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp->btime<head->btime)
{
temp->next=head;//swapping head and temp
head=temp;
}
}
if(temp->btime>head->btime)
{
tp=head;
while((tp!=NULL) && (temp->btime>tp->btime))
{
p=tp;

tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the burst time:5
Press '1' to continue:1
Enter the name of the process:P2
Enter the burst time:9
Press '1' to continue:1
Enter the name of the process:P3
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P4
Enter the burst time:6
Press '1' to continue:1
Enter the name of the process:P5
Enter the burst time:12
Press '1' to continue:0

Gantt chart:
P3 P1
0
2
7

P4
13

P2
22

P5
34

//IMPLEMENTATION OF PRIORITY SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,priority;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the priority:");
scanf("%d",&temp-> priority);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp-> priority <head-> priority)
{
temp->next=head;//swapping head and temp
head=temp;
}
else if(temp-> priority >head-> priority)
{
tp=head;
while((tp!=NULL) && (temp-> priority >tp-> priority))
{

p=tp;
tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the priority:7
Enter the burst time:5
Press '1' to continue:1
Enter the name of the process:P2
Enter the priority:4
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P3
Enter the priority:0
Enter the burst time:10
Press '1' to continue:1

Gantt chart:
P3
P2
0
10
12

P1
17

(null)

FILE MANIPULATIONS


#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
struct stat stbuf;
char file[10],old[20],new[20],filename[20];
int amode,ch;
printf("\n1.Rename 2.Remove 3.Change access 4.Display access mode");
do
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter file to rename:");
scanf("%s",old);
printf("\nEnter the new name:");
scanf("%s",new);
if(rename(old,new)==0)
printf("\n%s is renamed as %s",old,new);
else
printf("\nError in renaming %s",old); break;
case 2:printf("Enter filename to be removed:");
scanf("%s",file);
if(remove(file)==0)
printf("\n%s is removed",file);
else
printf("\nError in removing %s",file); break;
case 3:printf("\nEnter the name of the file to change its access mode:");
scanf("%s",filename);
if((stat(filename,&stbuf))!=0)
{
printf("\nError in moving status info of %s to the strct variable
stbuf..",filename); break;
}
if(stbuf.st_mode & S_IWRITE)
{
printf("\n%s is Changed to READ ACCESS ONLY",filename);
amode=S_IREAD;
}
else
{
printf("\n%s is Changed to WRITE ACCESS ONLY",filename);
amode=S_IWRITE;

}
if(chmod(filename,amode)!=0)
{
printf("\nError in changing the access mode...!");
break;
} break;
case 4:printf("\nCheck the access mode:READ/WRITE mode:");
printf("\nEnter name of a file:");
scanf("%s",filename);
if((access(filename,R_OK))==0)
printf("\n%s has READ permission only..",filename);
if((access(filename,W_OK))==0)
printf("\n%s has WRITE permission only..",filename);
else
printf("\n%s does not have read/write permission");
break;
default:printf("\nInvalid choice...!"); break;
}
}while(ch<=4);
}

OUTPUT

1.Rename 2.Remove 3.Change access 4.Display access mode
Enter your choice:1
Enter file to rename:sum.sh
Enter the new name:add.sh
sum.sh is renamed as add.sh
Enter your choice:2
Enter filename to be removed:add.sh
add.sh is removed
Enter your choice:3
Enter the name of the file to change its access mode:first.c
first.c is Changed to READ ACCESS ONLY
Enter your choice:4
Check the access mode:READ/WRITE mode:
Enter name of a file:first.c
first.c has READ permission only..
Enter your choice:5
Invalid choice…!

Wednesday, 2 May 2012

IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS


//IMPLEMENTATION OF FCFS SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,atime;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the arrival time:");
scanf("%d",&temp->atime);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp->atime<head->atime)
{
temp->next=head;//swapping head and temp
head=temp;
}
else if(temp->atime>head->atime)
{
tp=head;
while((tp!=NULL) && (temp->atime>tp->atime))
{

p=tp;
tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the arrival time:0
Enter the burst time:4
Press '1' to continue:1
Enter the name of the process:P2
Enter the arrival time:3
Enter the burst time:1
Press '1' to continue:1
Enter the name of the process:P3
Enter the arrival time:4
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P4
Enter the arrival time:6
Enter the burst time:3
Press '1' to continue:1
Enter the name of the process:P5
Enter the arrival time:8
Enter the burst time:2
Press '1' to continue:0

Gantt chart:
P1 P2
0
4
5

P3
7

P4
10

P5
12

(null)

// IMPLEMENTATION OF SJF SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,atime;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp->btime<head->btime)
{
temp->next=head;//swapping head and temp
head=temp;
}
}
if(temp->btime>head->btime)
{
tp=head;
while((tp!=NULL) && (temp->btime>tp->btime))
{
p=tp;

tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the burst time:5
Press '1' to continue:1
Enter the name of the process:P2
Enter the burst time:9
Press '1' to continue:1
Enter the name of the process:P3
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P4
Enter the burst time:6
Press '1' to continue:1
Enter the name of the process:P5
Enter the burst time:12
Press '1' to continue:0

Gantt chart:
P3 P1
0
2
7

P4
13

P2
22

P5
34

(null)

//IMPLEMENTATION OF PRIORITY SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,priority;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the priority:");
scanf("%d",&temp-> priority);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp-> priority <head-> priority)
{
temp->next=head;//swapping head and temp
head=temp;
}
else if(temp-> priority >head-> priority)
{
tp=head;
while((tp!=NULL) && (temp-> priority >tp-> priority))
{

p=tp;
tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the priority:7
Enter the burst time:5
Press '1' to continue:1
Enter the name of the process:P2
Enter the priority:4
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P3
Enter the priority:0
Enter the burst time:10
Press '1' to continue:1

Gantt chart:
P3
P2
0
10
12

P1
17

(null)

IMPLEMENTATION OF SYSTEM CALLS IIN LINUX


//IMPLEMENTATION OF ls SYSTEM CALL
#include<stdio.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
struct dirent *dp;
DIR *d;
if ( argc!=2 )
printf("No directory name found...");
else
{
if (( d=opendir(argv[1])) == NULL )
printf("Directory cannot be opened");
else
{
while (dp=readdir(d))
printf("\n%s",dp->d_name);0

closedir(d);
exit(0);
}
}
}

OUTPUT:
[student@localhost ex5]$ ./a.out ice
ice
process.c
shan
process2.c
file2.txt

//IMPLEMENTATION OF cat SYSTEM CALL
#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
char fname[21],data[0];
int fd;

printf("Enter filename to be created:");
scanf("%s",fname);
if (( fd=creat(fname,S_IRUSR|S_IWUSR|S_IXUSR ))<0)
printf("\nFile Creation Error...!");
else
printf("\nEnter your data:");
while(fgets(data,50,stdin))
{
data[strlen(data)-1]='\0';
printf("FD=%d",fd);
printf("\n Data=%s\n",data);
if(strcmp(data,"END")==0)
break;
if((write(fd,data,strlen(data)))!=strlen(data))
{
//printf("\n Data1=%s",data);
printf("\nWriteError1...!");
}
}
if((write(fd,"\n",1))!=1)
printf("\nWrite Error2...!");
close(fd);
}

OUTPUT
Enter filename to be created:abinaya
Enter your datatype END to exit:operating system
END

AFTER CREATING FILE
operating system


//FILE MANIPULATIONS
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
struct stat stbuf;
char file[10],old[20],new[20],filename[20];
int amode,ch;
printf("\n1.Rename 2.Remove 3.Change access 4.Display access mode");
do
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter file to rename:");
scanf("%s",old);
printf("\nEnter the new name:");
scanf("%s",new);
if(rename(old,new)==0)
printf("\n%s is renamed as %s",old,new);
else
printf("\nError in renaming %s",old); break;
case 2:printf("Enter filename to be removed:");
scanf("%s",file);
if(remove(file)==0)
printf("\n%s is removed",file);
else
printf("\nError in removing %s",file); break;
case 3:printf("\nEnter the name of the file to change its access mode:");
scanf("%s",filename);
if((stat(filename,&stbuf))!=0)
{
printf("\nError in moving status info of %s to the strct variable
stbuf..",filename); break;
}
if(stbuf.st_mode & S_IWRITE)
{
printf("\n%s is Changed to READ ACCESS ONLY",filename);
amode=S_IREAD;
}
else
{
printf("\n%s is Changed to WRITE ACCESS ONLY",filename);
amode=S_IWRITE;

}
if(chmod(filename,amode)!=0)
{
printf("\nError in changing the access mode...!");
break;
} break;
case 4:printf("\nCheck the access mode:READ/WRITE mode:");
printf("\nEnter name of a file:");
scanf("%s",filename);
if((access(filename,R_OK))==0)
printf("\n%s has READ permission only..",filename);
if((access(filename,W_OK))==0)
printf("\n%s has WRITE permission only..",filename);
else
printf("\n%s does not have read/write permission");
break;
default:printf("\nInvalid choice...!"); break;
}
}while(ch<=4);
}

OUTPUT

1.Rename 2.Remove 3.Change access 4.Display access mode
Enter your choice:1
Enter file to rename:sum.sh
Enter the new name:add.sh
sum.sh is renamed as add.sh
Enter your choice:2
Enter filename to be removed:add.sh
add.sh is removed
Enter your choice:3
Enter the name of the file to change its access mode:first.c
first.c is Changed to READ ACCESS ONLY
Enter your choice:4
Check the access mode:READ/WRITE mode:
Enter name of a file:first.c
first.c has READ permission only..
Enter your choice:5
Invalid choice…!