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:


#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);
}
view raw testapp.c hosted with ❤ by GitHub


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

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto