歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux下進度條的編寫和實現

Linux下進度條的編寫和實現

日期:2017/3/1 11:48:07   编辑:關於Linux

  Linux下實現了一個簡單的進度條,主要技術啥的算不上,但有幾個需要注意的點

  首先是回車符,回車符可不是\n,我們可以把\n看成是兩個動作的合體,分別是,回車和換行,都有自己對應的符號,這利用回車符一直在同一個位置輸出造成動態的假象

  因為沒有用到\n和換行,但是C語言的printf是行緩沖輸出,什麼意思呢?就是說不滿一行不輸出,就是靠\n輸出的,沒有\n只好強制把緩沖中的數據輸出出來,這就要用到函數fflush()

 #include<stdio.h>                                                                
#include<string.h>                                                              
#include<unistd.h>                                                              
                                                                                
void proc()                                                                      
{                                                                                
  int rate=0;                                                                    
  const char *running="|/-\\";                                                                                                                  
  char p[102];                                                                  
  memset(p,'\0',sizeof(p));                                                      
                                                                                
  while(rate<=100)                                                              
  {                                                                              
    p[rate]='#';                                                                
    printf("[%-102s][%d%%][%c]\r",p,rate,running[rate%4]);                      
    rate++;                                                                      
    fflush(stdout);                                                              
    sleep(1);                                                                    
  }                                                                              
}                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
int main()                                                                      
{                                                                                
  proc();                                                                        
  return 0;                                                                      
}                                                                                
~
Linux編寫進度條

Linux編寫進度條  附加task_struct的定義,

  因為每一個PCB都是這樣的, 只有這些結構, 才能滿足一個進程的所有要求。打開/include/linux/sched.h可以找到task_struct 的定義

