Application program for read/write
#include<stdio.h>
#include<fcntl.h> // For open()
#include<unistd.h> // For read()
#include<string.h>
int main(int charc, char * argv[])
{
int fd;
char wbuf[40];
char rbuf[40];
ssize_t rd,wr;
strcpy(wbuf,”HELLO KERNEL!!\n”);
strcpy(rbuf,””);
fd = open(argv[1], O_RDWR);
printf(“File Descp. = %d\n”,fd);
wr = write(fd, wbuf, sizeof (wbuf));
printf(“\nData in file is: \n%s\n”,wbuf);
fd = open(argv[1], O_RDWR);
printf(“File Descp. = %d\n”,fd);
rd = read(fd, rbuf, sizeof (rbuf));
printf(“\nrd = %d \n”, rd);
printf(“%s\n”, rbuf);
close(fd);
printf(“File closed”);
return 0;
}
why u used open() twice..????????
Once for writing: As the node is unidirectional… if the node is used for writing once then from then onwards it could be used for writing only. Now, if you want to read you need a new node in the read mode. so it has to be reopened in the read mode.
Even i can’t understand why the file is opened two times,when you can use same file descriptor for read(),write() operation…using syn between these two e.g. pipes…which is named pipes…