Thursday, February 4, 2010

SYSTEM CALLS

SYSTEM CALLS

#include “ stdio.h “
#include “ sys/types.h “
#include “ sys/dir.h “
#include “ sys/stat.h “
#include “ fcntl.h “
#include “ unistd.h “
#define size 512
char buff[size],name[20];
int fd,n,k;
main()
{
int ch,nread,nwrite,a,pos,i,j;
long int of;
struct stat buf;
do
{
printf("\n\n\t IMPLEMENTATION OF SYSTEM CALLS");
printf("\n\t1.Create a file");
printf("\n\t2.Open a file");
printf("\n\t3.Write into the file");
printf("\n\t4.Read the file contents");
printf("\n\t5.Seek the file");
printf("\n\t6.View the status of the file");
printf("\n\t7.Close the file");
printf("\n\t8.exit");
printf("\n\tEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter name of file:");
scanf("%s",name);
fd=creat(name,S_IRUSRS_IWUSRS_IXUSR);
if(fd==1)
printf("\n\tError in creating file");
else
{
printf("\n\tFile created");
printf("\n\tThe descriptor is %d",fd);
}
break;

case 2:
printf("\n\t Enter the name of the file:");
scanf("%s",name);
fd=open(name,2);
if(fd==1)
printf("\n\tError in opening the file");
else
printf("\n\tFile opened with fd %d",fd);
break;

case 3:
printf("\n\tEnter the info to be stored in the file");
scanf("%s",buff);
if(write(fd,buff,sizeof(buff)) “ 0)
printf("\n\tError in writing into the file");
else
printf("\n\tWrite Successful");
break;

case 4:
printf("\n\tThe contents of the file:\n");
nread=read(fd,buff,sizeof(buff));
if(nread!= -1)
printf("\n\t%s",buff);
break;

case 5:
printf("\n\tEnter the offset:");
scanf("%d",&of);
printf("\n\tEnter the position:");
scanf("%d",&pos);
if(a== -1)
printf("\n\tSeek Error");
else
printf("\n\tSeek Ok");
break;

case 6:
i=stat(name,&buf);
j=fstat(fd,&buf);
if(i==0 && j==0)
printf("\n\tSTAT & FSTAT successful");
else
printf("\n\tstat &fstat error");
printf("\n\tFile %s has %d links:",name,buf.st_nlink);
printf("\n\tFile inode number:%d",buf.st_ino);
printf("\n\tUserid:%d",buf.st_uid);
break;

case 7:
k=close(fd);
if(k==1)
printf("\n\tError in closing the file");
else
printf("\n\tFile Closed Successfully");
break;

case 8:
printf("\n\n\t\tPress any key.");
break;

}
}while(ch!=8);
}


















OUTPUT

[user1@localhost user1]$ cc syscall.c
[user1@localhost user1]$ ./a.out


IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:1

Enter name of file:unix

File created
The descriptor is 3

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:2

Enter the name of the file:unix

File opened with fd 4

IMPLEMENTATION OF SYSTEM CALLS

1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:3

Enter the info to be stored in the file
operatingsystem

Write Successful

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:4

The contents of the file:

operatingsystem

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:5

Enter the offset:0

Enter the position:1

Seek Ok

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:6

STAT & FSTAT successful
File unix has 1 links:
File inode number:4383000
Userid:515

IMPLEMENTATION OF SYSTEM CALLS
...................................
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:7

File Closed Successfully

IMPLEMENTATION OF SYSTEM CALLS
...................................
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:8


Press any key.[user1@localhost user1]$

No comments:

Post a Comment