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

news2025/2/1 17:44:53

        本文主要探讨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;
}

结果显示:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1329291.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

react 2

1.快速搭建开发环境 2.react渲染流程 3.1 jsx基础 概念 3.2 jsx基础 本质 3.3 jsx基础 jsx表达式 3.4 jsx基础 实现列表渲染 3.5 jsx基础 实现条件渲染 3.5 jsx基础 实现复杂的条件渲染 4. react中事件绑定 5.react组建基础使用 6.1 useState 6.2 useState修改状态的规则 7.基础…

渗透测试和漏洞扫描有什么区别

渗透测试和漏洞扫描是网络安全领域中非常重要的两种技术手段&#xff0c;它们都可以帮助组织或企业发现和修复系统中的漏洞和弱点。然而&#xff0c;这两种技术手段在目的、深度、方法和时间和成本等方面存在显著的区别。 首先我们来了解下渗透测试和漏洞扫描分别是什么&#x…

测试开发体系介绍——测试体系介绍-L1

目录&#xff1a; 软件测试基础概念 软件测试:软件测试作用:软件缺陷:软件测试原则:软件测试对象:测试用例软件开发流程 软件:软件生命周期:软件开发流程:瀑布模型:瀑布模型优缺点敏捷开发模型: XP - 极限编程:SCRUM:DevOps&#xff1a;DevOps 生命周期&#xff1a;DevOps 对发…

C语言中关于操作符的理解

本篇文章只会列出大家在生活中经常使用的操作符 算术操作符 在算数操作符中常用的有&#xff0c;&#xff0c;-&#xff0c;*&#xff0c;/&#xff0c;% &#xff0c;我们重点讲一讲 / (除) 和 % (模) " / "运算 #include <stdio.h>int main() {int a5/2;fl…

【Amazon 实验③】使用Amazon WAF做基础 Web Service 防护之速率策略

文章目录 1. 速率策略1.1 介绍 2. 实验步骤2.1 添加规则2.2 测试2.3 结果 通过上一篇文章大家了解到如何使用Amazon WAF做关于自定义规则设置的 Web Service 防护【Amazon 实验②】使用Amazon WAF做基础 Web Service 防护之自定义规则&#xff0c;本篇文章将继续讲解一下关于速…

WebGL开发三维解剖学应用

开发基于 WebGL 的三维解剖学应用通常涉及以下步骤。这些步骤包括创建三维模型、整合交互性、优化性能等&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1.三维模型创建&#xff1a; 首先&#xff0…

SpringBoot 使用Quartz执行定时任务对象时无法注入Bean问题

文章目录 问题描述解决方案结束语 大家好&#xff01;今天是2023年12月21日 | 农历十一月初九(距离2024年还有一周左右的时间)&#xff0c;最近还是比较忙的&#xff0c;忙着搞钱&#xff0c;毕竟马上过年啦&#xff01; 问题描述 感谢大家对我一直以来的支持与帮助&#xff0c…

7.串口通信uart编写思路及自定义协议

前言&#xff1a; 串口是很重要的&#xff0c;有许多模块通信接口就是串口&#xff0c;例如gps模块&#xff0c;蓝牙模块&#xff0c;wifi模块还有一些精度比较高的陀螺仪模块等等&#xff0c;所以学会了串口之后&#xff0c;这些听起来很牛批的模块都能够用起来了。此外&#…

Qt/QML编程学习之心得:在QML工程中添加库(十四)

实现库并且使用库&#xff0c;类似于vc中的静态库library、动态库dll、COM组件等方法一样&#xff0c;在Qt中也经常会使用库&#xff0c;或者将部分功能打包成库。 右击Qt项目&#xff0c;点击add library... 在linux中将.a文件导入&#xff0c;工程会自动在.pro温江中增加相应…

centos安装Jenkins并拉取git远程仓库的代码进行自动化构建部署

安装Jenkins并拉取git远程仓库的代码进行自动化构建部署 1 前置条件2 先安装jdk113 安装git4 安装maven5 安装jenkins5.1下载jenkins5.2启动jenkins 6 使用jenkins拉取git仓库代码并部署6.1 安装插件6.2 在jenkins中配置maven6.3在jenkins上构建maven项目6.4 配置拉取的git仓库…

