linux系统和网络(二):进程和系统时间

        本文主要探讨linux系统进程和系统相关知识,本博客其他博文对该文章的部分内容有详细介绍

        main函数

int main(int argc,char *argv[],char *envp[]);


        操作系统下main执行前先执行引导代码,编译连接引导代码和程序连接在一起构成可执行程序,加载器将程序加载到内存中执行

        程序终止(return、exit、_exit、atexit)

int atexit(void (*function)(void));


        atexit注册进程终止处理函数,可注册多个进程终止处理函数,先执行注册函数后终止
        return、exit、_exit:return和exit会执行进程终止处理函数,_exit不执行

        环境变量

extern char **environ;
char *getenv(const char *name);


        进程有环境变量构成环境变量表(字符串数组),通过environ全局变量使用环境变量,获取指定环境变量函数getenv

        进程(详见本博客其他文章)
                进程是程序运行一次的过程,进程控制块PCB是内核管理进程的数据结构
                进程ID可用getpid、getppid、getuid、geteuid、getgid、getegid函数获取
                进程独立运行在虚拟地址空,逻辑地址空间均为4GB(32位),0-1G为OS,1-4G为应用程序
                虚拟地址到物理地址空间的映实现进程隔离,多进程同时运行
                运行进程宏观上并行,微观上串行
                操作系统最小调度单元是线程
                进程状态:就绪态,运行态,阻塞态

        fork与vfork(详见本博客其他文章)
                子进程和父进程有独立PCB(复制父进程)
                fork父子进程执行次序不定,vfork子进程先运行,子进程调用exec/exit后,父进程执行,子进程未调用exec/exit,程序出错
                fork子进程拷贝父进程地址空间,vfork子进程共享父进程地址空间

        进程资源(详见本博客其他文章)
                进程运行需消耗系统资源(内存、IO),进程消亡未释放资源则资源丢失,进程退出,操作系统会回收资源
                操作系统回收进程工作消耗的内存和IO,父进程回收进程本身占用的内存(8KB,task_struct和栈)
                僵尸进程是子进程先于父进程结束,父进程为回收资源造成,从而造成内存泄漏
                父进程使wait/waitpid显示回收子进程资源并获取子进程退出状态
                孤儿进程是父进程先于子进程结束,子进程由init进程接收


        wait/waitpid(详见本博客其他文章)
                父进程调用wait函数阻塞,子进程结束,系统向其父进程发送SIGCHILD信号,父进程被SIGCHILD信号唤醒后去回收子进程

pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
WIFEXITED(status)宏判断子进程是否正常终止(return、exit、_exit)
WIFSIGNALED(status)宏判断子进程是否非正常终止(被信号终止)
WEXITSTATUS(status)宏得到正常终止进程返回值


        waitpid回收指定PID子进程,有阻塞或非阻塞

ret = waitpid(-1, &status, 0); == ret =wait(&status);     
-1任意子进程,0阻塞式(默认),ret子进程PID
ret = waitpid(pid, &status, WNOHANG);
非阻塞式回收子进程,子进程已结束回收成功,返回的子PID,子进程未结束则父进程返回0

        exec族函数(详见本博客其他文章)

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,.., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);


        path:程序路径,file:程序名arg:程序名,argv[]:程序名称+参数,envp新环境变量代替调用进程的环境变量,其余为命令参数,且以NULL结尾

        守护进程(详见本博客其他文章)
                进程组由多个进程构成,会话由进程组构成
                特点:生存周期长,随操作系统启动和关闭,可脱离控制台运行
                syslogd负责日志文件写入和维护
                syslog记录系统调试信息,/var/log/messages中存储(ubuntu中在/var/log/syslog)守护进程
                proc目录下文件大小是0,文件不在硬盘中,不是真实文件是接口,是映射内核内部数据结构被格式化成字符的结果
                sys是虚拟文件,proc文件只读,/sys文件可读写

        进程间通信(详见本博客其他文章)
                方式:无名管道和有名管道、信号量、消息队列、共享内存、Socket套接字、信号
                无名管道:内核维护内存,有读端和写端(单向通信的),父子进程间通信
                有名管道:内核维护内存,表现为有名字文件,半双工,任意2进程通信
                消息队列:类似内核维护的FIFO,全双工
                信号量:用于解决进程同步访问数据
                共享内存:进程间通过大块内存共享数据
                信号:系统向进程发送信号(指令kill)
                socket:通过TCP/IP等协议共享数据

        系统时间
                定时器(timer)用于段时间计时,实时时钟(RTC)用于点时间计时
                jiffies是linux内核中的全局变量,记录内核节拍数(1节拍为1-10ms,可设置)
                linux系统记录时间:内核开机启动读取RTC硬件获取初始基准时间,存储该基准时间对应jiffies值,系统运行每节拍,jiffies加1,根据jiffies加UTC(1970-01-01 00:00:00 +0000)可获得时间点,RTC在关机后记录节拍,用于开机后的点时间准确

        时间函数

