0% found this document useful (0 votes)
23 views

OSLAB2

The document describes the implementation of various system calls like open(), creat(), close(), write(), read(), and lseek() in C programming. It provides the syntax, parameters, return values, and examples of code snippets for each system call. The overall aim is to introduce these fundamental system calls and how to use them to perform operations on files like opening, creating, writing, reading, seeking in a file.

Uploaded by

Mariya Babu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

OSLAB2

The document describes the implementation of various system calls like open(), creat(), close(), write(), read(), and lseek() in C programming. It provides the syntax, parameters, return values, and examples of code snippets for each system call. The overall aim is to introduce these fundamental system calls and how to use them to perform operations on files like opening, creating, writing, reading, seeking in a file.

Uploaded by

Mariya Babu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB-02

AIM:Introduction to system calls -implementation of


open(),creat(),close(),write(),read(),lseek().
1)Name of the system call:open() system call
Description:the open() system call is used to provide access to a file in a file
system. This system call allocates resources to the file and provides a handle that
the process uses to refer to the file. A file can be opened by multiple processes at
the same time or be restricted to one process. It all depends on the file organisation
and file system.
Syntax:int open(char *pathname ,int flags,/*mode_t mode);
Parameters:-
pathname:Here we need to specify name of the file .
Flags: flags indicate the operations to be done on file .
O_RDONLY:opens for read only operation.
O_WRONLY:opens for write only operation.
O_RDWR:opens for read write operation.
O_APPEND:it appends contents to the end of the file.
O_TRUNC:truncate’s the contents of file .
O_CREAT:this flag is to create the new file.
O_EXCL:Reports informs if the file is already existing.
mode _t mode:it is an optional parameter and indicates the permission given to the
file.
=> u-user/owner => read(r)=4
=>G-group =>write(w)=2
=>O-others =>excute(x)=1
Return value:it returns integer file descriptor on success .
File descriptor is a logical file name given to the physical file name 0,1,2 are
reserved.
0->keyboard
1->monitor
2->standard error
Any open files will be assigned an integer greater than 2.
=>on failure it returns -1
EXAMPLE:
INPUT:
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
extern int errno;
int main()
{
int fd=open("oslab.txt",O_RDONLY|O_CREAT);
printf("file descriptor=%d\n",fd);
if(fd==-1)
{
perror("program");
}
return 0;
}
OUTPUT:

2))Name of the system call:creat() system call


Description:these system call is used to create a new file it takes two parameters.
Syntax:int creat(char *filepath,mode_t mode);
Parameters:-
pathname:Name of the file to be created.
Mode_t mode :This specifies permissions to be given to the file.
return value:on success it returns integer file descriptor.
On failure it returns -1.
EXAMPLE:
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd=creat("example.txt",0644);
printf("file descriptor=%d",fd);
if(fd==-1)
{
perror("error creating file");
return 1;
}
close(fd);
return 0;
}
OUTPUT:

3))Name of the system call:close() system call


Description close() is used to close the file. It takes file descriptor as a parameter
which is already open.
Syntax:int close(int file descriptor);
Parameters:-
File descriptor:file descriptor as a parameter which is already open.
Return value:on success it returns ‘0’.
On failure it returns ‘1’.
INPUT:
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
extern int errno;
int main()
{
int fd;
fd = open("close.txt",O_WRONLY|O_CREAT,677);
if(fd==-1){
perror("error opening file");
return 1;
}
if(close(fd)==-1){
perror("error closing file");
return 1;
}
printf("file closed successfully\n");
return 0;
}
OUTPUT:

4))Name of the system call:read() system call


Description: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:size_t read (int fd, void* buf, size_t cnt);
• fd: file descriptor
• buf: buffer to read data from
• cnt: length of buffer
Return value:returns no of bytes red on success
‘0’ - on end of file.
‘-1’ - on error

INPUT:
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
extern int errno;
int main()
{
int buf[1000];
int fd = open("read.txt",O_RDONLY|O_CREAT);
printf("file descriptor(open):%d\n",fd);
if(fd==-1){
perror("error opening file");
}
int fd1=read(3,buf,sizeof(buf));
printf("file descriptor(read):%d",fd1);
if(fd1<0)
{
perror("error reading file");
}
close(fd);}
OUTPUT:

5)Name of the system call:write() system call


Description: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, void* buf, size_t cnt);
Parameters:
• fd: file descriptor
• buf: buffer to write data to
• cnt: length of buffer
Return value:on success it returns no of bytes written .
‘-1’ on error.
INPUT:
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
int main(){
int sz;
int fd = open("write.txt",O_WRONLY|O_CREAT|O_TRUNC,644);
if(fd<0)
{
perror("error");
}
sz= write(fd,"hello friends\n",strlen("hello friends\n"));
printf("called write fd=%d\n hello friends=%d \n it retuned %d",fd,strlen("hello
friends\n"),sz);
close(fd);
}
OUTPUT:

6)Name of the system call:lseek() system call


Description:repositions the file offset of the open file description associated with
the file descriptor fd to the argument offset according to the directive whence as
follows:
SEEK_SET The file offset is set to offset bytes.
SEEK_CUR The file offset is set to its current location plus offset bytes.
SEEK_END The file offset is set to the size of the file plus offset bytes.
Syntax:int lseek(int fd, off_t offset, int whence);
INPUT:

#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
int main(){
int fd,fd1;
fd=open("lseek.txt",O_RDONLY|O_CREAT);
if(fd == -1){
perror("error openin file");
return 1;
}
fd1=lseek(fd,100,SEEK_SET);
printf(" file descrptor=%d",fd1);
if(fd1==-1){
perror("error seeking in file");
close(fd);
return 1;
}
close(fd);
return 0;
}
OUTPUT:

Program with all system-calls:


#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
extern int errno;
int main(){
int fd,fd1,fd2,fd3,fd4;
char buf[100];
fd=creat("hi.txt",644);
printf("file descriptor(creat)=%d\n",fd);
fd1=open("hi.txt",O_RDWR);
printf("file descriptor(open)=%d\n",fd1);
fd2=lseek(fd,1000,SEEK_SET),
printf("file descriptor(write)=%d\n",fd2);
fd3=write(fd,"oslab" ,strlen("oslab"));
printf("file desciptor(write)=%d\n",fd3);
fd4=read(fd1,buf,sizeof(buf));
printf("file desciptor(read)=%d\n",fd4);
if(close(fd)==-1)
{
perror("error closing file");
return 1;
}
printf("file closed successfully");
return 0;
}
OUTPUT:
……………………………………………………………………………………………………

You might also like