APUE第八章学习笔记

/**********************************************************
包含头文件:  #include <unistd.h>
函数原型:   pid_t   getpid(void);
 pid_t getppid(void);
 uid_t getuid(void);
 uid_t geteuid(void);
 gid_t getgid(void);
 gid_t getegid(void);
返回值: getpid: 返回调用进程ID
 getppid:返回调用进程父进程ID
 getuid: 返回调用进程实际用户ID
 geteuid:返回调用进程有效用户ID
 getgid: 返回调用进程实际组ID
 getegid: 返回调用进程有效组ID
***********************************************************/

vi 8.1.c

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("调用进程ID: %d\n",getpid());
    printf("调用进程父进程ID: %d\n",getppid());
    printf("调用进程实际用户ID: %d\n",getuid());
    printf("调用进程有效用户ID: %d\n",geteuid());
    printf("调用进程实际组ID: %d\n",getgid());
    printf("调用进程有效组ID: %d\n",getegid());

    exit(0);
}
/*****************************************************
包含头文件:  #include <unistd.h>
函数原型:   pid_t fork(void);
函数说明: 一个现有的进程可以调用fork函数创建一个进程
返回值: 子进程返回0,父进程返回进程ID,若出错,返回-1
******************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    int i = 0;
    int pid;

    printf("before fork\n");

    if ((pid = fork()) < 0)
    {
    printf("fork error\n");
    exit(0);
    }

    else if (pid == 0)
    {
    printf("forking\n");
    ++i;
    }
    else
    sleep(2);
    printf("i = %d\n",i);
    exit(0);
}

运行:

/*********************************************************
包含头文件:  #include <sys/wait.h>
函数原型:   pid_t wait(int *statloc);
参数说明: statloc是一个整型指针,如果statloc不是空指针,则终止进程的状态就存放在这里,若statloc是空指针,则不关心终止状态
函数说明: (1) 若所有子进程都未终止,则阻塞
(2) 若有任意一个子进程终止,则该函数得到子进程终止状态,立即返回
(3)若该进程无子进程,则出错返回
返回值: 若成功,返回进程ID,若出错,返回0
***********************************************************/

vi 8.3.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{

    pid_t p1;
    pid_t p2;
    pid_t p3;
    pid_t pw;
    int statloc;

    if ((p1 = fork()) < 0)
    {
    printf("1: fork error\n");
    exit(0);
    }
    else if (p1 == 0)
    {
    printf("子进程1 ID: %d\n",getpid());
    sleep(8);
    exit(0);
    }
    else
    {
    if ((p2 = fork()) < 0)
    {
        printf("2: fork error\n");
        exit(0);
    }
    else if (p2 == 0)
    {
        printf("子进程2 ID: %d\n",getpid());
        sleep(2);
        exit(0);
    }
    else
    {
        if ((p3 = fork()) < 0)
        {
        printf("3: fork eror\n");
        exit(0);
        }
        else if (p3 == 0)
        {
        printf("子进程3 ID: %d\n",getpid());
        sleep(5);
        exit(0);
        }
        if ((pw = wait(&statloc)) > 0)
        {
        printf("首先结束的进程为 %d\n",pw);
        }
        else
        printf("wait error\n");
    }
    }
    return 0;
}
/***********************************************************
包含头文件:  #include <sys/wait.h>
函数原型: pid_t waitpid(pid_t pid,int *statloc,int options);
函数说明:  waitpid并不等待调用之后的第一个终止子进程 ,它有若干个选项,可以控制它所等待的进程
返回值:若成功,返回进程ID,若出错,返回0或-1
**********************************************************/

vi 8.4.c

#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    pid_t ps1;
    pid_t ps2;

    if ((ps1 = fork()) < 0)
    {
    printf("1:fork error\n");
    exit(0);
    }
    else if (ps1 == 0)
    {
    printf("1: forking\n");
    sleep(7);
    exit(0);
    }

    if ((ps2 = fork()) < 0)
    {
    printf("2: forking error\n");
    exit(0); 
    }
    else if (ps2 == 0)
    {
    printf("2: forking\n");
    sleep(4);
    exit(0);
    }

    if (waitpid(ps1,NULL,0) != ps1)
    printf("waitpid 1 error\n");
    else
    printf("forking 1 has exited\n");

    if (waitpid(ps2,0) != ps2)
    printf("waitpid 2 has error\n");
    else
    printf("forking 2 has exited\n");

    return 0;
}