time_t time(time_t *t);


        返回当前时间距UTC秒数(使用jiffies)

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);


        将time返回的秒数转成字符串时间

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };

        gmtime/localtime将time秒数转成struct tm结构时间,gmtime是国际时间,localtime是本地时间(系统时区时间)

time_t mktime(struct tm *tm);


        mktime将struct tm转成time_t

char *asctime(const struct tm *timeptr);
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);


        asctime/strftime将struct tm转成字符串时间,strftime可指定格式

int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };

struct timezone {
               int tz_minuteswest;     /* minutes west of Greenwich */
               int tz_dsttime;         /* type of DST correction */
           };

gettimeofday


        返回时间由struct timeval和struct timezone结构体表示,timeval表示时间,timezone表示时区
        settimeofday设置当前时间和时区

        time产生随机数

int rand(void);
int rand_r(unsigned int *seedp);
void srand(unsigned int seed);


        srand设置不同种子,调用rand获取随机序列,常用time函数返回值做种子

demo1:

        环境变量(main,environ)

#include <stdio.h>

void printf_error()
{
        printf("uasge: ./a.out xxx \n");
}

int main(char argc,char *argv[],char *envp[])
{
        int i = 0;
        int j = 0;
        extern char **environ;

        if(argc != 2)
        {
                atexit(printf_error);
                return 0;
        }
        printf("argv:%s\n",argv[1]);


        while(envp[i] != NULL)
        {
                printf("%s\n",envp[i]);
                i++;
        }

        while (environ[j] != NULL)
        {
                printf("%s\n", environ[j]);
                j++;
        }
        return 0;
}

结果显示:

demo2: 

        fork前为父进程空间,子进程继承父进程的资源(fork前),pid=0为子进程空间,pid>0为父进程空间,其余为公共空间,子进程继承父进程fd(覆盖写),父子有独立的pcb

#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
        pid_t pid;
        int fd;
        int num = 0;

        printf("befor fork pid : %d\n",getpid());
        pid = fork();
        fd = open("test",O_RDWR);

        if(pid == 0)
        {
                num++;
                write(fd,"cd",2);
                printf("son : %d num : %d\n",getpid(),num);
        }
        else if(pid > 0)
        {
                num++;
                printf("father : %d num: %d\n",getpid(),num);
                write(fd,"ab",2);
        }
        else
        {
                perror("fork");
        }
        close(fd);

        printf("the last pid : %d num : %d\n",getpid(),num);

        return 0;
}

结果显示:

demo3: 

        wait/waitpid:非阻塞式回收子进程,子进程已结束回收成功,返回的子PID,子进程未结束则父进程返回0

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

int main(void)
{
        pid_t pid;
        pid_t ret;
        int status;

        pid = fork();
        if (pid > 0)
        {
                printf("father pid : %d.\n",getpid());
                //ret = wait(&status);
                //ret = waitpid(-1, &status, 0);
                //ret = waitpid(pid, &status, 0);
                ret = waitpid(pid, &status, WNOHANG);           // 非阻塞式

                printf("son pid : %d.\n", ret);
                printf("son normal exit status :%d\n", WIFEXITED(status));
                printf("son abnormal exit status :%d\n", WIFSIGNALED(status));
                printf("son ret : %d.\n", WEXITSTATUS(status));
        }
        else if (pid == 0)
        {
                return 27;
        }
        else
        {
                perror("fork");
                return -1;
        }

        return 0;
}