struct task_struct {

 

volatile long state; /*說明了該進程是否可以執行,還是可中斷等信息*/

unsigned long flags; /*Flage 是進程號,在調用fork()時給出*/

int sigpending; /*進程上是否有待處理的信號*/

mm_segment_t addr_limit;

/**********************************************************/

/**進程地址空間,區分內核進程與普通進程在內存存放的位置不同*/

/****0-0xBFFFFFFF for user-thead    ***********************/

/****0-0xFFFFFFFF for kernel-thread ***********************/

/**********************************************************/

volatile long need_resched;

/**********************************************************/

/**********調度標志,表示該進程是否需要重新調度,************/

/**********若非0,則當從內核態返回到用戶態,會發生調度*******/

/**********************************************************/

 

int lock_depth; /*********************鎖深度***************/

long nice; /*************進程的基本時間片******************/

unsigned long policy;

/**********************************************************/

/*進程的調度策略,有三種************************************/

/*實時進程:SCHED_FIFO,SCHED_RR*****************************/

/*分時進程:SCHED_OTHER*************************************/

/**********************************************************/

/**********************************************************/

struct mm_struct *mm; //進程內存管理信息

int processor;

/**********************************************************/

/*若進程不在任何CPU上運行,

/*cpus_runnable 的值是0,否則是1。

/*這個值在運行隊列被鎖時更新.*/

/**********************************************************/

unsigned long cpus_runnable, cpus_allowed;

struct list_head run_list; /****指向運行隊列的指針*********/

unsigned long sleep_time; /*****進程的睡眠時間*************/

struct task_struct *next_task, *prev_task;

/**********************************************************/

/*用於將系統中所有的進程連成一個雙向循環鏈表*/

/*其根是init_task.*/

/**********************************************************/

struct mm_struct *active_mm;

struct list_head local_pages;/**指向本地頁面***************/

unsigned int allocation_order, nr_local_pages;

struct linux_binfmt *binfmt;/*進程所運行的可執行文件的格式*/

int exit_code, exit_signal;

int pdeath_signal;/*父進程終止是向子進程發送的信號*********/

unsigned long personality;

/*Linux可以運行由其他UNIX操作系統生成的符合iBCS2標准的程序*/

int did_exec:1;

/**********************************************************/

/*按POSIX要求設計的布爾量,區分進程正在執行從***************/

/*父進程中繼承的代碼,還是執行由execve裝入的新程序代碼******/

/**********************************************************/

pid_t pid;/**********進程標識符,用來代表一個進程***********/

pid_t pgrp;/********進程組標識,表示進程所屬的進程組********/

pid_t tty_old_pgrp;/*******進程控制終端所在的組標識********/

pid_t session;/*************進程的會話標識*****************/

pid_t tgid;

int leader; /*************標志,表示進程是否為會話主管******/

struct task_struct *p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr;

struct list_head thread_group; /****線程鏈表***************/

struct task_struct *pidhash_next;/*用於將進程鏈入HASH表pidhash

struct task_struct **pidhash_pprev;

wait_queue_head_t wait_chldexit; /*供wait4()使用***********/

struct completion *vfork_done; /* 供vfork() 使用***********/

unsigned long rt_priority;

/****實時優先級,用它計算實時進程調度時的weight值,/*******/

/*it_real_value,it_real_incr用於REAL定時器,單位為jiffies*/

系統根據it_real_value //設置定時器的第一個終止時間。

在定時器到期時,向進程發送SIGALRM信號,同時根據

it_real_incr重置終止時間,it_prof_value,it_prof_incr

用於Profile定時器,單位為jiffies。當進程運行時,

不管在何種狀態下,每個tick都使it_prof_value值減一,

當減到0時,向進程發送信號SIGPROF,並根據it_prof_incr重置時間

it_virt_value,it_virt_value用於Virtual定時器,單位為jiffies。

當進程運行時,不管在何種狀態下,每個tick都使it_virt_value值減一

當減到0時,向進程發送信號SIGVTALRM,根據it_virt_incr重置初值。

Real定時器根據系統時間實時更新,不管進程是否在運行

Virtual定時器只在進程運行時,根據進程在用戶態消耗的時間更新

Profile定時器在進程運行時,根據進程消耗的時

(不管在用戶態還是內核態)更新*****************************/

unsigned long it_real_value, it_prof_value, it_virt_value;

unsigned long it_real_incr, it_prof_incr, it_virt_value;

struct timer_list real_timer;//指向實時定時器的指針

struct tms times; //記錄進程消耗的時間,

unsigned long start_time;//進程創建的時間

long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; //記錄進程在每個CPU上所消耗的用戶態時間和核心態時間

/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */

//內存缺頁和交換信息:

//min_flt, maj_flt累計進程的次缺頁數(Copy on Write頁和匿名頁)和主缺頁數(從映射文件或交換設備讀入的頁面數);

//nswap記錄進程累計換出的頁面數,即寫到交換設備上的頁面數。

//cmin_flt, cmaj_flt, cnswap記錄本進程為祖先的所有子孫進程的累計次缺頁數,主缺頁數和換出頁面數。在父進程

//回收終止的子進程時,父進程會將子進程的這些信息累計到自己結構的這些域中

unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;

int swappable:1; //表示進程的虛擬地址空間是否允許換出

/* process credentials *////進程認證信息

//uid,gid為運行該進程的用戶的用戶標識符和組標識符,通常是進程創建者的uid,gid //euid,egid為有效uid,gid

//fsuid,fsgid為文件系統uid,gid,這兩個ID號通常與有效uid,gid相等,在檢查對於文件系統的訪問權限時使用他們。

//suid,sgid為備份uid,gid

uid_t uid,euid,suid,fsuid;

gid_t gid,egid,sgid,fsgid;

int ngroups; //記錄進程在多少個用戶組中

gid_t groups[NGROUPS]; //記錄進程所在的組

kernel_cap_t cap_effective, cap_inheritable, cap_permitted;//進程的權能,分別是有效位集合,繼承位集合,允許位集合

int keep_capabilities:1;

struct user_struct *user;

/* limits */

struct rlimit rlim[RLIM_NLIMITS]; //與進程相關的資源限制信息

unsigned short used_math; //是否使用FPU

char comm[16]; //進程正在運行的可執行文件名

/* file system info *///文件系統信息

int link_count, total_link_count;

struct tty_struct *tty; /* NULL if no tty 進程所在的控制終端,如果不需要控制終端,則該指針為空*/

unsigned int locks; /* How many file locks are being held */

/* ipc stuff *///進程間通信信息

struct sem_undo *semundo; //進程在信號燈上的所有undo操作

struct sem_queue *semsleeping; //當進程因為信號燈操作而掛起時,他在該隊列中記錄等待的操作

/* CPU-specific state of this task *///進程的CPU狀態,切換時,要保存到停止進程的

task_struct中

struct thread_struct thread;

/* filesystem information文件系統信息*/

struct fs_struct *fs;

/* open file information *///打開文件信息

struct files_struct *files;

/* signal handlers *///信號處理函數

spinlock_t sigmask_lock; /* Protects signal and blocked */

struct signal_struct *sig; //信號處理函數,

sigset_t blocked; //進程當前要阻塞的信號,每個信號對應一位

struct sigpending pending; //進程上是否有待處理的信號

unsigned long sas_ss_sp;

size_t sas_ss_size;

int (*notifier)(void *priv);

void *notifier_data;

sigset_t *notifier_mask;/* Thread group tracking */

u32 parent_exec_id;

u32 self_exec_id;

/* Protection of (de-)allocation: mm, files, fs, tty */

spinlock_t alloc_lock;

void *journal_info;/* journalling filesystem info */

};

Copyright © Linux教程網 All Rights Reserved