黑馬《linux系統程式設計》學習筆記(從36到40)
阿新 • • 發佈:2018-12-29
三十六. 驗證管道緩衝區大小
以下命令,可以驗證緩衝區的大小
這裡先是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的建立
[[email protected]_0_15_centos ~]# ls 3Day 5Day app include main.c src vim 4Day 6Day HelloRoot lib Makefile TestGdb //通過mkfifo 使用者名稱,這樣的方式,建立了一個名為abc的管道,並且我們注意到這個管道的大小會始終為0 [[email protected]_0_15_centos ~]# mkfifo abc [
[email protected]_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的位置