进程1-进程相关知识点

一 概念:

进程是运行中的程序,是操作系统分配资源的最基本的单位。

二 进程标识符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[]);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值