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 | /* * hello-mod.c * a kernel-space client example of relayfs filesystem */ #include <linux/module.h> #include <linux/relayfs_fs.h> static struct rchan *hello_rchan; int init_module(void) { const char *msg="Hello world\n"; hello_rchan = relay_open("cpu", NULL, 8192, 2, NULL); if(!hello_rchan){ printk("relay_open() failed.\n"); return -ENOMEM; } relay_write(hello_rchan, msg, strlen(msg)); return 0; } void cleanup_module(void) { if(hello_rchan) { relay_close(hello_rchan); hello_rchan = NULL; } return; } MODULE_LICENSE ("GPL"); MODULE_DESCRIPTION ("Simple example of Relay"); |
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 | /* * audience.c * a user-space client example of relayfs filesystem */ #include <sys/types.h> #include <sys/stat.h> #include <sys/mount.h> #include <fcntl.h> #include <sched.h> #include <errno.h> #include <stdio.h> #define MAX_BUFLEN 256 const char filename_base[]="/mnt/relay/cpu"; // implement your own get_cputotal() before compilation static int get_cputotal(void); int main(void) { char filename[128]={0}; char buf[MAX_BUFLEN]; int fd, c, i, bytesread, cputotal = 0; if(mount("relayfs", "/mnt/relay", "relayfs", 0, NULL) && (errno != EBUSY)) { printf("mount() failed: %s\n", strerror(errno)); return 1; } cputotal = get_cputotal(); if(cputotal <= 0) { printf("invalid cputotal value: %d\n", cputotal); return 1; } for(i=0; i<cputotal; i++) { // open per-cpu file sprintf(filename, "%s%d", filename_base, i); fd = open(filename, O_RDONLY); if (fd < 0) { printf("fopen() failed: %s\n", strerror(errno)); return 1; } // read per-cpu file bytesread = read(fd, buf, MAX_BUFLEN); while(bytesread > 0) { buf[bytesread] = '\0'; puts(buf); bytesread = read(fd, buf, MAX_BUFLEN); }; // close per-cpu file if(fd > 0) { close(fd); fd = 0; } } if(umount("/mnt/relay") && (errno != EINVAL)) { printf("umount() failed: %s\n", strerror(errno)); return 1; } return 0; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |