0% found this document useful (0 votes)
40 views8 pages

Preston University, Kohat Islamabad Campus: Department of Computer Science

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Preston University, Kohat

Islamabad Campus

Department of Computer Science

OPERATING SYSTEMS

Lab Report #______________

Submitted by

Name: ________________________________

Reg. No: _______________________________

BS Computer Science (BSCS)

Submitted to:

Instructor’s Name: ________________________

Lab Date: ___________________ Marks & Signature

Submission Date: ________________

45
LAB NO 07: SYSTEM CALLS IN UNIX-II

Duration 3 Hours

OS /Tool/Language Linux Operating Systems, C compiler/gcc

Objective(s) To get familiar with system calls in UNIX (create(), open(),


read(), write(), lseek(), close() )

The file structure related system calls available in the UNIX system let you create, open, and
close files, read and write files, randomly access files, alias and remove files, get information
about files, check the accessibility of files, change protections, owner, and group of files, and
control devices. These operations either use a character string that defines the absolute or
relative path name of a file, or a small integer called a file descriptor that identifies the I/O
channel.

Some of the file related system calls are:

· create()
· open()
· read()
· write()
· lseek()
· close()

create ()

This system call lets you to create the file. The prototype for the creat() system call is:

int creat(file_name, mode)


char *file_name;
int mode;
where file_name is pointer to a null terminated character string that names the file and mode
defines the file's access permissions. The mode is usually specified as an octal number such as
0666 that would mean read/write permission for owner, group, and others. If the file named by
file_name does not exist, the UNIX system creates it with the specified mode permissions.

46
However, if the file does exist, its contents are discarded and the mode value is ignored. The
permissions of the existing file are retained.

Example:

