apue笔记-第8章 进程控制

swapper 进程(系统进程):ID = 0,只负责调度进程,并不执行磁盘上的任何程序

init 进程:ID = 1,负责读取与系统有关的初始化文件。init进程不会终止。它是一个普通的用户进程,以超级用户特权运行。

/sbin/init

init进程收养僵死进程-->只要有一个子进程终止,init就会调用一个wait函数取得其终止状态,防止了系统中塞满僵死进程。

进程正常或者异常终止--->内核就会向其父进程发送SIGCHLD信号,这种信号市内核向父进程发送的异步通知,父进程可以选择处理或者忽略。


exec 函数: 

#include "apue.h"
#include <sys/wait.h>

//char* env_init[] = {"USER=unknown","PATH=/tmp",NULL};

int main()
{
    pid_t pid;
    if((pid = fork())<0){
        printf("fork err!");

    }else if(pid == 0){
        if(execle("/bin/echo","echo","dd","MY arg2",(char*)0)<0)
            printf("execle err\n");

    }
 

    if(waitpid(pid,NULL,0)<0)
        printf("wait err!\n");
    
    if((pid = fork())<0){
        printf("fork error!\n");
    }
    else if(pid ==0){
        if(execlp("echo","echo","only 1 arg",(char*)0)<0)
            printf("execlp err\n");
    }
    
    exit(0);
}

输出:

kali@kali:~/Desktop/Linux Study/Hellos/Chapter8$ ./Exec_8_16 
dd MY arg2
kali@kali:~/Desktop/Linux Study/Hellos/Chapter8$ only 1 arg

通过nice()改变进程调度优先级

#include "apue.h"
#include <errno.h>
#include <sys/time.h>

#ifdef MACOS
#include <sys/syslimits.h>
#elif defined(BSD)
#include <sys/param.h>
#endif

unsigned long long count;
struct timeval end;

void checktime(char* str){
    struct timeval tv;
    gettimeofday(&tv,NULL);

    if(tv.tv_sec>= end.tv_sec && tv.tv_usec >= end.tv_usec){
        printf("%s count  = %lld\n",str,count);
        exit(0);
    }

}

int main(int argc,char* argv[]){
    pid_t pid;
    char *s;
    int nzero,ret;
    int adj = 0;

    setbuf(stdout,NULL);

    #ifdef NZERO
    nzero = NZERO;
    #elif defined _SC_NZERO
    nzero = sysconf(_SC_NZERO);
    #else
    #error NZERO undifined
    #endif
        printf("NZERO = %d\n",nzero);
        if(argc ==2)
            adj = strtol(argv[1],NULL,10);/* Convert a string to a long integer.  */
        gettimeofday(&end,NULL);
        end.tv_sec+=10;

        if((pid = fork())<0){
            printf("fork err\n");
        }else if(pid ==0){
            s = "child";
            printf("current nice value is %d,adjusting by %d\n",nice(0)+nzero,adj);
            errno = 0;
            if((ret = nice(adj))==-1 &&errno!=0)
                printf("child set scheduling priority");
            printf("now the child nice value is %d\n",ret+nzero);

        }
        else{
            s = "parent";
            printf("current parent process nice value is %d\n",nice(0)+nzero);
        }

        for(;;){
            if(++count == 0)
                printf("%s counter warp",s);
            checktime(s);
        }




}

输出:

kali@kali:~/Desktop/Linux Study/Hellos/Chapter8$ ./Nice_8_30 10
NZERO = 20
current parent process nice value is 20
current nice value is 20,adjusting by 10
now the child nice value is 30
child count  = 545334793
parent count  = 540767853

通过times()函数获取自己以及已经终止子进程的用户CPU时间和系统CPU时间

clock_t times(struct tms *buf); 


/* Structure describing CPU time used by a process and its children.  */
struct tms
  {
    clock_t tms_utime;		/* User CPU time.  */
    clock_t tms_stime;		/* System CPU time.  */

    clock_t tms_cutime;		/* User CPU time of dead children.  */
    clock_t tms_cstime;		/* System CPU time of dead children.  */
  };

#include "apue.h"
#include <sys/times.h>

static void pr_time(clock_t,struct  tms*,struct tms*);
static void do_cmd(char*);


int main(int argc,char* argv[]){

    int i;
    setbuf(stdout,NULL);
    for (int i = 1; i < argc; i++)
    {
        do_cmd(argv[i]);
    }
    exit(0);

}

static void do_cmd(char* cmd){
    struct tms tmsstart,tmsend;
    clock_t start,end;
    int status;

    printf("\n command:%s \n",cmd);
    if((start = times(&tmsstart)) == -1)
        printf("time err\n");
    if((status  = system(cmd))<0)
        printf("syetem err\n");

    if((end = times(&tmsend))==-1)
        printf("time err\n");

    pr_time(end-start,&tmsstart,&tmsend);
    pr_exit(status);

}

static void pr_time(clock_t real,struct tms*tmsstart,struct tms* tmsend){
    static long clktick  = 0;
    if(clktick ==0){
        if((clktick = sysconf(_SC_CLK_TCK))<0) /*fetch clock ticks per second first time */
            printf("sysconf err\n!");
    }
    printf(" real: %7.2f\n",real/(double)clktick);
    printf(" user: %7.2f\n",(tmsend->tms_utime -tmsstart->tms_utime)/(double)clktick);
    printf(" sys: %7.2f\n",(tmsend->tms_stime -tmsstart->tms_stime)/(double)clktick);
    printf(" child user: %7.2f\n",(tmsend->tms_cutime -tmsstart->tms_cutime)/(double)clktick);
    printf(" child sys: %7.2f\n",(tmsend->tms_cstime -tmsstart->tms_cstime)/(double)clktick);


}

 输出:

kali@kali:~/Desktop/Linux Study/Hellos/Chapter8$ ./Times_8_31 "sleep 5" "date" "man bash >/dev/null"

 command:sleep 5 
 real:    5.01
 user:    0.00
 sys:    0.00
 child user:    0.00
 child sys:    0.00
normal terminnation,exit status = 0

 command:date 
Wed 10 Jun 2020 10:50:38 PM EDT
 real:    0.00
 user:    0.00
 sys:    0.00
 child user:    0.00
 child sys:    0.00
normal terminnation,exit status = 0

 command:man bash >/dev/null 
 real:    0.26
 user:    0.00
 sys:    0.00
 child user:    0.14
 child sys:    0.20
normal terminnation,exit status = 0

猜你喜欢

转载自blog.csdn.net/qq_32095699/article/details/106548107