From b2da6ac9deb5616d723d99116d6fe9e43bf3ef04 Mon Sep 17 00:00:00 2001 From: gatieme Date: Wed, 25 May 2016 18:25:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86proc=5Fcreate?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E7=9A=84=E7=94=A8=E6=88=B7=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=AF=BB=E5=86=99=E7=A8=8B=E5=BA=8F...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- study/proc/proc_create/Makefile | 4 +-- study/proc/proc_create/test.c | 61 +++++++++++++++++++++++++++++++++ study/proc/proc_create/test2.c | 59 +++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 study/proc/proc_create/test.c create mode 100644 study/proc/proc_create/test2.c diff --git a/study/proc/proc_create/Makefile b/study/proc/proc_create/Makefile index 9dc31ae..22e24b3 100644 --- a/study/proc/proc_create/Makefile +++ b/study/proc/proc_create/Makefile @@ -4,8 +4,8 @@ obj-m := jif.o else -KERNELDIR ?= /lib/modules/$(shell uname -r)/build -#KERNELDIR ?= /home/gatieme/Work/Kernel/linux-headers-3.14-desktop/ +#KERNELDIR ?= /lib/modules/$(shell uname -r)/build +KERNELDIR ?= /home/gatieme/Work/Kernel/linux-headers-3.14-desktop/ PWD := $(shell pwd) all: diff --git a/study/proc/proc_create/test.c b/study/proc/proc_create/test.c new file mode 100644 index 0000000..19b0c12 --- /dev/null +++ b/study/proc/proc_create/test.c @@ -0,0 +1,61 @@ +/************************************************************************* + > File Name: test.c + > Author: gatieme + > Created Time: 2016年05月24日 星期二 19时45分38秒 + ************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define PROC_FILE "/proc/jif" +#define MAX_LINE 81 + +int main( ) +{ + int ret; + int procFile; + char buff[MAX_LINE]; + + //wait for ack signal + procFile = open(PROC_FILE, O_RDWR); + if(procFile == -1) + { + perror("Fail to open "PROC_FILE); + exit(-1); + } + else + { + printf("Open success...\n"); + } + + if((ret = write(procFile, "hello", strlen("hello"))) == -1) + { + perror("Fail to read "PROC_FILE); + } + else + { + printf("wriet success\n"); + } + + bzero(buff, sizeof(buff)); + if((ret = read(procFile, buff, MAX_LINE)) == -1) + { + perror("Fail to read "PROC_FILE); + } + else + { + printf("buff = %s\n", buff); + } + + close(procFile); +} diff --git a/study/proc/proc_create/test2.c b/study/proc/proc_create/test2.c new file mode 100644 index 0000000..64bf943 --- /dev/null +++ b/study/proc/proc_create/test2.c @@ -0,0 +1,59 @@ +/************************************************************************* + > File Name: test.c + > Author: gatieme + > Created Time: 2016年05月24日 星期二 19时45分38秒 + ************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define PROC_FILE "/proc/jif" +#define MAX_LINE 81 + +int main( ) +{ + int ret; + int count; + int procFile; + char buff[MAX_LINE]; + + bzero(buff, sizeof(buff)); + sprintf(buff, "echo \"hello\" > %s", PROC_FILE); + printf("%s\n", buff); + system(buff); + printf("write success...\n"); + //wait for ack signal + procFile = open(PROC_FILE, O_RDONLY); + if(procFile == -1) + { + perror("Fail to open "PROC_FILE); + exit(-1); + } + else + { + printf("Open success...\n"); + } + + bzero(buff, sizeof(buff)); + ret = read(procFile, buff, MAX_LINE); + if(ret == -1) + { + perror("Fail to read " PROC_FILE); + } + else + { + printf("buff = %s\n", buff); + } + + close(procFile); +}