]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
proc
authorseantywork <seantywork@gmail.com>
Wed, 19 Nov 2025 08:09:21 +0000 (08:09 +0000)
committerseantywork <seantywork@gmail.com>
Wed, 19 Nov 2025 08:09:21 +0000 (08:09 +0000)
kproc/kproc.c
kproc/kproc.h
kproc/user/Makefile
kproc/user/user.c

index 8647557ac1ea73b82b083529ad2d9b4f75647ecc..0cd7a9dfd6f4bb4ee333772e63628fb8b9da309e 100644 (file)
@@ -16,8 +16,6 @@
 
 #include "kproc.h"
 
-#define DEVICE_NAME "kproc_f"
-
 static struct work_struct job;
 
 static int dev_major;
index 0c7da2ba2b76ad981ad2597d85f168fa1c9a80eb..d11d5594b01490327fad4fc4494614180f3333d9 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _KPROC_H_
 #define _KPROC_H_
 
+#define DEVICE_NAME "kproc_f"
 #define KPROC_BUFF_LEN 128
 
 #endif
\ No newline at end of file
index 18ffa8afa341cbfa83467e814023228d0dcc349f..7716b82098d84acd73dd0fd5eff854992e39701e 100644 (file)
@@ -1,4 +1,7 @@
+
+INCLUDES := -I../
+
 all:
-       gcc -g -o user.out user.c -lpthread
+       gcc -g $(INCLUDES) -o user.out user.c -lpthread
 clean:
        rm -rf *.out *.o
\ No newline at end of file
index 9009319104529d54dfbb4c81a6c32ce76534341e..02fe7b287c3cc9197282f6f174bcc8a0bbb5b800 100644 (file)
@@ -2,9 +2,81 @@
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <unistd.h>
 
-int main(){
+#include "kproc.h"
 
+FILE* fp = NULL;
+pthread_mutex_t mut;
+int keepalive = 1;
+
+void sig_hdl(int sig){
+    printf("SIG: %d\n", sig);
+    keepalive =0;
+}
+
+void* another_thread(void* varg){
+    while(keepalive){
+        pthread_mutex_lock(&mut);
+
+        pthread_mutex_unlock(&mut);
+        sleep(1);
+    }
+    pthread_exit(NULL);
+}
+
+int main(int argc, char** argv){
+    pthread_t tid;
+    int result = -1;
+    char fbuf[1024] = {0};
+    char* tbuf = NULL;
+    size_t rlen = 0;
+    int major = -1;
+    fp = fopen("/proc/devices", "r");
+    if(fp == NULL){
+        printf("failed to open proc devices\n");
+        return -1;
+    }
+    printf("devices:\n");
+    while(fgets(fbuf, 1024, fp)){
+        printf("%s", fbuf);
+        if((tbuf = strstr(fbuf, "loop")) != NULL){
+            tbuf -= 1;
+            *tbuf = 0;
+            tbuf -= 3;
+            printf("target major: %s\n", tbuf);
+            break;
+        }
+        memset(fbuf, 0, 1024 * sizeof(char));
+    }
+    fclose(fp);
+    if(result < 0){
+        printf("target not found\n");
+        return -1;
+    }
+    result = -1;
+    signal(SIGINT, sig_hdl);
+    fp = fopen(DEVICE_NAME, "r+");
+    if(fp == NULL){
+        printf("failed to open\n");
+        return -1;
+    }
+    pthread_mutex_init(&mut, NULL);
+    result = pthread_create(&tid, NULL, another_thread, NULL);
+    if(result < 0){
+        printf("failed to create thread\n");
+        return -1;
+    }
+    while(keepalive){
+        pthread_mutex_lock(&mut);
+
+
+        pthread_mutex_unlock(&mut);
+        sleep(1);
+    }
+    fclose(fp);
 
     return 0;
 }
\ No newline at end of file