Basic Programs
System Calls:
System calls provide an intetace_to_theC SerY0Ces made available by an
operatingsystem, These calls are available as routines written in Cnd C+t,
certain low-level tasks may need to be written using assembly-language
instructions.
Ex: System calls for writing a simple program to read data from one file and
copy thenm from another file.
What is the File Descriptor?
The file descriptor is an integer that uniquely identifies an open file of
the process.
1) read() and write() system calls are used to read and write data
respectively to a file descriptor. To understand the concept of write()/
read() system calls let us first start with write() system call.
2) write() system call is used to write to a file descriptor. In other words
write() can be.used to write to any file in the system but rather than
specifyingthe file name, you need to specify its file descriptor.
3) Syntax:
#include<unistd. h>
ssize_t write(int fd, const void *buf, size_t count);
Parameters
fd: file descriptor
" buf: buffer to write data to.
cnt: length of the buffer.
The first parameter (fd) is the file
descriptor
data that is to be written is specified in the where you want to write. The
thìrdparameter is the total bytes that are to be second parameter. Finally, the
written.
Programl: To write some data on the standard output
monitor) device (by default
I/Name the program file as "write.c
#includesunistd.h>
int main()
write(1,"hellon",6);
/1 is the file descriptor, "hello\n" is the data, 6 is the count
data of characters in
How it works?
The write() system call takes three
descriptor of the file where we want toparameters:
"1" which is the file
write. Since we
standard output device which is the screen, hence the file want to write on
case, is 1', which is fixed (0 is the file descriptor, in this
descriptor for standard input device
(e.g. keyboard) and 2 is for standard error device).
Next thing is what we want to write on the
screen. In this case its "hello\n"*
i.e. helloand nevwline(\n), so atotal of 6
characters, which becomes the third
parameter. The third parameter is how much you want to write,
be less than the data specified in the which may
and see the change in output.
second parameter. Youcan play around
Program 2
lincludestdio.h
lincludeunistd.h
int main()
intcount;
count write(1,"helloln",6);
printf("Total bytes written: %dn",count);
How it Works?
Need to written on your own.
Program 3:
finclude<unistd.h
int main()
write(1,"hello'n",60); //the bytes to be printed (third parameter) are more
than the data specified in 2nd
parameter
Program4:
#include<unístd.h>
int main()
write(1,"hello'n",3),:/the
the data specificd in 2nd
bytes to be printed (thírd parameter) are less than
parameter
Progrann5:
#include<unistd.h>
#include<stdio.h>
int main()
int count;
count-write(3,
specified ones "i.e.,
hello\n",
0, Ior6):2
/the file descriptor is not one of the pre
printt("Total bytes written: %dn",count);
read)
The use of read) system call is to
working is same as write), the only read from ais file descriptor. The
data from file pointed to by file difference read) will read the
descriptor.
Syntax:
#include<unistd.h>
ssize t read( int fd, Const void
*buf,
count); size_t
The first parameter is the file
the buffer where the read data descriptor. The second parameter is
will be saved. Lastly, the third
parameter is the number of bytes that you want to read.
Program 1: To read data from the standard input device and write it
on the screen
//read.c
#include<unistd.h>
int main()
{
char buff [ 20];
read (0, buff, 10) ;//read 10 bytes
device (keyboard), store in bufferfron standard input
write(1, buff, 10) ;//print 10 bytes (buff)
from the
on the s r e e n buffer
}
How it works?
The read) system call reads the input typed by the user via the
(file descriptor 0) and stores it in the buffer (buff) keyboard
which is nothing but a
character array. It will read a maximum of 10 bytes (because of the third
parameter). This can be less than or equal to the buffer size. No matter how
much the user types only first 10 characters will be read.
Finally, the data is printed on the screen using the write() systemcall. It
prints the same 10 bytes from the buffer (buf) on the scrcen (file
1). descriptor
Program 2: To read data from the standard input device and write it on the
Screen
l/read.c
#include<unistd. h
int main( )
{
int nread;
char buff [20 ];
nread=read(0, buff,10);//read 10 bytes from standard
input device(keyboard), store in buf fer (buff)
write(l,buff, nread); l/print 10 bytes from the
buffer on the screen
}
create()
The create() function is used to create a new
the permission and the name of the file empty file in C. We can specify
which we
create() function. It is defined inside <unistd.h> want to crcate using the
header file and the flags
that are passed as arguments are defined inside <fentl.h>
header file.
Syntax:
int create(char *filename, mode t
mode);
Parameter
filename: name of the file
mode: indicates permissionswhich you want to create
of the new file.
How Ccreate) works in OS
Create a new empty file on the disk.
Create file table entry.
Set thefirst unused file descriptor to
Return file descriptor used, -1 upon point to the file table entry.
open) failure.
The open() function in Cis
used to open the file for
both. It is also capable of creating
inside <unisti.h> header file and the the file if it does notreading,
exist.
writing, or
It is defined
defined inside <fentl.h> header file. flags that are passed as arguments are
Syntax of open) in C
int open (const char* Path, int flags);
Parameters
Path: Path to the file which we want to open.
Use the absolute path beginning with when
working in the same directory as the Csource file. you are not
Use relative path which is only the file name with
when you are wvorking in the same directory as theextension,
C source
file.
flags: It is used to specify how you want to
the following flags. open the file. We can use
Example of Copen)
7CProgram o ilusLrate
1/ open Byatenm call
#inclucde <er no,h
#include xEenttL,h
#include <atdio,h>
#include Kunistd,hz
extern int Grrno
int ma in()
1/ if file does not have in dí rector
// then file foo. tzt is created,
int fd = open ("foo. tzt", 0 RDONIY | 9 CHEAT) ;
printf ("fd = Bd\n" fd) ;
1f (fd ---1) (
// Drint wh ich t ype of error have ín a code
printt ("Error Nunber % d\n", urrno) ;
1/ print program detaíl "Success or
failure"
porror ("Program") :
)
return 0;
C close
The close() function inC tells the operating system that you are done with a
file descriptor and closes the file pointed by the file descriptor. It is defined
inside <unistd.h> header file.
Syntax of close() in C
int close(int fd);
Parameter
fd: File descríptor of the file that you wart to cioSe.
How C close() works in the OS
Destroy file table entry referenced by eleent fd of the fle deseríptor
table
-As long as no other process is pointing to it!
Set element fd of file descriptor table to NULL
Example 1: close() in C
1/Cprogram to 11lustrate close
#include <Ecntl.r system Cal2
#include Kutio.he
$include Kunistd.n>
int main ()
int fdl open("foo. tzt", o RDOINLY);
if (£di < 0) 1
perroz ("c1"):
sKít (1) ;
printf (" opened the fd = 3 d\a, fd1) ;
11 Using cise sytem Ca11
if (cl0se(fd1) < 0) 4.
perror (" c1");
%it (1) ;
printf ("closed the fd.in");