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.
#include
#include
#include
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;
}
$ gcc open_sys.c
$ ./a.out
fd=3
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.
$ cat > sample.txt
Hello , Welcome to OS LAB
#include
#include
#include
#include
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;
}
$ gcc read_sys.c
$ ./a.out
called read(3, c, 10). returned that 13 bytes were read.
Those bytes are as follows: Hello, Welcom
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.
#include
#include
#include
#include
#include
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;
}
$ gcc write_sys.c
$ ./a.out
called write(3, "hello linux", 11). It returned 11
$ cat sample.txt
hello linux
int close(int fd);
Tells the operating system you are done with a file descriptor and Close the file which pointed by fd.
#include
#include
#include
#include
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;
}
$ gcc close_sys.c
$ ./a.out
opened the fd = 3
closed the fd.
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.
#include
#include
#include
#include
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;
}
$ gcc fcntl_sys.c
$ ./a.out sample.txt
opening sample.txt
locking
locked; hit Enter to unlock...
unlocking
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.
$ cat > file1.txt
This is an example of Linux System call
#include
#include
#include
#include
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;
}
$ gcc seek_sys.c
$ ./a.out
This is an example
example of Linux S
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.
#include
#include
#include
#include
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;
}
$ 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--
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.
#include
#include
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);
}
$ 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
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