Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Linux进程管理(C语言)

理解进程控制的原理对于理解和修改fio project非常的重要。"fio is an I/O tool meant to be used both for benchmark and stress/hardware verification."进程
unix提供了大量的从c程序中操作系统的系统调用(别的语言应该也是有的吧)。创建进程
每一个进程都有一个正数的id,叫做pid。getpid函数返回调用进程的pid,getppid函数返回它的父进程的pid。forkexit父进程和子进程是并发运行的独立进程。内核能够以任意方式交替执行他们的逻辑控制流中的指令。如果能够在fork函数在父进程和子进程中返回后立即暂停这两个进程,我们会看到每个进程的地址空间都是相同的。(每个进程有相同的用户栈,相同的本地变量值,相同的堆,相同的全局变量值以及相同的代码。然而他们都有自己独立的地址空间)关于这个,fio这个程序利用到它的地方就是while (todo) {struct thread_data *map[REAL_MAX_JOBS];struct timeval this_start;int this_jobs = 0, left;for_each_td(td, i) {if (td->runstate != TD_NOT_CREATED)continue; 
 if (td->o.start_delay) {spent = utime_since_genesis(); if (td->o.start_delay > spent)continue;} if (td->o.stonewall && (nr_started || nr_running)) {dprint(FD_PROCESS, "%s: stonewall wait ",td->o.name);break;} init_disk_util(td); td->rusage_sem = fio_mutex_init(FIO_MUTEX_LOCKED);td->update_rusage = 0; /* * Set state to created. Thread will transition * to TD_INITIALIZED when it"s done setting up. */td_set_runstate(td, TD_CREATED);map[this_jobs++] = td;nr_started++;            ...if (td->o.use_thread) {int ret; dprint(FD_PROCESS, "will pthread_create ");ret = pthread_create(&td->thread, NULL,thread_main, td);if (ret) {log_err("pthread_create: %s ",strerror(ret));nr_started--;break;}ret = pthread_detach(td->thread);if (ret)log_err("pthread_detach: %s",strerror(ret));} else {pid_t pid;dprint(FD_PROCESS, "will fork ");pid = fork();if (!pid) {int ret = fork_main(shm_id, i); _exit(ret);} else if (i == fio_debug_jobno)*fio_debug_jobp = pid;}
 
 
 这就相当于这么编程:#include <stdio.h>int main(){    int i=1;    int pid;    while((i--)>=0){        pid=fork();        if(pid==0){                i--;                printf("in the child process. ");        }        else                printf("in the parent process. ");    }}
  编译并执行上面那段程序的结果:root@localhost ~]# ./a.outin the parent process.in the child process.in the parent process.in the child process.
 一共创建了两个进程,只不过在Fio中的子进程的执行是由另外一个函数fork_main和thread_main来决定的。note:thread main这个函数做了许多事情,会再分析。回收子进程
当一个进程由于某种原因终止时,内核并不是立即把它从系统中清除。相反,进程被保持在一种已终止的状态中,直到被它的父进程reap。当父进程回收已经终止的子进程时,内核将子进程的退出状态(这是什么样的退出状态呢,留个心)传递给父进程,然后抛弃已终止的进程,从此时开始,该进程就不存在了。一个终止了但未被回收的进程称为僵尸zombie。如果父进程没有回收他的zombie就终止了,那么内核就会安排init进程来回收他们。长时间运行的程序,比如shell或者服务器,总是应该回收他们的zombie(总是在消耗系统的存储器资源)。一个进程可以通过调用waitpid函数来等待它的子进程终止或者停止。函数原型: #include<sys/types.h>
#include<sys/wait.h>
定义函数 pid_t waitpid(pid_t pid,int * status,int options);waitpid - 函数说明 waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程 结束。如果在调用 waitpid()时子进程已经结束,则 waitpid()会立即 返回子进程结束状态值。 子进程的结束状态值会由参数 status 返回, 而子进程的进程识别码也会一起返回。如果不在意结束状态值,则 参数 status 可以设成 NULL。参数 pid 为欲等待的子进程识别码, 其他数值意义如下: pid<-1 等待进程组识别码为 pid 绝对值的任何子进程。 pid=-1 等待任何子进程,相当于 wait()。 pid=0 等待进程组识别码与目前进程相同的任何子进程。 pid>0 等待任何子进程识别码为 pid 的子进程。 参数options提供了一些额外的选项来控制waitpid,参数 option 可以为 0 或可以用"|"运算符把它们连接起来使用,比如: ret=waitpid(-1,NULL,WNOHANG | WUNTRACED); 如果我们不想使用它们,也可以把options设为0,如: ret=waitpid(-1,NULL,0); WNOHANG 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若结束,则返回该子进程的ID。 WUNTRACED 若子进程进入暂停状态,则马上返回,但子进程的结束状态不予以理会。WIFSTOPPED(status)宏确定返回值是否对应与一个暂停子进程。 子进程的结束状态返回后存于 status,底下有几个宏可判别结束情况: WIFEXITED(status)如果若为正常结束子进程返回的状态,则为真;对于这种情况可执行WEXITSTATUS(status),取子进程传给exit或_eixt的低8位。 WEXITSTATUS(status)取得子进程 exit()返回的结束代码,一般会先用 WIFEXITED 来判断是否正常结束才能使用此宏。 WIFSIGNALED(status)若为异常结束子进程返回的状态,则为真;对于这种情况可执行WTERMSIG(status),取使子进程结束的信号编号。 WTERMSIG(status) 取得子进程因信号而中止的信号代码,一般会先用 WIFSIGNALED 来判断后才使用此宏。 WIFSTOPPED(status) 若为当前暂停子进程返回的状态,则为真;对于这种情况可执行WSTOPSIG(status),取使子进程暂停的信号编号。 WSTOPSIG(status) 取得引发子进程暂停的信号代码,一般会先用 WIFSTOPPED 来判断后才使用此宏。 如果执行成功则返回子进程识别码(PID) ,如果有错误发生则返回 返回值-1。失败原因存于 errno 中。fio使用waitpid的例子* Run over the job map and reap the threads that have exited, if any. */static void reap_threads(unsigned int *nr_running, unsigned int *t_rate, unsigned int *m_rate){...realthreads = pending = cputhreads = 0;for_each_td(td, i) {int flags = 0; /* * ->io_ops is NULL for a thread that has closed its * io engine */if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))cputhreads++;elserealthreads++;...flags = WNOHANG;if (td->runstate == TD_EXITED)flags = 0; /* * check if someone quit or got killed in an unusual way */ret = waitpid(td->pid, &status, flags);if (ret < 0) {if (errno == ECHILD) {log_err("fio: pid=%d disappeared %d ",(int) td->pid, td->runstate);td->sig = ECHILD;td_set_runstate(td, TD_REAPED);goto reaped;}perror("waitpid");} else if (ret == td->pid) {if (WIFSIGNALED(status)) {int sig = WTERMSIG(status); if (sig != SIGTERM && sig != SIGUSR2)log_err("fio: pid=%d, got signal=%d ",(int) td->pid, sig);td->sig = sig;td_set_runstate(td, TD_REAPED);goto reaped;}if (WIFEXITED(status)) {if (WEXITSTATUS(status) && !td->error)td->error = WEXITSTATUS(status); td_set_runstate(td, TD_REAPED);goto reaped;}} ...让进程休眠
sleep函数将一个进程挂起一段指定的时间。sleep函数返回0,否则返回剩下的要休眠的秒数(是可能的,因为可能被信号中断而过早的返回)。另外一个有用的函数是pause,该函数让调用函数休眠,直到该进程收到一个信号。进程间的信号传递
note:由于时间关系,有空再补充。参考文献
Caapp:深入理解计算机系统  http://www.linuxidc.com/Linux/2015-03/114720.htm源码fio-2.1.10fio 的详细介绍:请点这里
fio 的下载地址:请点这里相关阅读:黄金搭档之fio+blktrace--Linux下模拟块设备访问方式 http://www.linuxidc.com/Linux/2011-12/48468.htm本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-03/114722.htm