结果显示:

 demo4:

        execl族函数

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

int main()
{
        pid_t pid;

        pid = fork();
        if (pid > 0)
        {
                printf("father pid :%d.\n", getpid());
        }
        else if (pid == 0)
        {
                //execl("/bin/ls", "ls", "-l", NULL);

                //char * const arg[] = {"ls", "-l", "-a", NULL};
                //execv("/bin/ls", arg);

                //execlp("ls", "ls", "-l", NULL);

                char * const envp[] = {"num = 27", NULL};
                execle("/bin/ls","ls","-l", NULL, envp);

                return 0;
        }
        else
        {
                perror("fork");
                return -1;
        }

        return 0;
}

结果显示: 

demo5: 

        守护进程

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void create_daemon(void)
{
        pid_t pid;
        int i = 0;
        int num = sysconf(_SC_OPEN_MAX);

        pid = fork();
        if (pid < 0)
        {
                perror("fork");
                exit(-1);
        }
        if (pid > 0)
        {
                exit(0);
        }

        pid = setsid();
        if (pid < 0)
        {
                perror("setsid");
                exit(-1);
        }

        chdir("/");
        umask(0);

        for (i=0; i<num; i++)
        {
                close(i);
        }

        open("/dev/null", O_RDWR);
        open("/dev/null", O_RDWR);
        open("/dev/null", O_RDWR);

}

void test_process()
{
        int fd;
        fd = open("lock", O_RDWR | O_TRUNC | O_CREAT | O_EXCL, 0664);
        if(fd < 0)
        {
                printf("./a.out is running\n");
                exit(-1);
        }

}

int main(void)
{
        test_process();
        create_daemon();
        while (1)
        {
                printf("hello word\n");
                sleep(1);
        }

        return 0;
}

结果显示: 

         syslog

#include <syslog.h>
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);

  

demo6:

        系统时间函数

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>

int main()
{
        time_t ts;
        time_t mk;
        struct tm dt;
        int ret = -1;
        char buf[100];
        struct timeval tv = {0};
        struct timezone tz = {0};

        time(&ts);
        if (ts < 0)
        {
                perror("time");
                return -1;
        }
        printf("time : %ld\n",ts);

        // ctime
        printf("ctime: %s\n", ctime(&ts));

        // gmtime/localtime
        memset(&dt, 0, sizeof(dt));
        gmtime_r(&ts, &dt);
        printf("gmtime: %d-%d-%d %d:%d:%d\n", dt.tm_year+1900, dt.tm_mon+1, dt.tm_mday,dt.tm_hour,dt.tm_min,dt.tm_sec);

        memset(&dt, 0, sizeof(dt));
        localtime_r(&ts, &dt);
        printf("loacaltime : %d-%d-%d %d:%d:%d\n", dt.tm_year+1900, dt.tm_mon+1, dt.tm_mday,dt.tm_hour,dt.tm_min,dt.tm_sec);

        //mktime
        mk = mktime(&dt);
        printf("mktime : %ld\n",mk);

        // asctime
        printf("asctime:%s\n", asctime(&dt));

        // strftime
        memset(buf, 0, sizeof(buf));
        strftime(buf, sizeof(buf), "%Y-%m-%d %H-%M-%S", &dt);
        printf("strftime : %s\n", buf);

        // gettimeofday
        ret = gettimeofday(&tv, &tz);
        if (ret < 0)
        {
                perror("gettimeofday");
                return -1;
        }
        printf("sec: %ld\n", tv.tv_sec);
        printf("timezone:%d\n", tz.tz_minuteswest);

        return 0;
}

结果显示: 

        time做随机数种子 

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

int main()
{
        int i = 0;
        while(i < 5)
        {
                srand(time(NULL));
                printf("%d ",rand());
                i++;
        }
        printf("\n");
        return 0;
}

结果显示:

 

  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值