| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #include <sys/types.h> |
|---|
| 11 | #include <sys/stat.h> |
|---|
| 12 | #include <fcntl.h> |
|---|
| 13 | #include <dirent.h> |
|---|
| 14 | #include <stdio.h> |
|---|
| 15 | #include <errno.h> |
|---|
| 16 | |
|---|
| 17 | #include <stdlib.h> |
|---|
| 18 | #include <string.h> |
|---|
| 19 | #include <unistd.h> |
|---|
| 20 | #include <sys/time.h> |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include "process.h" |
|---|
| 25 | #include "extern.h" |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | char * fastload(char *); |
|---|
| 32 | void fastlist(void); |
|---|
| 33 | |
|---|
| 34 | void fastlist(void) |
|---|
| 35 | { |
|---|
| 36 | DIR *directory; |
|---|
| 37 | struct dirent *entry; |
|---|
| 38 | int i = 0; |
|---|
| 39 | int fd; |
|---|
| 40 | |
|---|
| 41 | long start_time, total_time, end_time; |
|---|
| 42 | long start_time2, total_time2, end_time2; |
|---|
| 43 | |
|---|
| 44 | start_time = get_time(); |
|---|
| 45 | |
|---|
| 46 | directory = opendir("/proc"); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | while ( (entry = readdir(directory)) ) |
|---|
| 50 | { |
|---|
| 51 | char path[PATH_MAX]; |
|---|
| 52 | |
|---|
| 53 | char* buf; |
|---|
| 54 | int z; |
|---|
| 55 | |
|---|
| 56 | char tmpString[1024]; |
|---|
| 57 | int tmpInt; |
|---|
| 58 | long tmpLong; |
|---|
| 59 | char *cp; |
|---|
| 60 | |
|---|
| 61 | if (*entry->d_name < '0' || *entry->d_name > '9') |
|---|
| 62 | continue; |
|---|
| 63 | |
|---|
| 64 | sprintf(path, "/proc/%s/status", entry->d_name); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | start_time2 = get_time(); |
|---|
| 70 | |
|---|
| 71 | buf = fastload(path); |
|---|
| 72 | |
|---|
| 73 | end_time2 = get_time(); |
|---|
| 74 | |
|---|
| 75 | fprintf(stdout,"fastload(%s) %lu usec\n", path, end_time2 - start_time2); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | i++; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | free(buf); |
|---|
| 87 | } |
|---|
| 88 | total_time = get_time () - start_time; |
|---|
| 89 | |
|---|
| 90 | printf ("\nTime elapsed in fastlist() for %duSec processes=%d\n", total_time, i); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | char * fastload(char * filename) |
|---|
| 99 | { |
|---|
| 100 | int fd ; |
|---|
| 101 | int result; |
|---|
| 102 | char buffer[2048]; |
|---|
| 103 | long start_time, end_time; |
|---|
| 104 | int len; |
|---|
| 105 | char *newbuf; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | fd = open(filename,O_RDONLY); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | if (fd == -1) |
|---|
| 130 | { |
|---|
| 131 | fprintf(stderr,"Error while trying to open: %s\n", filename); |
|---|
| 132 | return NULL; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | result = read(fd, buffer, sizeof(buffer)); |
|---|
| 139 | close (fd); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | if (result == -1) |
|---|
| 147 | { |
|---|
| 148 | fprintf(stderr,"%s reading file %s\n", strerror(errno),filename); |
|---|
| 149 | return NULL; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | len = strlen(buffer); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | newbuf = (char*) malloc( len+1 ); |
|---|
| 156 | newbuf[0] = 0; |
|---|
| 157 | |
|---|
| 158 | strncpy(newbuf, buffer, (len) ); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | return newbuf; |
|---|
| 164 | } |
|---|