Linux高并发服务器开发---笔记2(多进程)

0630

第4章 项目制作与技能提升

4.0 视频课链接

4.1 项目介绍与环境搭建

4.2 Linux系统编程1、4.3 Linux系统编程2

4.4 多进程

1-9

Linux高并发服务器开发—笔记1

10.进程间通信☆☆☆

视频课01:30:30开始)

进程间通信的概念(IPC)

进程是一个独立的资源分配单元,不同进程(这里所说的进程通常指的是用户进程)之间的资源是独立的,没有关联,不能在一个进程中直接访问另一个进程的资源

但是,进程不是孤立的,不同的进程需要进行信息的交互和状态的传递等,因此需要进程间通信( IPC:Inter Processes Communication )。

进程间通信的目的:

  • 数据传输:一个进程需要将它的数据发送给另一个进程。
  • 通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件(如进程终止时要通知父进程)。
  • 资源共享:多个进程之间共享同样的资源。为了做到这一点,需要内核提供互斥和同步机制。
  • 进程控制:有些进程希望完全控制另一个进程的执行(如 Debug 进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。

Linux 进程间通信的方式(七种)

管道(匿名管道、有名管道)
信号量(互斥锁)
共享内存
内存映射(匿名映射、内存映射)
消息队列
信号
socket
在这里插入图片描述

①匿名管道(管道)— 亲缘关系的进程

管道也叫无名(匿名)管道,它是 UNIX 系统 IPC(进程间通信)的最古老形式,所有的 UNIX 系统都支持这种通信机制。
统计一个目录中文件的数目命令:ls | wc –l,为了执行该命令,shell 创建了两个进程来分别执行 ls 和 wc。

管道其实是一个在内核内存中维护的缓冲器,这个缓冲器的存储能力是有限的,不同的操作系统大小不一定相同。
管道拥有文件的特质:读操作、写操作匿名管道没有文件实体,有名管道有文件实体,但不存储数据。可以按照操作文件的方式对管道进行操作。

一个管道是一个字节流,使用管道时不存在消息或者消息边界的概念,从管道读取数据的进程可以读取任意大小的数据块,而不管写入进程写入管道的数据块的大小是多少。

通过管道传递的数据是顺序的,从管道中读取出来的字节的顺序和它们被写入管道的顺序是完全一样的。

在管道中的数据的传递方向是单向的,一端用于写入,一端用于读取,管道是半双工的

从管道读数据是一次性操作,数据一旦被读走,它就从管道中被抛弃,释放空间以便写更多的数据,在管道中无法使用 lseek() 来随机的访问数据。

匿名管道只能在具有公共祖先(父进程与子进程,或者两个兄弟进程,具有亲缘关系的进程之间使用

创建匿名管道:

#include <unistd.h>
int pipe(int pipefd[2]);

功能:创建一个匿名管道,用来进程间通信。
参数:int pipefd[2] 这个数组是一个传出参数。

  • pipefd[0] 对应的是管道的读端
  • pipefd[1] 对应的是管道的写端

返回值: 成功 0;失败 -1

管道默认是阻塞的:如果管道中没有数据,read阻塞;如果管道满了,write阻塞

注意:匿名管道只能用于具有关系的进程之间的通信(父子进程,兄弟进程)

示例:

// 子进程发送数据给父进程,父进程读取到数据输出
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
   

    // 在fork之前创建管道
    int pipefd[2];
    int ret = pipe(pipefd);
    if(ret == -1) {
   
        perror("pipe");
        exit(0);
    }

    // 创建子进程
    pid_t pid = fork();
    if(pid > 0) {
   
        // 父进程
        printf("i am parent process, pid : %d\n", getpid());

        // 关闭写端
        close(pipefd[1]);
        
        // 从管道的读取端读取数据
        char buf[1024] = {
   0};
        while(1) {
   
            int len = read(pipefd[0], buf, sizeof(buf));
            printf("parent recv : %s, pid : %d\n", buf, getpid());
            
            // 向管道中写入数据
            // const char * str = "hello,i am parent";
            // write(pipefd[1], str, strlen(str));
            // sleep(3);
        }

    } else if(pid == 0){
   
        // 子进程
        printf("i am child process, pid : %d\n", getpid());
        // 关闭读端
        close(pipefd[0]);
        char buf[1024] = {
   0};
        while(1) {
   
            // 向管道中写入数据
            const char *str = "hello,i am child";
            write(pipefd[1], str, strlen(str));
            sleep(3);

            // int len = read(pipefd[0], buf, sizeof(buf));
            // printf("child recv : %s, pid : %d\n", buf, getpid());
            // bzero(buf, 1024);
        }
        
    }
    return 0;
}
查看管道缓冲大小命令:
ulimit –a
root@VM-16-2-ubuntu:~# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15343
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15343
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

其中pipe size (512 bytes, -p) 8表示管道大小为512B* 8 = 4KB。

查看管道缓冲大小函数:fpathconf()函数
#include <unistd.h>
long fpathconf(int fd, int name);
//gets a value for the configuration option name for the open file descriptor fd.

示例:

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

int main() {
   

    int pipefd[2];
    int ret = pipe(pipefd);
    if(ret == -1) {
   
        perror("pipe");
        exit(0);
    }
    // 获取管道的大小
    long size = fpathconf(pipefd[0], _PC_PIPE_BUF);
    printf("pipe size : %ld\n", size);

    return 0;
}

结果:

pipe size : 4096

4096字节 = 4KB

示例:
// 子进程发送数据给父进程,父进程读取到数据输出
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
   

    // 在fork之前创建管道
    int pipefd[2];
    int ret = pipe(pipefd);
    if(ret == -1) {
   
        perror("pipe");
        exit(0);
    }

    // 创建子进程
    pid_t pid = fork();
    if(pid > 0) {
   
        // 父进程
        printf("i am parent process, pid : %d\n", getpid());

        // 关闭写端
        close(pipefd[1]);
        
        // 从管道的读取端读取数据:
        char buf[1024] = {
   0};
        while(1) {
   
            int len = read(pipefd[0], buf, sizeof(buf));
            printf("parent recv : %s, pid : %d\n", buf, getpid());
            
            // 向管道中写入数据:
            // const char * str = "hello,i am parent";
            // write(pipefd[1], str, strlen(str));
            // sleep(3);
        }

    } else if(pid == 0){
   
        // 子进程
        printf("i am child process, pid : %d\n", getpid());
        // 关闭读端
        close(pipefd[0]);
        char buf[1024] = {
   0};
        while(1) {
   
            // 向管道中写入数据:
            const char *str = "hello,i am child";
            write(pipefd[1], str, strlen(str));
            sleep(3);

			// 从管道的读取端读取数据:
            // int len = read(pipefd[0], buf, sizeof(buf));
            // printf("child recv : %s, pid : %d\n", buf, getpid());
            // bzero(buf, 1024);
        }
    }
    return 0;
}

