Search code examples
clinux-kernelkerneldriversmp

In what order does user space code execute?


Hi I'm writting a char driver that reads and writes to a particular device. Since I'm a noob, this is a very simple and easy char drive that only utilizes the simplest of protocols such as open, read, write, and release. To test my driver I am using the following program ... bellow is the source for my user-space program.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <poll.h>

int main(void){
int num;
char *buff;
FILE *fd = fopen("/dev/hi","a+");
num = fprintf(fd,"this is sentence 1 !!");
num = fprintf(fd,"this is sentence 2 !!");
num = fprintf(fd,"this is sentence 3 !!");
num = fprintf(fd,"this is sentence 4 !!");
num = fprintf(fd,"this is sentence 5 !!");
buff = malloc(sizeof(char) * num+1);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
free(buff);
close(fd);
return 0;
}

Now its not important how my driver works but, in what order that I call my read and write methods. Ideally, it would be nice if the driver was being written to in the order that I've written my code and read from in the order that I've wrote my code. However I've noticed that if I have written my code like ...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <poll.h>

int main(void){
int num;
char *buff;
FILE *fd = fopen("/dev/hi","w");
num = fprintf(fd,"this is sentence 1 !!");
num = fprintf(fd,"this is sentence 2 !!");
num = fprintf(fd,"this is sentence 3 !!");
num = fprintf(fd,"this is sentence 4 !!");
num = fprintf(fd,"this is sentence 5 !!");
    close(fd);
    fd = fopen("/dev/hi","r");
buff = malloc(sizeof(char) * num+1);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
fread(buff,sizeof(char),num+1,fd);
printf("%s\n",buff);
free(buff);
close(fd);
return 0;
}

I notice that fprintf() only writes when I close the file descriptor and worst yet, executes after I read from my device. Of course I want to write to my device and then read from it but it doesn't happen in order that. This gives me the impression that many things in user-space execute at the same time which baffles me. How do I know the order of which my device functions get called when dealing with user space. Sorry if this seems vague, I'll elaborate on anything that is mirky.

Thanks for any replies!!


Solution

  • Your writes to 'fd' are being cached and only written to the device driver after you close it. This is normal and done to reduce the number of system calls.

    If you really need each write to be sent to the device try adding a call to fsync() after each write. Alternatively, since it's a char driver it is most likely being line buffered, try adding a '\n' at the end of each line.