Menu

Operating Systems [ Lab Programs ]


Aim:

  Write programs using the I/O system calls of UNIX/LINUX operating system (open, read, write, close,fcntl, seek, stat, opendir, readdir)

Solution :

a) open () system call

DESCRIPTION:

Used to open the file for reading, writing or both. This function returns the file descriptor or in case of an error -1. The number of arguments that this function can have is two or three. The third argument is used only when creating a new file. When we want to open an existing file only two arguments are used.

PROGRAM: ( open_sys.c )

 
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
extern int errno;
int main()
{
int fd=open("f3.txt",O_RDONLY | O_CREAT);
printf("fd=%d\n",fd);
if (fd==-1)
{
printf("error Number %d\n",errno);
perror("program");
}
return 0;
}


OUTPUT:

 
$ gcc open_sys.c
$ ./a.out
fd=3



b) read () system call

DESCRIPTION:

size_t read (int fd, void* buf, size_t cnt);
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.

Sample Text File : sample.txt

 
 $ cat > sample.txt
 Hello , Welcome to OS LAB


PROGRAM: ( read_sys.c )

 
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
 int fd,sz;
 char *c = (char *) calloc(100, sizeof(char));
 fd = open("sample.txt", O_RDONLY);
 if (fd==-1)
 {
 perror("r1");
 exit(1);
 }
 sz=read(fd,c,13);
 printf("called read(%d, c, 10). returned that" " %d bytes were read.\n",fd, sz);
 c[sz] = '\0';
 printf("Those bytes are as follows: %s\n", c);
 return 0; 
 }

OUTPUT:

 
$ gcc read_sys.c
$ ./a.out
called read(3, c, 10). returned that 13 bytes were read.
Those bytes are as follows: Hello, Welcom



c) write () system call

DESCRIPTION:

size_t write (int fd, void* buf, size_t cnt);
Writes cnt bytes from buf to the file or socket associated with fd. If cnt is zero, write ( ) simply returns 0 without attempting any other action.

PROGRAM: ( write_sys.c )

 
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main( )
{
 int sz;
 int fd = open("sample.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
 if (fd ==-1)
 {
 perror("r1");
 exit(1);
 }
 sz = write(fd, "hello linux", strlen("hello linux"));
printf("called write(%d, \"hello linux\", %ld). It returned %d\n", fd,strlen("hello linux"), sz);
 close(fd);
 return 0;
}

OUTPUT:

 
$ gcc write_sys.c
$ ./a.out
called write(3, "hello linux", 11). It returned 11

$ cat sample.txt
hello linux



d) close () system call

DESCRIPTION:

int close(int fd);
Tells the operating system you are done with a file descriptor and Close the file which pointed by fd.

PROGRAM: ( close_sys.c )

 
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
 int fd1 = open("sample.txt", O_RDONLY);
 if (fd1==-1)
 {
perror("c1");
exit(1);
 }
 printf("opened the fd = % d\n", fd1);
 // Using close system Call
 if (close(fd1)==-1)
 {
perror("c1");
exit(1);
}
 printf("closed the fd.\n");
 return 0;
}

OUTPUT:

 
$ gcc close_sys.c
$ ./a.out
opened the fd =  3
closed the fd.


e) fcntl ( ) system call

DESCRIPTION:

The fcntl system call is the access point for several advanced operations on file descriptors. The first argument to fcntl is an open file descriptor, and the second is a value that indicates which operation is to be performed. For some operations, fcntl takes an additional argument. We'll describe here one of the most useful fcntl operations, file locking.

PROGRAM: ( fcntl_sys.c )

 
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
char* file = argv[1];
int fd;
struct flock lock;
printf ("opening %s\n", file);
/* Open a file descriptor to the file. */
fd = open (file, O_WRONLY);
printf ("locking\n");
/* Initialize the flock structure. */
memset (&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
/* Place a write lock on the file. */
fcntl (fd, F_SETLKW, &lock);
printf ("locked; hit Enter to unlock... ");
/* Wait for the user to hit Enter. */
getchar ();
printf ("unlocking\n");
/* Release the lock. */
lock.l_type = F_UNLCK;
fcntl (fd, F_SETLKW, &lock);
close (fd);
return 0;
}

OUTPUT:

 
$ gcc fcntl_sys.c
$ ./a.out sample.txt
opening sample.txt
locking
locked; hit Enter to unlock...
unlocking


f) seek () system call

DESCRIPTION:

The lseek() function allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a "hole") return null bytes ('\0') until data is actually written into the gap.

Sample Text File : file1.txt

 
 $ cat > file1.txt
 This is an example of Linux System call


PROGRAM: ( seek_sys.c )

 
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
int main()
{
 int file=0;
 if((file=open("file1.txt",O_RDONLY)) < -1)
 return 1;
 char buffer[19];
 if(read(file,buffer,19) != 19) return 1;
 printf("%s\n",buffer);
 if(lseek(file,10,SEEK_SET) < 0) return 1;
 if(read(file,buffer,19) != 19) return 1;
 printf("%s\n",buffer);
 return 0;
}

OUTPUT:

 
$ gcc seek_sys.c
$ ./a.out
This is an example
 example of Linux S


g) stat () system call

DESCRIPTION:

int stat(const char *path, struct stat *buf);
These functions return information about a file. No permissions are required on the file itself, but in the case of stat() and lstat() - execute (search) permission is required on all of the directories in path that lead to the file.

PROGRAM: ( stat_sys.c )

 
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
 if(argc!=2)
 return 1;
 struct stat fileStat;
 if(stat(argv[1],&fileStat) < 0)
 return 1;
 printf("Information for %s\n",argv[1]);
 printf("---------------------------\n");
 printf("File Size: \t\t%ld bytes\n",fileStat.st_size);
 printf("Number of Links: \t%ld\n",fileStat.st_nlink);
 printf("File inode: \t\t%lu\n",fileStat.st_ino);
 printf("File Permissions: \t");
 printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
 printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
 printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
 printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
 printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
 printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
 printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
 printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
 printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
 printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
 printf("\n\n");
 return 0;
}


OUTPUT:

 
$ gcc stat_sys.c
$ ./a.out file1.txt
Information for file1.txt
---------------------------
File Size:              40 bytes
Number of Links:        1
File inode:             12283
File Permissions:       -rw-r--r--


h) opendir (), readdir ()system calls

DESCRIPTION:

The opendir( ) function opens a directory stream corresponding to the directory named by the d_name argument. The directory stream is positioned at the first entry. The readdir( ) function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by the argument dir, and positions the directory stream at the next entry.

PROGRAM: ( dir_sys.c )

 
#include <dirent.h>
#include <stdio.h>
int main(void)
{
 DIR *d;
 struct dirent *dir;
 d = opendir(".");
 if (d)
 {
 while ((dir = readdir(d)) != NULL)
 {
 printf("%s\n", dir->d_name);
 }
 closedir(d);
 }
 return(0);
}


OUTPUT:

 
$ gcc dir_sys.c
$ ./a.out
sample.txt
open_sys.c
.bashrc
file1.txt
priority_cpu.c
stat_sys.c
sjf_cpu.c
f3.txt
.bash_logout
write_sys.c
fcntl_sys.c
.profile
.
read_sys.c
seek_sys.c
fcfs_cpu.c
f4.txt
a.out
.cache
.sudo_as_admin_successful
..
.bash_history
f3.txtx
.motd_shown
.viminfo
rr_cpu.c
close_sys.c
dir_sys.c
.landscape



Related Content :

1. Write C programs to simulate the following CPU Scheduling algorithms
a) FCFS
b) SJF
c) RoundRobin
d) priority    View Solution


2. Write programs using the I/O system calls of UNIX/LINUX operating system (open, read, write, close,fcntl, seek, stat, opendir, readdir)    View Solution


3. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention.    View Solution


4. Write a C program to implement the Producer – Consumer problem using semaphores using UNIX/LINUX system calls.   View Solution


5. Write C programs to illustrate the following IPC mechanisms
a) Pipes
b) FIFOs
c) Message Queues
d) Shared Memory.    View Solution


6. Write C programs to simulate the following memory management techniques
a) Paging
b) Segmentation    View Solution


7. Write C programs to simulate Page replacement policies
a) FCFS
b) LRU
c) Optimal    View Solution