唠唠叨(2)

唠唠叨(2)

目录

1435096-20181125155241449-1934713983.png

1.程序和进程

程序:二进制文件,占用磁盘空间
进程:启动的程序

2.并行与并发

并行:时间点概念
并发:时间段概念

1435096-20181125155034385-126283215.png

3.进程5种状态

1435096-20181125155043717-391183425.png

4.循环创建多个子进程

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int counter = 100;

int main(int argc, const char* argv[])
{
    pid_t pid;

    int i=0;
    for(i=0; i<3; i++)
    {
        pid = fork();
        if(pid == 0)
        {
            break;
        }
    }

    // 父进程
    if(i == 3)
    {
        counter += 100;
        printf("parent process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
        //        sleep(1);
    }
    // 子进程
    else if(i == 0)
    {
        // 1th
        counter += 200;
        printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
    }
    else if(i == 1)
    {
        // 2th
        counter += 300;
        printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
    }
    else if(i == 2)
    {
        // 3th
        counter += 400;
        printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
    }

    return 0;
}

5.ps、kill

```
ps aux | grep myhello
ps ajx

kill:向指定进程发送信号
kill -l   查看信号
kill -9         杀死进程   
```

6.exec函数族

让父子进程执行不相干操作(替换进程地址空间中代码段)

执行一个另外的程序不需要创建额外的地址空间

execl()

1435096-20181125155057712-2138255300.png

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>


int main(int argc, const char* argv[])
{
    printf("hello, world\n");

    pid_t pid = fork();
    if(pid == -1)
    {
        perror("fork error");
        exit(1);
    }

    // 子进程执行程序
    if(pid == 0)
    {
        execl("hello", "xxxx",  NULL);
        //execl("/home/kevin/hello", "xxxx",  NULL);
        perror("execl");
        exit(1);
    }

    for(int i=0; i<3; ++i)
    {
        printf(" i = %d\n", i);
    }

    return 0;
} 

execlp()

1435096-20181125155108090-1602681501.png

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>


int main(int argc, const char* argv[])
{
    printf("hello, world\n");

    pid_t pid = fork();
    if(pid == -1)
    {
        perror("fork error");
        exit(1);
    }

    // 子进程执行程序
    if(pid == 0)
    {
        execlp("ps", "pssdfsdf", "aux", NULL);
        perror("execlp");
        exit(1);
    }

    for(int i=0; i<3; ++i)
    {
        printf(" i = %d\n", i);
    }

    return 0;
}

7.wait、waitpid

进程结束之后,能够释放用户空间,释放不了内核空间,需有父进程回收

1435096-20181125155136644-882530600.png

1435096-20181125155146782-273194663.png

posted @ 2018-11-25 15:53 神秘的火柴人 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《 Big bang 》是著名的美剧。在剧中 Sheldon 可以说是一个极品,真不知 Leonard 是如何忍受这位极品室友成天的唠叨。 你知道么? Sheldon 有一个神秘的小本本,记录了所有他从小开始讨厌的人名。 Stuart 这位漫画店老板就是小本本的一员哦,谁叫他常常毫不客气地挤兑 Sheldon ,曾多次赌赢过 Sheldon 呢。 Penny 是一个漂亮的女孩,好奇心也很强。为了满足她的好奇心,我当回编剧让她意外知道了 Sheldon 的那个小本本放在了哪里。于是她几乎每天都去看,看看上面有哪些人。但是那个小本本上的人名实在太多。要知道她可是没上过大学在饭店里面当服务员啊。请聪明的你帮帮她处理处理那个小本本吧。Sheldon 每天都会在小本本里记录些人名,当然有时也会与他们和好就会从小本本中将这个人名删除。我们假设 Sheldon 会在一个空的小本本上插入、删除、查询某个人。 要帮助 Penny ,你需要知道一个链表是怎么初始化、插入、删除以及查找的。 输入格式: 输入数据只有一组,有很多行。每行的格式可能是下列一种: insert a name delete name show search name 其中 a 是一个整数,代表在第 a 个名字前插入名字。 name 是一个姓名,只包含英文字母的大小写,每个名字不超过30个字符。 输入保证不会插入列表中已经存在的姓名,不会删除列表中不存在的姓名,不会搜索列表中不存在的姓名,也不会要求在非法的位置插入列表。
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值