vi 8.5.c

/*********************************************************
包含头文件:  #include    <sys/wait.h>
函数原型:   int waitid(idtype_t idtype,id_t id,siginfo_t *infop,int options);
参数说明:   idtype_t:
常量                          说明
P_PID                       等待某一特定进程id
P_PGID                  等待某进程组中的任一id
P_ALL                       等待任一id 
options是下列的常量按位或运算:
WCONTINUED(等待一进程,他以前曾被停止,此后又继续,但其状态尚未报告)
WEXITED(等待已退出的进程)
WNOHANG(如无可用的子进程退出状态,立即返回而非阻塞)
WNowAIT(不破坏子进程退出状态,该子进程退出状态,可由后续函数取得)
WSTOPPED(等待一进程,它已经停止,但状态尚未报告)
返回值: 若成功,返回0,若失败,返回-1
**********************************************************/

vi 8.5.c

#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    pid_t ps1;
    pid_t ps2;

    if ((ps1 = fork()) < 0)
    {
    printf("1 fork error\n");
    exit(0);
    }
    else if (ps1 == 0)
    {
    printf("1: forking\n");
    sleep(2);
    exit(0);
    }

    if ((ps2 = fork()) < 0)
    {
    printf("2 fork error\n");
    exit(0);
    }
    else if (ps2 == 0)
    {
    printf("2: forking\n");
    sleep(8);
    exit(0);
    }
    if (waitid(P_PID,ps1,WEXITED) == 0)
    {
    printf("1 fork has exited\n");
    }

    if (waitid(P_PID,ps2,WEXITED) == 0)
    {
    printf("2 fork has exited\n");
    }
    return 0;
}
/***********************************************************
包含头文件:  #include <sys/wait.h>
 #include <sys/type.h>
 #include <sys/time.h>
 #include <sys/resource.h>
函数原型:   pid_t wait3(int *statloc,int options,struct 
rusage* rusage);
pid_t wait4(pid_t pid,struct 
rusage *rusage);
函数说明:   资源统计信息包括用户cpu时间总量,系统cpu时间总量,缺页次数,接受到信号的次数等,返回由终止进程及其所有子进程使用的资源概况
返回值:    若成功,返回进程ID,若出错,返回
-1
**********************************************************/

vi 8.6.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

static void charatatime(char *);

int main()
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
    printf("fork error\n");
    exit(0);
    }
    else if (pid == 0)
    {
    charatatime("output from child\n");
    }
    else
    {
    charatatime("ouput from parent\n");
    }
    return 0;
}

static void charatatime(char *str)
{
    char *ptr;
    int c;
    setbuf(stdout,NULL);
    for (ptr = str; (c = *ptr++) != 0;)
    putc(c,stdout);
}
/********************************************************** 包含头文件: #include <unistd.h> 函数原型: int execl(const char* pathname,const char* arg0,…/*(char*)0*/);
int execv(const char* pathname,char *const argv[]);
int execle(const char* pathname,const char* arg0,…/*(char*)0,char char *const envp[] */);
int execve(const char* pathname,char *const argv[],char *const envp[]);
int execlp(const char* filename,…
/*(char*)0*/);
int execvp(const char* pathname,char *const envp[]);
int fexecve(int fd,char *const envp[]);
函数说明: 多用也就会了
返回值:若出错,返回-1,若成功,不返回
************************************************************/

vi echoarg.c

#include <stdio.h>
#include <stdlib.h>

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

    for (int i = 0; i < argc; ++i)
    {
    printf("argv[%d]: %s\n",i,argv[i]);
    }
    return 0;
}

vi 8.7.c

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
    printf("fork error\n");
    }
    else if (pid == 0)
    {
    execlp("/home/marco/echoarg.out","marco","is good","man",(char*)0);
    exit(0);
    }
    exit(0);
}