#include <stdio.h>
#include <sys/types.h> // defines types used by sys/stat.h
#include <sys/stat.h> // defines S_IREAD & S_IWRITE
#include <fcntl.h>
int main()
{
int fd;
fd = creat("datafile.txt", S_IREAD | S_IWRITE);
if (fd == -1)
printf("Error in opening datafile.txt\n");
else{
printf("datafile.txt opened for read/write access\n");
printf("datafile.txt is currently empty\n");
}

open ()

This system call can be used to open an existing file or to create a new file if it does not exist
already. The syntax of open has two forms:

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


int open(const char *path, int flags, mode_t modes);
The first form is normally used to open an existing file, and the second form to open a file and
to create a file if it does not exist already. Both forms returns an integer called the file
descriptor. The file descriptor will be used for reading from and writing to the file. If the file
cannot be opened or created, it returns -1. The first parameter path in both forms specifies the
file name to be opened or created. The second parameter (fl specifies how the file may be used.
The following list some commonly used flag values.

47
Flags Description

O_RDONLY open for reading only

O_WRONLY open for writing only

O_RDWR open for reading and writing

O_CREAT create file if it does not exist

O_APPEND append on each write

There is a limit on how many files a program can open, because it takes resources to store all
information needed to correctly handle an opened file. So, close all files you don't currently
need to save some resources. The number of files that can be opened varies from system to
system and user to user. Some system allows as many as 2000 files, and sometimes a user's
resource is limited.

Example:

open(“hello.c”,O_RDWR) // hello.c is opened and is ready for reading and writing.

If successful open returns non negative integer else return -1.

close ()

To close a channel, use the close() system call. The prototype for the close() system call is:

int close(file_descriptor)
int file_descriptor;
where file_descriptor identifies a currently open channel. close() fails if file_descriptor does
not identify a currently open channel.

Example of open() and close():

#include <stdio.h>
#include <fcntl.h> /* defines options flags */
#include <sys/types.h> /* defines types used by sys/stat.h
*/
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE */

48
static char message[] = "Hello, world";

int main()
{
int fd;
char buffer[80];

/* open datafile.txt for read/write access (O_RDWR)


create datafile.txt if it does not exist (O_CREAT)
return error if datafile already exists (O_EXCL)
permit read/write access to file (S_IWRITE | S_IREAD)*/

fd = open("datafile.txt",O_RDWR | O_CREAT | O_EXCL,


S_IREAD | S_IWRITE);
if (fd != -1)
{
printf("datafile.txt opened for read/write
access\n");
close (fd);
}
else
printf("*** datafile.txt already exists ***\n");
// exit (0);
}

read () & write()

The read() system call does all input and the write() system call does all output. When used
together, they provide all the tools necessary to do input and output sequentially. When used
with the lseek() system call, they provide all the tools necessary to do input and output
randomly. Both read() and write() take three arguments. Their prototypes are:

int read(file_descriptor, buffer_pointer, transfer_size)


int file_descriptor;
char *buffer_pointer;
unsigned transfer_size;

49
int write(file_descriptor, buffer_pointer, transfer_size)
int file_descriptor;
char *buffer_pointer;
unsigned transfer_size;
where file_descriptor identifies the I/O channel, buffer_pointer points to the area in memory
where the data is stored for a read() or where the data is taken for a write(), and transfer_size
defines the maximum number of characters transferred between the file and the buffer. read()
and write() return the number of bytes transferred.

There is no limit on transfer_size, but you must make sure it's safe to copy transfer_size bytes
to or from the memory pointed to by buffer_pointer. A transfer_size of 1 is used to transfer a
byte at a time for so-called "unbuffered" input/output. The most efficient value for
transfer_size is the size of the largest physical record the I/O channel is likely to have to handle.

lseek ()

The UNIX system file system treats an ordinary file as a sequence of bytes. Generally, a file
is read or written sequentially, that is, from beginning to the end of the file. The UNIX system
lets you read and write anywhere in the file. Known as "random access", this capability is
made possible with the lseek() system call. During file I/O, the UNIX system uses a long
integer, also called a File Pointer, to keep track of the next byte to read or write. This long
integer represents the number of bytes from the beginning of the file to that next character.
Random access I/O is achieved by changing the value of this file pointer using the lseek()
system call.

The prototype for lseek() is:

long lseek(file_descriptor, offset, whence)


int file_descriptor;
long offset;
int whence;
where file_descriptor identifies the I/O channel and offset and whence work together to
describe how to change the file pointer according to the following table:

50
whence new position

-----------------------------------------

0 offset bytes into the file

1 current position in the file plus offset

2 current end-of-file position plus offset

If successful, lseek() returns a long integer that defines the new file pointer value measured in
bytes from the beginning of the file. If unsuccessful, the file position does not change.

Example of read (), write (), lseek ():

#include <stdio.h>
#include <fcntl.h> /* defines options flags */
#include <sys/types.h> /* defines types used by sys/stat.h*/
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE */
static char message[] = "Hello, world";

int main()
{
int fd;
char buffer[80];

/* open datafile.txt for read/write access (O_RDWR)


create datafile.txt if it does not exist (O_CREAT)
return error if datafile already exists (O_EXCL)
permit read/write access to file(S_IWRITE|S_IREAD)
*/
fd = open("datafile.txt",O_RDWR | O_CREAT | O_EXCL,
S_IREAD | S_IWRITE);
if (fd != -1)
{
printf("datafile.txt opened for read/write
access\n");
write(fd, message, sizeof(message));

51
lseek(fd, 0L, 0); /* go back to the beginning
of the file */
if (read(fd, buffer, sizeof(message)) ==
sizeof(message))
printf("\"%s\" was written to datafile.txt\n",
buffer);
else
printf("*** error reading datafile.txt ***\n");
close (fd);
}
else
printf("*** datafile.txt already exists ***\n");
exit (0);
}

Lab task(s)

· Write a c code to create a file named “your first name.txt” present in directory named
lab7 located in Desktop directory.
· Create a program to open a file for reading named “your first name.txt” present in
directory named “lab7” located in Desktop directory.
· Create a program which creates a new file for reading and writing, whose location
(path) is provided by the user at run time.
· Write complete code to copy the contents of an input file to an output file using system
calls where the input and output files names are entered by the user at the time of
execution.

52

You might also like