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.
例子
#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
### 注意点在创建子进程的时候 不能子线程再创建
正确的例子:
#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之后父进程
阅读全文
0 0
- Linux环境的函数
- Linux环境下系统函数的使用
- linux环境下的时间函数
- Linux环境下系统函数的使用
- linux环境下的main函数传递参数的问题
- linux上的php环境默认不支持bcdiv函数解决方法
- C函数调用机制(x86的linux环境下)
- 整理LINUX环境下常用的字符串相关C函数
- Linux C 环境下不带缓冲的I/O操作函数
- C函数调用机制(x86的linux环境下)
- C函数调用机制(x86的linux环境下)
- Linux环境下注册函数的调用顺序
- linux环境编程-- ftok()函数
- Linux 环境下 fork 函数和 exec 函数族的使用
- 我的Linux环境
- Linux的工作环境
- linux环境的建立
- linux的工作环境
- 机器学习--神经网络算法系列--BackPropagation算法
- 面试题43—n个色子的点数
- vue2+element 管理后台 集成解决方案 没有没做的,只要想不到的!
- WAVE文件格式说明
- 学习互联网架构第十课(并发类容器)
- Linux环境的函数
- VSCrawler 爬虫 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解决方案
- LeetCode-94. Binary Tree Inorder Traversal
- 基于fabric 0.6定制智能合约
- 面试题目44—扑克牌的顺子
- 解决RecyclerView代码复用问题
- 无领导小组讨论注意事项
- 面试题45—圆圈中最后剩下的数字
- 浅谈Web网站架构演变过程