<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    選擇java 進入自由開放的國度

    隨筆 - 49, 文章 - 3, 評論 - 154, 引用 - 1
    數據加載中……

    linux中命令who的實現

    實現了linux中w ho 命令,采用了緩沖機制,一次從utmp文件中讀取16條數據,這樣可以大大提高性能。

    下面是代碼,采用了緩沖機制:
    ??1?/*
    ??2??*?what?is?diffient?to?who1?is?that?we?add?a?buffer?area?in?who2.c.
    ??3??*??In?this?way,?the?efferency?is?improved?.
    ??4??*/
    ??5?#include?<stdio.h>
    ??6?#include?<utmp.h>
    ??7?#include?<fcntl.h>
    ??8?#include?<unistd.h>
    ??9?#include?<stdlib.h>
    ?10?#include?<time.h>
    ?11?
    ?12?#define?SHOWHOST??????????//include?remote?machine?on?output
    ?13?
    ?14?//-------------------------------------------------------------------------
    ?15?#define?NRECS???16
    ?16?#define?NULLUT?((struct?utmp?*)NULL)
    ?17?#define?UTSIZE?(sizeof(struct?utmp))
    ?18?
    ?19?static?char?utmpbuf[NRECS?*?UTSIZE];????????????//storage
    ?20?static?int??num_recs;???????????????????????????//num?stored
    ?21?static?int??cur_rec;????????????????????????????//next?to?go
    ?22?static?int??fd_utmp?=?-1;???????????????????????//read?from
    ?23?
    ?24?int?utmp_open(char?*filename)
    ?25?{
    ?26???fd_utmp?=?open(filename,?O_RDONLY);
    ?27???cur_rec?=?num_recs?=?0;
    ?28???return?fd_utmp;
    ?29?}
    ?30?
    ?31?int?utmp_reload()
    ?32?{
    ?33???int?amt_read;
    ?34???amt_read?=?read(fd_utmp,?utmpbuf,?NRECS?*?UTSIZE);
    ?35???num_recs?=?amt_read/UTSIZE;????????????????????????//how?many?did?we?get?
    ?36???cur_rec?=?0;???????????????????????????????????????//reset?pointer
    ?37???
    ?38???return?num_recs;
    ?39?}
    ?40?
    ?41?struct?utmp?*?utmp_next()
    ?42?{
    ?43???struct?utmp?*?recp;
    ?44???if(?fd_utmp?==?-1)?return?NULLUT;
    ?45???
    ?46???if(cur_rec?==?num_recs?&&?utmp_reload()?==?0)?return?NULLUT;
    ?47???
    ?48???recp?=?(struct?utmp?*)?&utmpbuf[cur_rec?*?UTSIZE];
    ?49???cur_rec++;
    ?50???return?recp;
    ?51?}
    ?52?
    ?53?void?utmp_close()
    ?54?{
    ?55???if(fd_utmp?!=?-1)?close(fd_utmp);??//don't?close?if?not?open
    ?56?}
    ?57?
    ?58?//-------------------------------------------------------------------------
    ?59?
    ?60?void?show_info(struct?utmp?*);
    ?61?void?showtime(long?timeval);
    ?62?
    ?63?int?main()
    ?64?{
    ?65???struct?utmp?*?utbufp;???????//holds?pointer?to?next?rec
    ?66???struct?utmp?*?utmp_next();??//return?pointer?to?next
    ?67???
    ?68???if(utmp_open(UTMP_FILE)?==?-1)
    ?69???{
    ?70?????perror(UTMP_FILE);
    ?71?????exit(1);
    ?72???}
    ?73???while?(?(utbufp?=?utmp_next()?)?!=?((struct?utmp?*)?NULL)?)
    ?74?????show_info(utbufp);
    ?75???
    ?76???utmp_close();
    ?77????
    ?78????return?0;
    ?79???
    ?80?}
    ?81?
    ?82?/*
    ?83??*?display?contents?of?the?utmp?struct?
    ?84??*/
    ?85?void?show_info(?struct?utmp?*?utbufp)
    ?86?{
    ?87???/*
    ?88????*?remove?the?blank?record
    ?89????*??the?entry?ut_type?in?struct?utmp?indicate?the?types
    ?90????*???USER_PROCESS?is?the?auser?process
    ?91????*/
    ?92???if(utbufp->?ut_type?!=?USER_PROCESS)?return;
    ?93????
    ?94???printf("%?-8.8s",?utbufp->ut_name);
    ?95???printf("?");
    ?96???printf("%?-12.12s",?utbufp->ut_line);
    ?97???printf("?");
    ?98???//printf("%101d",?utbufp->ut_time);
    ?99???showtime(utbufp->ut_time);
    100???printf("?");
    101???#ifdef?SHOWHOST
    102?????printf("(%s)",?utbufp->ut_host);
    103???#endif
    104???printf("\n");??
    105?}
    106?
    107?void?showtime(long?timeval)
    108?{
    109???/*
    110????*?display?time
    111????*/
    112????char?*cp;
    113????cp?=?ctime(&timeval);
    114????printf("%12.12s",?cp+4);
    115?}
    116?
    下面的代碼沒有采用緩沖機制,每次讀取一條記錄:
    ?1?#include?<stdio.h>
    ?2?#include?<utmp.h>
    ?3?#include?<fcntl.h>
    ?4?#include?<unistd.h>
    ?5?#include?<stdlib.h>
    ?6?#include?<time.h>
    ?7?
    ?8?#define?SHOWHOST??????????//include?remote?machine?on?output
    ?9?
    10?void?show_info(struct?utmp?*);
    11?void?showtime(long?timeval);
    12?
    13?int?main()
    14?{
    15???struct?utmp?current_record;????//read?info?into?here
    16???int??????????????utmpfd;???????????//read?from?this?descriptor
    17???int????????????reclen?=?sizeof(current_record);??//how?many?bytes?to?read
    18???
    19???if(?(utmpfd?=?open(UTMP_FILE,?O_RDONLY))?==?-1)
    20???{
    21?????perror(UTMP_FILE);??//UTMP_FILE?is?in?utmp.h
    22?????exit(1);
    23???}
    24?????/*
    25????*?read?data?structure?from?UTMP_FILE
    26????*/
    27???while(?read(utmpfd,?&current_record,?reclen)?==?reclen)
    28?????????show_info(&current_record);
    29???
    30???close(utmpfd);
    31???
    32?
    33??//?return?0;
    34?}
    35?
    36?/*
    37??*?display?contents?of?the?utmp?struct?
    38??*/
    39?void?show_info(?struct?utmp?*?utbufp)
    40?{
    41???/*
    42????*?remove?the?blank?record
    43????*??the?entry?ut_type?in?struct?utmp?indicate?the?types
    44????*???USER_PROCESS?is?the?auser?process
    45????*/
    46???if(utbufp->?ut_type?!=?USER_PROCESS)?return;
    47????
    48???printf("%?-8.8s",?utbufp->ut_name);
    49???printf("?");
    50???printf("%?-12.12s",?utbufp->ut_line);
    51???printf("?");
    52???//printf("%101d",?utbufp->ut_time);
    53???showtime(utbufp->ut_time);
    54???printf("?");
    55???#ifdef?SHOWHOST
    56?????printf("(%s)",?utbufp->ut_host);
    57???#endif
    58???printf("\n");??
    59?}
    60?
    61?void?showtime(long?timeval)
    62?{
    63???/*
    64????*?display?time
    65????*/
    66????char?*cp;
    67????cp?=?ctime(&timeval);
    68????printf("%12.12s",?cp+4);
    69?}
    70?

    可以對比一下性能,記錄少的情況下不明顯,但是在記錄超過1000條時就可以明顯看出來了。這個大家可以做一下實驗。

    這個who命令實現的還算比較完美。

    posted on 2006-03-27 08:41 soochow_hhb 以java論成敗 以架構論英雄 閱讀(1312) 評論(3)  編輯  收藏 所屬分類: Reading

    評論

    #  linux中命令who實現的是什么功能  回復  更多評論   

    請問: linux中命令who實現的是什么功能
    2006-06-17 20:25 | ear

    # re: linux中命令who的實現  回復  更多評論   

    who命令的功能是列出當前登錄到服務器的用戶,及其屬性(如IP地址,終端類型等)。

    # re: linux中命令who的實現  回復  更多評論   

    UTMP_FILE 這個在那定義的啊 。utmp.h中也找不到啊 。
    2010-03-01 21:44 | jaychenfu
    主站蜘蛛池模板: 国产小视频在线免费| 毛片免费全部播放一级| 亚洲中文久久精品无码ww16| 亚洲AV无码XXX麻豆艾秋| 好吊妞在线新免费视频| 亚洲首页国产精品丝袜| 啦啦啦www免费视频| 亚洲国产成人AV在线播放| 宅男666在线永久免费观看| 亚洲av中文无码乱人伦在线观看| 日韩电影免费在线| 色婷婷精品免费视频| 国产精品亚洲二区在线观看| 成人免费一区二区三区| 久久精品国产亚洲夜色AV网站| 免费看又黄又无码的网站| 亚洲av无码国产综合专区| 永久久久免费浮力影院| 尤物视频在线免费观看| 亚洲不卡av不卡一区二区| 成人浮力影院免费看| 亚洲精品成a人在线观看☆| www.亚洲色图.com| 91免费精品国自产拍在线不卡| 亚洲一区中文字幕在线电影网 | 成人免费的性色视频| 人人狠狠综合久久亚洲 | 老司机亚洲精品影院| 一色屋成人免费精品网站 | 国产免费高清69式视频在线观看| 国产日韩亚洲大尺度高清| 桃子视频在线观看高清免费完整| 亚洲国产区男人本色| 国产亚洲精品线观看动态图| 最好看的中文字幕2019免费| 亚洲精品色播一区二区| 亚洲VA中文字幕不卡无码| 国产精品成人免费一区二区| 精品无码一级毛片免费视频观看 | 美女被cao免费看在线看网站| 老司机午夜免费视频|