成功案例分享:物业管理小程序如何助力打造智慧社区

随着科技的进步和互联网的普及&#xff0c;数字化转型已经渗透到各个行业&#xff0c;包括物业管理。借助小程序这一轻量级应用&#xff0c;物业管理可以实现线上线下服务的无缝对接&#xff0c;提升服务质量&#xff0c;优化用户体验。本文将详细介绍如何通过乔拓云网设计小程…

【vtkWidgetRepresentation】第十六期 vtkContourRepresentation(三)

很高兴在雪易的CSDN遇见你 VTK技术爱好者 QQ:870202403 前言 本文分享vtkContourLineInterpolator接口的源码剖析和实例应用,希望对各位小伙伴有所帮助! 感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步! 你的点赞就是我的动力(^U^)ノ~YO 目录 前言 …

csrf自动化检测调研

https://github.com/pillarjs/understanding-csrf/blob/master/README_zh.md CSRF 攻击者在钓鱼站点&#xff0c;可以通过创建一个AJAX按钮或者表单来针对你的网站创建一个请求&#xff1a; <form action"https://my.site.com/me/something-destructive" metho…

【Java基础】 一个空的Object对象到底占多少内存

对象头包括&#xff08;Markword、类元指针、数组长度&#xff09; 压缩指针ON&#xff1a;占用12字节&#xff0c;Markword占8字节、类元指针占4字节.但是为了避免伪共享问题&#xff0c;JVM会按照8字节的倍数填充&#xff0c;所以会在对其区填充4字节&#xff0c;变成16字节。…

PhysX——源码编译

从git下载源码 git主页 https://github.com/NVIDIA-Omniverse/PhysXclone地址 https://github.com/NVIDIA-Omniverse/PhysX.git源码编译 运行PhysX需要两个编译器的支持&#xff0c;CMake 3.12 或以上版本以及Python 2.7.6 版本 进入工程的 physx 目录&#xff0c;运行generate…

系列一、GitHub搜索技巧

一、GitHub搜索技巧 1.1、概述 作为程序员&#xff0c;GitHub大家应该都再熟悉不过了&#xff0c;很多时候当我们需要使用某一项技能而又无从下手时&#xff0c;通常会在百度&#xff08;面向百度编程&#xff09;或者在GitHub上通过关键字寻找相关案例&#xff0c;比如我想学…

贪心法之活动安排问题

问题: 给定n个活动&#xff0c;每个活动有一个开始时间si和结束时间fi&#xff0c;选择出最大的互不相容的活动 集合。 思路&#xff1a; 将所有活动按照结束时间从小到大排序。 选择第一个活动&#xff0c;并将其加入最终的选择集合中。 依次考虑剩余的活动&#x…

基于Linphone android sdk开发Android软话机

1.Linphone简介 1.1 简介 LinPhone是一个遵循GPL协议的开源网络电话或者IP语音电话&#xff08;VOIP&#xff09;系统&#xff0c;其主要如下。使用linphone&#xff0c;开发者可以在互联网上随意的通信&#xff0c;包括语音、视频、即时文本消息。linphone使用SIP协议&#…

2024年你的年度目标OKR制定好了吗?

标题2023年余额见底&#xff0c;2024年的FLAG都制定好了吗&#xff1f; 目标很明确&#xff0c;计划很丰满&#xff0c;执行起来又处处透着一点点乏力&#xff0c;怎么办&#xff1f; 2024年可以尝试用OKR制定目标。 OKR目标管理方法&#xff0c;既适用于企业&#xff0c;也…

连续色调图像的二维编码

连续色调图像的二维编码&#xff08;也被称为连续色调图像的矢量量化&#xff09;是一种图像压缩和数据储存的方法&#xff0c;适用于连续渐变的色彩信息。该编码方法有助于减小图像文件的体积&#xff0c;并被广泛应用于数字图像处理、存储和传输领域。本文将介绍连续色调图像…