黑马《linux系统编程》学习笔记(从36到40)

三十六. 验证管道缓冲区大小

以下命令,可以验证缓冲区的大小

这里先是fpathconf的文档

然后是fpathconf.c的程序

#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[])
{
    int fd[2];
    int ret = pipe(fd);
    if(ret == -1)
    {
        perror("pipe error");
        exit(1);
    }
	
	//测试管道缓冲区的大小
	//下面这里,传fd[0]或者fd[1]其实本质没有区别
    long size = fpathconf(fd[0], _PC_PIPE_BUF);
    printf("size = %ld\n", size);
    return 0;
}

 三十七. 设置管道的非阻塞属性

三十八. fifo的创建

 

[root@VM_0_15_centos ~]# ls
3Day  5Day  app        include  main.c    src      vim
4Day  6Day  HelloRoot  lib      Makefile  TestGdb

//通过mkfifo 用户名,这样的方式,建立了一个名为abc的管道,并且我们注意到这个管道的大小会始终为0
[root@VM_0_15_centos ~]# mkfifo abc
[root@VM_0_15_centos ~]# ll
total 60
drwxr-xr-x 5 root root 4096 Dec 15 07:26 3Day
drwxr-xr-x 7 root root 4096 Dec 20 18:34 4Day
drwxr-xr-x 4 root root 4096 Dec 26 03:11 5Day
drwxr-xr-x 3 root root 4096 Dec 27 02:26 6Day
prw-r--r-- 1 root root    0 Dec 28 01:22 abc
-rwxr-xr-x 1 root root 8552 Dec 14 03:17 app
drwxr-xr-x 3 root root 4096 Sep  1 20:06 HelloRoot
drwxr-xr-x 2 root root 4096 Dec 13 09:05 include
drwxr-xr-x 2 root root 4096 Dec 14 05:39 lib
-rw-r--r-- 1 root root  117 Dec 13 09:35 main.c
drwxr-xr-x 2 root root 4096 Dec 15 06:45 Makefile
drwxr-xr-x 2 root root 4096 Dec 14 03:05 src
drwxr-xr-x 2 root root 4096 Dec 15 07:02 TestGdb
drwxr-xr-x 3 root root 4096 Sep  4 06:18 vim

 三十九. fifo进行没有血缘关系的进程间通信

 write_fifo.c

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

int main(int argc, const char* argv[])
{
    if(argc < 2)
    {
        printf("./a.out fifoname\n");
        exit(1);
    }

    // 判断文件是否存在
    int ret = access(argv[1], F_OK);
    if(ret == -1)
    {
        int r = mkfifo(argv[1], 0664);
        if(r == -1)
        {
            perror("mkfifo error");
            exit(1);
        }
        printf("有名管道%s创建成功\n", argv[1]);
    }

    int fd = open(argv[1], O_WRONLY);
    if(fd == -1)
    {
        perror("open error");
        exit(1);
    }

    char *p = "hello, world";
    while(1)
    {
        sleep(1);
        int len = write(fd, p, strlen(p)+1);
    }

    close(fd);

    return 0;
}

 read_fifo.c

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

int main(int argc, const char* argv[])
{
    if(argc < 2)
    {
        printf("./a.out fifoname\n");
        exit(1);
    }

    // 判断文件是否存在
    int ret = access(argv[1], F_OK);
    if(ret == -1)
    {
        int r = mkfifo(argv[1], 0664);
        if(r == -1)
        {
            perror("mkfifo error");
            exit(1);
        }
        printf("有名管道%s创建成功\n", argv[1]);
    }

    int fd = open(argv[1], O_RDONLY);
    if(fd == -1)
    {
        perror("open error");
        exit(1);
    }

    char buf[512];
    while(1)
    {
        int len = read(fd, buf, sizeof(buf));
        buf[len] = 0;
        printf("buf = %s\n, len = %d", buf, len);
    }

    close(fd);

    return 0;
}

 四十. mmap函数参数讲解

 下图中,绿色区域的首地址,就是mmap中的adrr的位置

 

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85271857