os week1
os week1
Write programs using the I/O system calls of UNIX/LINUX operating system
(open, read, write, close, fcntl, seek, stat, opendir, readdir)
Theory:
There are 5 basic system calls that Unix provides for file I/O.
1. Create: Used to Create a
new empty file Syntax :int creat(char
*filename, mode_t mode) filename :
name of the file which you want to
create mode : indicates permissions of
new file.
2. open: Used to Open the file for reading, writing or both.
Syntax: int open(char *path, int flags [ , int mode ] );
Path : path to file which you
want to use flags : How you
like to use
O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write,
O_CREAT: create file if it doesn’t exist, O_EXCL: prevent creation if it already
exists
3. close: Tells the operating system you are done with a file descriptor
and Close the file which pointed by fd.
Syntax: int
close(int fd); fd :file
descriptor
4. read: From the file indicated by the file descriptor fd, the read()
function reads cnt bytes of input into the memory area indicated by buf. A successful
read() updates the access time for the file.
Syntax: int read(int fd, char
*buf, int size); fd: file descripter
buf: buffer to
read data from
cnt: length of
buffer
5. write: Writes cnt bytes from buf to the file or socket associated with
fd. cnt should not be greater than INT_MAX (defined in the limits.h header file). If
cnt is zero, write() simply returns 0 without attempting any other action.
Syntax: int write(int fd, char
*buf, int size); fd: file descripter
buf: buffer to
write data to
cnt: length of
buffer
*File descriptor is integer that uniquely identifies an open file of the process.
Algorithm
1. Star the program.
2. Open a file for O_RDWR for R/W,O_CREATE for creating a file ,O_TRUNC for
truncate a file.
3. Using getchar(), read the character and stored in the string[] array.
4. The string [] array is write into a file close it.
5. Then the first is opened for read only mode and read the characters and displayed it and
close the file.
6. Stop the program.
Program
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100]; f1=open("data",O_RDWR|
O_CREAT|O_TRUNC); while((c=getchar())!='\n')
{
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY);
read(f2,strin,0); printf("\n%s\
n",strin); close(f2);
return 0;
3
Output:
Hai
Hai
4
a) Aim: C program using lseek
Theory:
lseek is a system call that is used to change the location of the read/write pointer of a file
descriptor. The location can be set either in absolute or relative terms.
Syntax : off_t lseek(int fildes, off_t offset, int whence);
int fildes : The file descriptor of the pointer that is going to be moved.
off_t offset : The offset of the pointer (measured in bytes).
int whence : Legal values for this variable are provided at the end which are
SEEK_SET (Offset is to be measured in absolute terms), SEEK_CUR (Offset is to be measured
relative to the current location of the pointer), SEEK_END (Offset is to be measured relative to
the end of the file)
Algorithm:
1. Start the program
2. Open a file in read mode
3. Read the contents of the file
4. Use lseek to change the position of pointer in the read process
5. Stop
5
Program:
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
{
int file=0; if((file=open("testfile.txt",O_RDONLY)) <
-1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1; printf("%s\
n",buffer);
return 0;
}
Output:
6
b) Aim: C program using opendir(), closedir(), readdir()
Theory:
The following are the various operations using directories
1. Creating directories.
Syntax : int mkdir(const char *pathname, mode_t mode);
2. The ‘pathname’ argument is used for the name of the directory.
3. Opening directories
Syntax : DIR *opendir(const char *name);
4. Reading directories.
Syntax: struct dirent *readdir(DIR *dirp);
5. Removing directories.
Syntax: int rmdir(const char *pathname);
6. Closing the directory.
Syntax: int closedir(DIR *dirp);
7. Getting the current working directory.
Syntax: char *getcwd(char *buf, size_t size);
Algorithm:
1. Start the program
2. Print a menu to choose the different directory operations
3. To create and remove a directory ask the user for name and create and remove the same
respectively.
4. To open a directory check whether directory exists or not. If yes open the directory .If it
does not exists print an error message.
5. Finally close the opened directory.
6. Stop
7
Program:
#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
main()
{
char d[10]; int c,op; DIR *e;
struct dirent *sd;
printf("**menu**\n1.create dir\n2.remove dir\n 3.read dir\n enter ur choice");
scanf("%d",&op);
switch(op)
{
case 1: printf("enter dir name\n"); scanf("%s",&d);
c=mkdir(d,777);
if(c==1)
printf("dir is not created");
else
printf("dir is created"); break;
case 2: printf("enter dir name\n"); scanf("%s",&d);
c=rmdir(d);
if(c==1)
printf("dir is not removed");
else
printf("dir is removed"); break;
case 3: printf("enter dir name to open"); scanf("%s",&d);
e=opendir(d);
if(e==NULL)
printf("dir does not exist"); else
{
printf("dir exist\n"); while((sd=readdir(e))!=NULL) printf("%s\t",sd->d_name);
}
closedir(e);
break;
8
}
}
9
Output:
10