-
Notifications
You must be signed in to change notification settings - Fork 0
/
userspace.c
67 lines (58 loc) · 1.81 KB
/
userspace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#define NUM_THREADS 2
#define BUFFER_LENGTH 256 ///< The buffer length (crude but fine)
static char receive[BUFFER_LENGTH]; ///< The receive buffer from the LKM
void *thread_function(void *ptr) //Thread Function
{
int ret,fd;
char stringToSend[BUFFER_LENGTH];
int *myid = (int *)ptr;
fd=open("/dev/kernelDriver",O_RDWR);
if (fd < 0)
perror("Failed to open the device...");
printf("Running thread:[%d]\n",*myid);
printf("Type in a short string to send to the kernel module:\n");
scanf("%[^\n]%*c",stringToSend);
printf("Writing message to the device [%s].\n",stringToSend);
ret=write(fd,stringToSend,strlen(stringToSend));
if (ret < 0)
perror("Failed to write the message to the device.");
printf("Press ENTER to read back from the device...\n");
getchar();
printf("Reading from the device...\n");
ret = read(fd, receive, BUFFER_LENGTH); // Read the response from the LKM
if (ret < 0)
perror("Failed to read the message from the device.");
printf("The received message is: [%s]\n", receive);
//Close the device
int err = close(fd);
if (err < 0)
perror("Error closing file.");
}
int main(int argc,char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc,i,j;
printf("Starting device code example...\n");
//Create Threads
for (i=0;i<NUM_THREADS;i++)
{
rc=pthread_create(&threads[i],NULL,thread_function,(void *)&threads[i]);
if(rc)
{
printf("ERROR;return code from pthread_create()is %d\n",rc);
exit(-1);
}
//Wait for the thread to terminate
pthread_join( threads[i],NULL);
}
pthread_exit(NULL); //Terminates the thread
printf("End of the program\n");
return 0;
}