vi echoget.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char* argv[])
{
    int i;
    char **ptr;
    extern char **environ;

    for (int i = 0; i < argc; ++i)
    printf("argv[%d]: %s\n",argv[i]);

    for (ptr = environ; *ptr != 0; ++ptr)
    printf("environmet: %s\n",*ptr);

    return 0;
}

vi 8.8.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

char *env_init[] = {"name = marco","PATH = /home/marco/8.8.c",NULL};

int main()
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
    printf("fork error\n");
    exit(0);
    }
    else if (pid == 0)
    {
    execle("/home/marco/echoget.out","is a","king",env_init);
    exit(0);
    }

    if (waitpid(pid,0) != pid)
    printf("waitpid error\n");
    return 0;
}
/******************************************************
包含头文件:  #include <unistd.h>
函数原型:   int setuid(uid_t uid);
 int setgid(gid_t gid);
函数说明:   设置实际用户ID和有效用户ID
 设置实际用户组ID和有效组ID
返回值:若成功,返回0,若失败,返回-1
********************************************************/
/*******************************************************
包含头文件:  #include <unistd.h>
函数原型:   int setreuid(uid_t ruid,uid_t euid);
 int setregid(gid_t rgid,gid_t egid);
函数说明:交换实际用户ID和有效用户ID的值
返回值:    若成功,返回0,若出错,返回-1
******************************************************/
/**********************************************************
包含头文件:  #include <unistd.h>
函数原型:   int seteuid(uid_t uid);
 int setegid(gid_t gid);
函数说明:设置有效用户ID或有效用户组ID
返回值:若成功,返回0,若出错,返回-1
********************************************************/
/***********************************************************
包含头文件:  #include <stdlib.h>
函数原型:   int system(const char* cmdstring);
函数说明:执行cmdstring程序
返回值:(1):fork失败或者waitpid返回除EINTR之外的出错,则system返回-1,并且设置errno以指示错误类型
(2):如果exec失败(表示不能执行shell),则其返回值如同shell执行了exit一样
(3):否则所有3个函数(fork,exec和waitpid)都成功,那么system的返回值是shell的终止状态,其格式已在
waitpid中说明
***********************************************************/

vi 8.9.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

char *env_init[] = {"name = marco",0) != pid)
    printf("waitpid error\n");
    return 0;
}
/********************************************************
包含头文件:  #include <unistd.h>
函数原型:   char *getlogin(void);
函数说明:获取用户登录的名字
返回值:若成功,返回指向登录名字字符串的指针,返回NULL
注:如果调用函数的进程没有连接到用户登录时所用的终端,则函数会失败,通常称这些进程为守护进程
*********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    char *ptr;

    if ((ptr = getlogin()) == NULL)
    {
    printf("getlogin error\n");
    exit(0);
    }
    else
    printf("运行该程序的用户登录名: %s\n",ptr);
    return 0;
}
/****************************************************
包含头文件:  #include <unistd.h>
函数原型:   int nice(int incr);
函数说明:获取或更改进程的nice值,提高或降低进程调度优先级(nice越小,优先级越高,nice的范围在
(2 * NZERO) – 1之间,NZERO是系统认的nice值
返回值:若成功,返回新的nice值NZERO;若出错,返回-1
注:-1是合法的成功返回值,如果nice调用成功,返回值为-1,则errno为0,否则,nice调用失败
如果inr太小(太大),系统会自动升高(降低)到最小(最大)合法值
*********************************************************/


/*********************************************************
包含头文件:  #include <sys/resource.h>
函数原型:   int getpriority(int which,id_t who);
参数说明: which参数可以取以下三个值:
PRIO_PROCESS    表示进程
PRIO_PGRP   表示进程组
PRIO_USER   表示用户ID
当which设为PRIO_USER并且who为0,使用调用进程的实际用户ID
当which作用于多个进程,则返回所有作用进程中优先级最高的(最小的nice值)
函数说明: 获取进程或一组相关进程的nice值
返回值:若成功,返回-NZERO ~ NZERO – 1之间的nice值,返回-1
*******************************************************/

/********************************************************
包含头文件:  #include <sys/resource.h>
函数原型:   int setpriority(int which,id_t who,int value);
函数说明:为进程,进程组和属于特定用户ID的所有进程设置优先级
返回值:若成功,返回-1
*******************************************************/

