Linux下实现了一个简单的进度条,主要技术啥的算不上,但有几个需要注意的点
- 首先是回车符,回车符可不是
,我们可以把
看成是两个动作的合体,分别是,回车和换行,都有自己对应的符号,这利用回车符一直在同一个位置输出造成动态的假象
- 因为没有用到
和换行,但是C语言的printf是行缓冲输出,什么意思呢?就是说不满一行不输出,就是靠
输出的,没有
只好强制把缓冲中的数据输出出来,这就要用到函数fflush()
#include<stdio.h>
#include<string.h>
#include<unistd.h>
void proc()
{
int rate=0;
const char *running="|/-\";
char p[102];
memset(p," ",sizeof(p));
while(rate<=100)
{
p[rate]="#";
printf("[%-102s][%d%%][%c]
",p,rate,running[rate%4]);
rate++;
fflush(stdout);
sleep(1);
}
}
int main()
{
proc();
return 0;
}
~附加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表pidhashstruct 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,giduid_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; //是否使用FPUchar 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 */};
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/131931.htm