Linux环境的函数

来源:互联网 发布:中美进出口数据 编辑:程序博客网 时间:2024/06/01 13:24

一,查看进程的环境变量

正常的命令是:echo $PATH

在Linux的 extarn 全局变量 extarn char **entern

编码

#include <stdio.h>                                        //定义一个全局变量extern char **environ;int  main(void){    int i;    for (i = 0; environ[i]; i++)    {        printf("%d,%s\n",i, environ[i]);    }     return 0; }

二, getenv函数和setenv函数和unsetenv函数

man文档

NAME       getenv, secure_getenv - get an environment variableSYNOPSIS       #include <stdlib.h>       char *getenv(const char *name);       char *secure_getenv(const char *name);   Feature  Test  Macro  Requirements  for  glibc  (see fea‐   ture_test_macros(7)):       secure_getenv(): _GNU_SOURCE
NAME       setenv - change or add an environment variableSYNOPSIS       #include <stdlib.h>       int setenv(const char *name, const char *value, int overwrite);  //重写   overwrite 不能为0  为0函数无意思       int unsetenv(const char *name);   Feature  Test  Macro  Requirements  for  glibc  (see fea‐   ture_test_macros(7)):       setenv(), unsetenv():           _BSD_SOURCE || _POSIX_C_SOURCE >= 200112L ||           _XOPEN_SOURCE >= 600

例子:

#include <stdio.h>                                        #include <stdlib.h>#include <string.h>int main(void){    char *val;    const char *name = "ABD";    //获取当前的环境变量的ABD      val = getenv(name);    printf("1,%s = %s\n", name, val);   //设置当前环境变量 ABD 为    setenv(name, "haha-dav-and_night", 1);    val = getenv(name);   printf("2, %s = %s\n", name, val); #if 0  //ret 为0 走这步     int ret = unsetenv("ABDFGH");   //删除这个ABDFGH的环境变量    printf("ret = %d\n", ret );    val = getenv(name);     printf("3, %s = %s\n", name, ret); #else //ret 不为0 走这步     int ret = unsetenv("ABD"); //name=vlaue:value    printf("ret = %d\n", ret);    val = getenv(name);    printf("3,  %s = %s\n", name, val);#endif    return 0;  }          

运行的结果

3,进程控制 fork函数

man文档

NAME       fork - create a child processSYNOPSIS       #include <unistd.h>       pid_t fork(void);  //返回值有两个  1,返回 子进程的pid  2,返回 0               pid_t   是结构体struct中的DESCRIPTION       fork() creates a new process by duplicating the call‐       ing process.  The new process is referred to  as  the       child process.  The calling process is referred to as       the parent process.

进程控制fork

例子

 #include <stdio.h>                                         #include <unistd.h> #include <sys/types.h>  #include <stdlib.h> int main(void) {    pid_t pid; //这是结构体中的pid    printf("xxxxxx\n");    pid = fork();    if (pid == -1)  //判断是否报错     {         perror("fork error:");        exit(1);    }     else if ( pid == 0)  //判断是否是子进程       {        printf("I',m child , pid = %u\n, ppid = %u\n", get    pid(), getppid()); // 获取进程的pid       }     else    {        printf("I'm parent ,pid = %u, ppid = %u\n", getpid    (), getppid());   sleep(1000);   //子进程休息一下    }     printf("YYYYYYYYYYYYY\n");    return 0; }

4,循环创建子进程pid

### 注意点在创建子进程的时候 不能子线程再创建 

fork错误分析创建子进程图

正确的例子:

#include <stdio.h>                                         #include <stdlib.h> #include <unistd.h> #include <sys/types.h> int main(void){     int i;    pid_t pid;    for ( i = 0; i < 5; i++)    {          pid = fork(); //创建子进程         if ( pid == -1 )          {           perror("fork error:");            exit(1);         }         else if ( pid == 0) // 是子进程就跳出循环          {            break;         }          else         {             printf("parent pid = %u, ppid = %u\n", getpid(    ), getppid());           }    }   //打印子进程的pid和ppid    if ( i < 5 )   {       printf("I'm child =%d, pid = %u, ppid = %u\n",i, g    etpid(), getppid());    }    return 0;}    

效果图

顺序
Shell进程 父
父子进程:读时共享,写时复制

父子进程共享:1,文件描述符(打开文件的结构体)
2,mmap建立的映射区(进程的中)
fork之后父进程