进程间通讯(二).fifo(2)

2021-09-16 09:42:13 浏览数 (1)

编译执行

代码语言:javascript复制
emacs@ubuntu:~/c$ alias  gtc
alias gtc='gcc -Wall -g -o'
emacs@ubuntu:~/c$ gtc  fifoB.x fifoB.c
emacs@ubuntu:~/c$ gtc  fifoA.x fifoA.c
emacs@ubuntu:~/c$ 

先执行fifoB.x,因为等待输入,所以会在终端挂起

代码语言:javascript复制
emacs@ubuntu:~/c$ ./fifoB.x 

执行fifoA.x,会立即返回

代码语言:javascript复制
emacs@ubuntu:~/c$ ./fifoA.x 12345
432
emacs@ubuntu:~/c$ 

这时fifoB.x 也有返回了

代码语言:javascript复制
emacs@ubuntu:~/c$ ./fifoB.x 
12345
54321
emacs@ubuntu:~/c$

编译执行过程中没有报错,从结果来看,符合预期


access

access 函数的相关声明在 unistd.h

代码语言:javascript复制
/* Values for the second argument to access.
   These may be OR'd together.  */
#define R_OK    4               /* Test for read permission.  */
#define W_OK    2               /* Test for write permission.  */
#define X_OK    1               /* Test for execute permission.  */
#define F_OK    0               /* Test for existence.  */

/* Test for access to NAME using the real UID and real GID.  */
extern int access (__const char *__name, int __type) __THROW __nonnull ((1));

使用 F_OK 来检查是否存在


errno

这个变量在 errno.h

代码语言:javascript复制
#ifndef errno
extern int errno;
#endif

其值的宏在 asm-generic/errno-base.h 这个文件中

代码语言:javascript复制
emacs@ubuntu:/usr/include$ cat asm-generic/errno-base.h
#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H

#define	EPERM		 1	/* Operation not permitted */
#define	ENOENT		 2	/* No such file or directory */
#define	ESRCH		 3	/* No such process */
#define	EINTR		 4	/* Interrupted system call */
#define	EIO		 5	/* I/O error */
#define	ENXIO		 6	/* No such device or address */
#define	E2BIG		 7	/* Argument list too long */
#define	ENOEXEC		 8	/* Exec format error */
#define	EBADF		 9	/* Bad file number */
#define	ECHILD		10	/* No child processes */
#define	EAGAIN		11	/* Try again */
#define	ENOMEM		12	/* Out of memory */
#define	EACCES		13	/* Permission denied */
#define	EFAULT		14	/* Bad address */
#define	ENOTBLK		15	/* Block device required */
#define	EBUSY		16	/* Device or resource busy */
#define	EEXIST		17	/* File exists */
#define	EXDEV		18	/* Cross-device link */
#define	ENODEV		19	/* No such device */
#define	ENOTDIR		20	/* Not a directory */
#define	EISDIR		21	/* Is a directory */
#define	EINVAL		22	/* Invalid argument */
#define	ENFILE		23	/* File table overflow */
#define	EMFILE		24	/* Too many open files */
#define	ENOTTY		25	/* Not a typewriter */
#define	ETXTBSY		26	/* Text file busy */
#define	EFBIG		27	/* File too large */
#define	ENOSPC		28	/* No space left on device */
#define	ESPIPE		29	/* Illegal seek */
#define	EROFS		30	/* Read-only file system */
#define	EMLINK		31	/* Too many links */
#define	EPIPE		32	/* Broken pipe */
#define	EDOM		33	/* Math argument out of domain of func */
#define	ERANGE		34	/* Math result not representable */

#endif
emacs@ubuntu:/usr/include$

#define EEXIST 17 /* File exists */ 可以获知这个宏定义代表文件已经存在


总结

以下函数可以进行有名管道的创建

  • mkfifo

通过各方面资料弄懂其参数的意义和返回值的类型,是熟练掌握的基础

原文地址

0 人点赞