How many times open and release function is called of a character driver after fork
Consider the /dev/msg which we created in our previous post, what if the test application created fork(), let's see how many times the open and close function will be called.
Test Application Code:
Output:
Notes:
The open and release function is only called once.
When you do fork(), it will not create a new file structure and close() will call the release method of the driver only when the counter of the file structure becomes zero.
Test Application Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
char buffer[1024]; | |
int fd; | |
int length; | |
pid_t pid; | |
fd = open("/dev/msg", O_RDWR); | |
if (fd < 0) { | |
perror("fd failed"); | |
exit(2); | |
} | |
pid = fork(); | |
if (pid == 0) { | |
printf("Child Process Executing and writing hello world:%d\n", | |
write(fd, "hello world", sizeof("hello world"))); | |
} | |
else { | |
printf(" Parent Process executing and writing hello embedded:%d\n", | |
write(fd, "hello embedded", sizeof("hello embedded"))); | |
} | |
close(fd); | |
} |
Output:
Notes:
The open and release function is only called once.
When you do fork(), it will not create a new file structure and close() will call the release method of the driver only when the counter of the file structure becomes zero.
Comments
Post a Comment