Advertisement
Guest User

asdf

a guest
Sep 30th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 KB | None | 0 0
  1. /* Kevin Jafari
  2. ** Assignment 1 Operating Systems Design
  3. ** September 29, 2010
  4. ** This program has 10 functions that compute the following:
  5. ** -CPU type and model
  6. ** -Kernel version
  7. ** -Amount of time since the system was last booted, in the form dd:hh:mm:ss
  8. ** -The amount of time that the CPU has spent in user mode, in system mode, and idle.
  9. ** -The number of disk requests made on the system.
  10. ** -The number of context switches that the kernel has performed.
  11. ** -The time when the system was last booted.
  12. ** -The number of processes that have been created since the system was booted.
  13. ** -The amount of memory configured into this computer.
  14. ** -The amount of memory currently available.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <sys/utsname.h>
  22. #include <time.h>
  23. #include <syslog.h>
  24. #define USER_INPUT_SIZE 5
  25. #define ROW_SIZE 150
  26.  
  27. char* cpumodel();
  28. char* kernelver();
  29. char* boottimer();
  30.  
  31.  
  32. int main()
  33. {
  34. openlog("prog1", 0, LOG_USER);
  35.  
  36. char *str = (char *) malloc(sizeof(char)*250);
  37.  
  38. //#1
  39. str = cpumodel();
  40. printf("Cpu model and type: %s\n", str);
  41. syslog(LOG_INFO, "Cpu model and type: %s", str);
  42.  
  43. //#2
  44. str = kernelver();
  45. printf("Kernel version: %s\n", str);
  46. syslog(LOG_INFO, "Kernel version: %s", str);
  47.  
  48. //str =
  49. boottimer();
  50. //printf("%s", str);
  51.  
  52. /*//
  53. /*
  54. //#4
  55. FILE *fp;
  56. if ((fp = fopen("/proc/stat", "r")) ==NULL)
  57. {
  58. fprintf(stderr, "Error opening /proc/stat: %s", strerror(errno));
  59. return 1;
  60. }
  61.  
  62. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  63. row = fgets(row, ROW_SIZE, fp);
  64.  
  65. row = strtok(row, " ");
  66. char *normpromode;
  67. char *nicepromode;
  68. char *sysmode;
  69. char *idle;
  70.  
  71. char *strtoulPtr;
  72. int normtime, nicetime;
  73.  
  74. if ( row != NULL )
  75. {
  76. normpromode = strtok(NULL, " ");
  77.  
  78. if ( normpromode != NULL)
  79. {
  80. nicepromode = strtok(NULL, " ");
  81.  
  82. if ( nicepromode != NULL)
  83. {
  84. sysmode = strtok(NULL, " ");
  85.  
  86. if ( sysmode != NULL)
  87. {
  88. idle = strtok(NULL, " ");
  89.  
  90. if ( idle != NULL )
  91. {
  92. normtime = strtoul(normpromode, strtoulPtr, 10);
  93. nicetime = strtoul(nicepromode, strtoulPtr, 10);
  94.  
  95. printf("CPU Times:\nUser mode: %d\nSystem mode: %s\nIdle: %s", (normtime + nicetime), sysmode, idle);
  96. }
  97. else
  98. {
  99. fprintf(stderr, "Error retrieving idle time info from /proc/stat.");
  100. return 1;
  101. }
  102. }
  103. else
  104. {
  105. fprintf(stderr, "Error retrieving system process time info from /proc/stat.");
  106. return 1;
  107. }
  108. }
  109. else
  110. {
  111. fprintf(stderr, "Error retrieving niced process time info from /proc/stat.");
  112. return 1;
  113. }
  114. }
  115. else
  116. {
  117. fprintf(stderr, "Error retrieving normal process time info from /proc/stat.");
  118. return 1;
  119. }
  120. }
  121. else
  122. {
  123. fprintf(stderr, "Error retrieving cpu info from /proc/stat.");
  124. return 1;
  125. }
  126.  
  127. fclose(fp);
  128. free(row);
  129. */
  130. /*
  131. //#5
  132. FILE *fp;
  133. if ((fp = fopen("/sys/block/sda/stat", "r")) == NULL)
  134. {
  135. fprintf(stderr, "Error opening /sys/block/sda/stat: %s", strerror(errno));
  136. return 1;
  137. }
  138.  
  139. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  140. row = fgets(row, ROW_SIZE, fp);
  141.  
  142. char *read, *written, *readmerge, *writtenmerge, *readsec, *writtensec, *current, *strtoulPtr;
  143. int readint, writtenint, readmergeint, writtenmergeint, readsecint, writtensecint, currentint;
  144.  
  145. row = strtok(row, " ");
  146.  
  147. int cnt=1;
  148. while( row != NULL )
  149. {
  150. switch(cnt)
  151. {
  152. case 1:
  153. read = row;
  154. break;
  155. case 2:
  156. readmerge = row;
  157. break;
  158. case 3:
  159. readsec = row;
  160. break;
  161. case 5:
  162. written = row;
  163. break;
  164. case 6:
  165. writtenmerge = row;
  166. break;
  167. case 7:
  168. writtensec = row;
  169. break;
  170. case 9:
  171. current = row;
  172. break;
  173. }
  174. row = strtok(NULL, " ");
  175. cnt++;
  176.  
  177. }
  178.  
  179. if (read == NULL ||
  180. readmerge == NULL ||
  181. readsec == NULL ||
  182. written == NULL ||
  183. writtenmerge == NULL ||
  184. writtensec == NULL ||
  185. current == NULL)
  186. {
  187. fprintf(stderr, "Error retrieving disk info from /sys/block/sda/stat.");
  188. return 1;
  189. }
  190.  
  191. readint = strtoul(read, strtoulPtr, 10);
  192. readmergeint = strtoul(readmerge, strtoulPtr, 10);
  193. readsecint = strtoul(readsec, strtoulPtr, 10);
  194. writtenint = strtoul(written, strtoulPtr, 10);
  195. writtenmergeint = strtoul(writtenmerge, strtoulPtr, 10);
  196. writtensecint = strtoul(writtensec, strtoulPtr, 10);
  197. currentint = strtoul(current, strtoulPtr, 10);
  198.  
  199. printf("read: %d\nreadmerge: %d\nreadsec: %d\nwritten %d\nwrittenmerge: %d\nwrittensec: %d\ncurrent: %d\n",
  200. readint, readmergeint, readsecint, writtenint, writtenmergeint, writtensecint, currentint);
  201.  
  202. printf("_____________________\n");
  203. printf("Total disk read/writen: %d\n", (readint+writtenint+currentint-readmergeint-writtenmergeint));
  204. printf("Total disk sectors read/written : %d\n", (readsecint+writtensecint));
  205.  
  206. fclose(fp);
  207. free(row);
  208. */
  209. /*//6
  210. FILE *fp;
  211. if ((fp = fopen("/proc/stat", "r")) ==NULL)
  212. {
  213. fprintf(stderr, "Error opening /proc/stat: %s", strerror(errno));
  214. return 1;
  215. }
  216.  
  217. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  218. row = fgets(row, ROW_SIZE, fp);
  219.  
  220. char *pattern = "ctxt";
  221.  
  222. while(!feof(fp))
  223. {
  224. row = fgets(row, ROW_SIZE, fp);
  225.  
  226. if(strstr(row, pattern))
  227. {
  228. break;
  229. }
  230. }
  231.  
  232. row = strtok(row, " ");
  233.  
  234.  
  235. if ( row != NULL)
  236. {
  237. row = strtok(NULL," ");
  238. }
  239. else
  240. {
  241. fprintf(stderr, "Error reading context switches from /proc/stat: %s");
  242. return 1;
  243. }
  244.  
  245. printf("Number of context switches: %s\n",row);
  246.  
  247. fclose(fp);
  248. //free(row);
  249. */
  250. /*
  251. //#7
  252. FILE *fp;
  253. if ((fp = fopen("/proc/uptime", "r")) ==NULL)
  254. {
  255. fprintf(stderr, "Error opening /proc/uptime: %s", strerror(errno));
  256. return 1;
  257. }
  258. char *row = (char*)malloc(250*sizeof(char));
  259.  
  260. row = fgets(row, 250, fp);
  261.  
  262. row = strtok(row, " ");
  263.  
  264. char *strtoulPtr;
  265.  
  266. int timer = strtoul(row, &strtoulPtr, 10);
  267.  
  268. time_t totaltime = time(NULL) ;//- timer;
  269.  
  270. printf("Time when system was booted:\n%s", asctime(localtime(&totaltime)));
  271.  
  272. fclose(fp);
  273. free(row);
  274. */
  275. /*
  276. //8
  277. FILE *fp;
  278. if ((fp = fopen("/proc/stat", "r")) ==NULL)
  279. {
  280. fprintf(stderr, "Error opening /proc/stat: %s", strerror(errno));
  281. return 1;
  282. }
  283.  
  284. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  285. row = fgets(row, ROW_SIZE, fp);
  286.  
  287. char *pattern = "processes";
  288.  
  289. while(!feof(fp))
  290. {
  291. row = fgets(row, ROW_SIZE, fp);
  292.  
  293. if(strstr(row, pattern))
  294. {
  295. break;
  296. }
  297.  
  298. }
  299.  
  300. row = strtok(row, " ");
  301.  
  302.  
  303. if ( row != NULL)
  304. {
  305. row = strtok(NULL," ");
  306. }
  307. else
  308. {
  309. fprintf(stderr, "Error reading processes from /proc/stat: %s");
  310. return 1;
  311. }
  312.  
  313. printf("Number of processes since system was booted: %s\n",row);
  314.  
  315. fclose(fp);
  316. //free(row);
  317. */
  318. /*//9
  319.  
  320. FILE *fp;
  321. if ((fp = fopen("/proc/meminfo", "r")) ==NULL)
  322. {
  323. fprintf(stderr, "Error opening /proc/meminfo: %s", strerror(errno));
  324. return 1;
  325. }
  326. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  327. char *pattern = "MemTotal", *strtoulPtr;
  328.  
  329.  
  330. while(!feof(fp))
  331. {
  332. row = fgets(row, ROW_SIZE, fp);
  333.  
  334. if(strstr(row, pattern))
  335. {
  336. break;
  337. }
  338.  
  339. }
  340.  
  341. char *extract = strstr(row,": ");
  342.  
  343. extract += 1;
  344.  
  345. int memnum = strtoul(extract, strtoulPtr, 10);
  346.  
  347. printf("Total system memory is: %d kB\n", memnum);
  348.  
  349. fclose(fp);
  350. free(row);
  351.  
  352. /*
  353. //10
  354. FILE *fp;
  355. if ((fp = fopen("/proc/meminfo", "r")) ==NULL)
  356. {
  357. fprintf(stderr, "Error opening /proc/meminfo: %s", strerror(errno));
  358. return 1;
  359. }
  360. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  361. char *pattern = "MemFree", *strtoulPtr;
  362.  
  363.  
  364. while(!feof(fp))
  365. {
  366. row = fgets(row, ROW_SIZE, fp);
  367.  
  368. if(strstr(row, pattern))
  369. {
  370. break;
  371. }
  372. }
  373.  
  374. char *extract = strstr(row,": ");
  375.  
  376. extract += 1;
  377.  
  378. int memnum = strtoul(extract, strtoulPtr, 10);
  379.  
  380. printf("Available memory is: %d kB\n", memnum);
  381.  
  382. fclose(fp);
  383. free(row);
  384. */
  385.  
  386. return 0;
  387. }
  388.  
  389. char* cpumodel() //#1
  390. {
  391.  
  392. FILE *fp;
  393. if ((fp = fopen("/proc/cpuinfo", "r")) ==NULL)
  394. {
  395. fprintf(stderr, "Error opening /proc/cpuinfo: %s", strerror(errno));
  396. return NULL;
  397. //
  398. }
  399.  
  400. char *row = (char*)malloc(ROW_SIZE*sizeof(char));
  401. char *pattern = "model name";
  402.  
  403. while(!feof(fp))
  404. {
  405. row = fgets(row, ROW_SIZE, fp);
  406.  
  407. if(strstr(row, pattern))
  408. {
  409. break;
  410. }
  411. }
  412.  
  413. char *extract = strstr(row,": ");
  414. extract += 2;
  415.  
  416. fclose(fp);
  417.  
  418. return extract;
  419. }
  420.  
  421. char* kernelver()
  422. {
  423. struct utsname kernel;
  424.  
  425. int struDis = uname (&kernel);
  426.  
  427. if (struDis == -1)
  428. {
  429. fprintf(stderr, "Error using uname function: %s", strerror(errno));
  430. return NULL;
  431. }
  432.  
  433. char *returnStr = (char *) malloc(sizeof(char)*strlen(kernel.version+1));
  434.  
  435. strcpy(returnStr, kernel.version);
  436.  
  437. return (returnStr);
  438. }
  439.  
  440. char* boottimer()
  441. {
  442. FILE *fp;
  443. if ((fp = fopen("/proc/uptime", "r")) ==NULL)
  444. {
  445. fprintf(stderr, "Error opening /proc/uptime: %s", strerror(errno));
  446. return 1;
  447. }
  448. char *row = (char*)malloc(250*sizeof(char));
  449.  
  450. row = fgets(row, 250, fp);
  451.  
  452. row = strtok(row, " ");
  453.  
  454. char *strtoulPtr;
  455.  
  456. int time = strtoul(row, &strtoulPtr, 10);
  457.  
  458. int dd, hh, mm, ss = time;
  459. dd = ss /86400; //how many seconds in a day
  460. ss -= dd*86400;
  461. hh = ss /3600; //how many seconds in an hour
  462. ss -= hh*3600;
  463. mm = ss / 60;
  464. ss -= mm *60;
  465.  
  466. //printf("Amount of time since system was booted: (%02dd, %02dh, %02dm, %02ds) ", dd, hh, mm, ss);
  467.  
  468. char *returnStr = (char *) malloc(sizeof(char)*(100));
  469.  
  470. strcpy(returnStr, ("Amount of time since system was booted: (%02dd, %02dh, %02dm, %02ds) ", dd, hh, mm, ss) );
  471.  
  472. //printf("here %s\n", returnStr);
  473. //printf("here\n");
  474. //strcpy(returnStr, returnStr);
  475. //printf("here\n");
  476. fclose(fp);
  477.  
  478. return "hello";
  479.  
  480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement