一 概念:
进程是运行中的程序,是操作系统分配资源的最基本的单位。
二 进程标识符pid:
类型pid_t
命令ps
进程标识符是顺序依次向下使用
getpid();
getppid();
三 进程的产生:fork()
fork意味着拷贝,一模一样的命令 cow。未决信号与文件锁不继承,返回值不相同。资源利用量归0
init进程:1号进程是所有进程的祖先进程
四 进程的消亡与释放资源
wait();
waitpid();
执行谁分配谁释放原则
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <sys/wait.h>
void test1()
{
pid_t pid1;
pid1 = fork();
if(pid1 == 0) {
usleep(100);
printf("%d: I am child\n", getpid());
}
if(pid1 > 0) {
printf("%d: I am parent\n", getpid());
}
}
void test2()
{
pid_t pid1;
pid1 = fork();
if(pid1 == 0) {
usleep(100);
printf("%d: I am child\n", getpid());
}
if(pid1 > 0) {
printf("%d: I am parent\n", getpid());
}
wait(&pid1);
}
void test3()
{
execl("/tmp/out", "echo 2 >");
}
int main(int argc, char **argv)
{
int i = atol(argv[1]);
switch (i) {
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test3();
break;
default:
break;
}
exit(0);
}
五 exec函数族
int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
int execle(const char *path, const char *arg0, ... /*, (char *)0, char *const envp[] */);
int execlp(const char *file, const char *arg0, ... /*, (char *)0 */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
execvP(const char *file, const char *search_path, char *const argv[]);