You are on page 1of 7

(Open ,read,write,close,dup ,dup2,lseek,stat,fstat,lstat )system calls

Open system call

int open(const char *pathname, int flags);


int open(const char *pathname, int flags, mode_t mode);

write system call

ssize_t write(int fd, const void *buf, size_t count);

read system call

ssize_t read(int fd, void *buf, size_t count);

close system call

int close(int fd);

dup()
dup2() system call

int dup(int oldfd);


int dup2(int oldfd, int newfd);

lseek system call

off_t lseek(int fd, off_t offset, int whence);

stat system call

int stat(const char *path, struct stat *buf);


int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
Programs

1)
#include <stdio.h>
#include <sys/types.h> /* defines types used by sys/stat.h */
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE */
int main()
{
int fd;
fd = creat("datafile.dat", S_IREAD | S_IWRITE);
if (fd ==-1)
printf("Error in opening datafile.dat\n");
else

{
printf("datafile.dat opened for read/write access\n");
printf("datafile.dat is currently empty\n");
}
close(fd);

}
…………………………………………….

2)
#include<fcntl.h>
#include<unistd.h>
main()
{
int fd,i;
fd=open("test123",O_CREAT|O_RDWR|O_APPEND);
for(i=0;i<10;i++)
write(fd,"h",1);
close(fd);
}
……………………………………………………….

3) #include<stdio.h>
#include<fcntl.h>
main()
{ char buf[100],fn[10];
int fd,n;
printf("Enter file name ");
scanf("%s",fn);
fd=open(fn,O_RDONLY);
n=read(fd,buf,100);
n=write(1,buf,n);//write to monitor
close(fd);
}
……………………………………………………
4)
#include<stdio.h>
#include<fcntl.h>
main()
{ char buf[1000],fn1[10],fn2[10];
int fd1,fd2,n;
printf("Enter source file name ");
scanf("%s",fn1);
printf("Enter destination file name ");
scanf("%s",fn2);
fd1=open(fn1,O_RDONLY);
n=read(fd1,buf,1000);
fd2=open(fn2,O_CREAT|0666);
n=write(fd2,buf,n);//write file
close(fd1);
close(fd2);
}

…………………………………………………….
5)
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>

int main()
{
int fd,fd1;
struct stat avi;
//fd=open("f1.doc",O_RDWR,0777);
//fd1=open("f2.doc",O_CREAT|O_RDWR,S_IRWXU);
stat("/home/linux/f1.doc",&avi);
printf("%d %d\n",avi.st_uid,avi.st_mode);
//dup2(1,fd);
// dup2(fd1,fd);
//write(fd,"hello\n",6);
return 0;
}
……………………………………….
6)
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>

int main()
{
int fd,fd2;
char buffer[6];
fd=open("/home/g/pgr/p.doc",O_RDWR|O_CREAT,00777);
fd2=open("/home/g/pgr/q.doc",O_RDWR|O_CREAT,00777);
// dup2(fd2,fd);
write(fd,"Hello World",11);
lseek(fd,5,SEEK_SET);
read(fd,buffer,6);
write(1,buffer,6);
return 0;
}
…………………………………………..
7)
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>

int main()
{
int fd,fd2,fd3;
char buffer[6];
fd=open("/home/g/pgr/w3.doc",O_RDWR|O_CREAT,00777);
fd2=open("/home/g/pgr/w4.doc",O_RDWR|O_CREAT,00777);
dup2(1,fd);
write(fd,"Hello World",11);
// lseek(fd,5,SEEK_SET);
// read(fd,buffer,6);
// write(1,buffer,6);
return 0;
}

………………………………………………………….

8)
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
void main()
{
int in,out;
char ch;
in=open("file",O_RDONLY);
out=open("file1",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
while(read(in,&ch,1)==1)
write(out,&ch,1);
exit(0);
}
………………………………………………………………

9)
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int file;
file=open("error",O_RDONLY);
if(file==-1)
return 1;
char buf[50];
if(read(file,buf,30)!=30)
return 1;
printf("%s\n",buf);
if(lseek(file,10,SEEK_SET)<0)
return 1;
if(read(file,buf,30)!=30)
return 1;
printf("%s\n",buf);
close(file);
return 0;
}
……………………………………..
10)
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int in,out,n;
char buf[200];
in=open("file",O_RDONLY);
out=open("file3",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
while((n=read(in,buf,sizeof(buf)))>0)
write(out,buf,n);
exit(0);
}
…………………………………..
11)
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
void main()
{
int n;
char buf[100],file;
n=read(0,buf,100);
if(n==-1)
write(2,"A read error has been occured\n",30);
if((write(1,buf,n))!=n)
write(2,"A write error has been occured\n",32);
exit(0);
}
………………………..
12)
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
void main()
{
int n;
char buf[100],file;
n=read(file,buf,100);
if(n==-1)
write(2,"A read error has been occured\n",30);
if(n==0)
write(1,"There in no read data from file\n",30);
if((write(1,buf,n))!=n)
write(2,"A write error has been occured\n",32);
exit(0);
}
……………………………..
13)
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
void main()
{
//int a;
if((write(1,"Here is some data\n",18))!=10)
write(2,"A write error has occured on file discriptor1\n",46);
exit(0);
}

…………………………………….

14)

#include<stdio.h>
#include<fcntl.h>
main()
{ char buf[100],fn[10];
int fd,n;
printf("Enter file name ");
scanf("%s",fn);
fd=open(fn,O_RDONLY);
n=read(fd,buf,100);
n=write(1,buf,n);//write to monitor
close(fd);
}

You might also like