vi 8.10.1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main()
{

    int ret;

    if ((ret = nice(sysconf(_SC_NZERO) - 2)) == -1)
    {
    if (errno != 0)
    {
        printf("nice error\n");
        exit(0);
    }
    }
    else
    printf("%d\n",ret);

    return 0;
}

vi 8.10.2.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>

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);
    nzero = sysconf(_SC_NZERO);

    printf("NZERO = %d\n",nzero);

    if (argc == 2)
    adj = strtol(argv[1],10);

    gettimeofday(&end,NULL);
    end.tv_sec += 10;

    if ((pid = fork()) < 0)
    {
    printf("fork error\n");
    exit(0);
    }
    else if (pid == 0)
    {
    s = "child";
    printf("current nice value is %d,adjustfily by %d\n",nice(0) + nzero,adj);

    errno = 0;

    if ((ret = nice(adj)) == -1 && errno != 0)
    {
    printf("nice error\n");
    exit(0);
    }
    printf("Now child nice value is %d\n",ret + nzero);
    }
    else
    {
    s = "parent";
    printf("current nice value in parent is %d\n",nice(0) + nzero);
    }
    for (;;)
    {
    if (++count == 0)
    {
        printf("%s counter wrap\n",s);
        exit(0);
    }
    checktime(s);
    }
    return 0;
}

vi 8.11.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <unistd.h>
#include <errno.h>

int main()
{
    int ret;

    int n;
    errno = 0;

   /* if ((n = nice(sysconf(_SC_NZERO))) == -1 && errno == -1) { printf("nice error\n"); exit(0); } printf("%d\n",n);*/

    if (setpriority(PRIO_PROCESS,getpid(),5) < 0)
    {
    printf("setpriority error\n");
    exit(0);
    }

    if ((ret = getpriority(PRIO_PROCESS,getpid())) == -1)
    {
    printf("getpriority error\n");
    exit(0);
    }

    printf("%d\n",ret);

    return 0;
}
/*******************************************************
包含头文件:  #include <sys/times.h>
函数原型:   clock_t times(struct tms *buf);
参数说明:   struct tms
{
 clock_t tms_utime; //用户cpu时间
 clock_t tms_stime; //系统cpu时间
 clock_t tms_cutime; //用户cpu时间 (子进程终止)
 clock_t tms_cstime; //系统cpu时间(子进程终止)
}
函数说明:获得进程时间
返回值:    若成功,返回流逝的墙上时钟时间(以时钟滴答数为单位);若出错,返回-1
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/times.h>


long clktck;

void print_time(clock_t pass,struct tms start,struct tms end)
{

    printf("流逝 %7.2f 秒\n",pass /(double) clktck);
    printf("用户cpu时间 %7.2f秒\n",(end.tms_utime - start.tms_utime) /(double) clktck);
    printf("系统cpu时间 %7.2f秒\n",(end.tms_stime - start.tms_stime) /(double) clktck);
    printf("子进程用户cpu时间 %7.2f秒\n",(end.tms_cutime - start.tms_cutime) /(double) clktck);
    printf("子进程系统cpu时间 %7.2f秒\n",(end.tms_cstime - start.tms_cstime) /(double) clktck);
}

int main ()
{
    if ((clktck = sysconf(_SC_CLK_TCK)) < 0)
    {
    printf("sysconf error\n");
    exit(0);
    }

    struct tms start;
    clock_t s;
    if ((s = times(&start)) < 0)
    {
    printf("times error\n");
    exit(0);
    }
    pid_t pid;

    if ((pid = fork()) < 0)
    {
    printf("fork error\n");
    exit(0);
    }
    else if (pid == 0)
    {
    sleep(6);

    printf("in child\n");
    clock_t child;
    struct tms end;
    if ((child = times(&end)) < 0)
    {
        printf("times error\n");
        exit(0);
    }

    print_time(child - s,start,end);

    exit(0);
    }
    else
    {
    sleep(2);

    printf("in parent\n");
    struct tms end;
    clock_t parent;

    if ((parent = times(&end)) < 0)
    {
        printf("times error\n");
        exit(0);
    }

    print_time(parent - s,end);
    }
    return 0;
}

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...