编译运行:

i am child process, pid : 2799345
i am parent process, pid : 2799340
parent recv : hello,i am child, pid : 2799340
parent recv : hello,i am child, pid : 2799340
parent recv : hello,i am child, pid : 2799340
parent recv : hello,i am child, pid : 2799340

①有名管道(命名管道,FIFO)

  1. 匿名管道,由于没有名字,只能用于亲缘关系的进程间通信。为了克服这个缺点,提出了有名管道(FIFO),也叫命名管道、FIFO文件。
  2. 有名管道(FIFO)不同于匿名管道之处在于它提供了一个路径名与之关联,以 FIFO 的文件形式存在于文件系统中,并且其打开方式与打开一个普通文件是一样的,这样即使与 FIFO 的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过 FIFO 相互通信,因此,通过 FIFO, 不相关的进程也能交换数据
  3. 一旦打开了 FIFO,就能在它上面使用与操作匿名管道和其他文件的系统调用一样的I/O系统调用了(如read()、write()和close())。与管道一样,FIFO 也有一个写入端读取端,并且从管道中读取数据的顺序与写入的顺序是一样的。FIFO 的名称也由此而来:先入先出
  4. 有名管道(FIFO)和匿名管道(pipe)有一些特点是相同的,不一样的地方在于:
    FIFO 在文件系统中作为一个特殊文件存在,但 FIFO 中的内容却存放在内存中
    当使用 FIFO 的进程退出后,FIFO 文件将继续保存在文件系统中以便以后使用;
    FIFO 有名字,不相关的进程可以通过打开有名管道进行通信

通过命令创建有名管道:

mkfifo 名字

通过函数创建有名管道:

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

参数:

  • pathname: 管道名称的路径
  • mode: 文件的权限, 和 open 的参数 mode 是一样的,是一个八进制的数

返回值:成功返回0,失败返回-1,并设置错误号

一旦使用 mkfifo 创建了一个 FIFO,就可以使用 open 打开它,常见的文件I/O 函数都可用于 fifo。如:close、read、write、unlink 等。
FIFO 严格遵循先进先出(First in First out),对管道及 FIFO 的读总是从开始处返回数据,对它们的写则把数据添加到末尾。它们不支持诸如 lseek() 等文件定位操作

示例:

mkfifo.c

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

int main() {
   

    // 判断文件是否存在
    int ret = access("fifo1", F_OK);
    if(ret == -1) {
   
        printf("管道不存在,创建管道\n");        
        ret = mkfifo("fifo1", 0664);//0664是权限
        if(ret == -1) {
   
            perror("mkfifo");
            exit(0);
        }       
    }

    return 0;
}

read.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值