linux 通用IO

open(),read(),write(),close()可以应用于管道,FIFO,socket,或者终端等所有文件类型执行IO操作。

lseek()并不适用于所有类型的文件。不允许将lseek()应用于管道,FIFO,socket或者终端,只要合情合理,也可以将lseek应用于设备。例如在磁盘或磁带上查找一处具体位置。

cp test test.old

cp test /dev/pts/0

cp /dev/pts/0 test

cp /dev/pts/0 /dev/pts/1

[root@250-shiyan pts]# echo "wo shi " > /dev/pts/0
[root@250-shiyan ~]# cp /dev/pts/1 b.txt
fjei
sfoei
rifeo
wfowef
[root@250-shiyan ~]# cat b.txt
fjei
sfoei
rifeo
wfowef

当你在终端或控制台上工作时,你可能想记录下自己做了些什么。这种记录可以看成是保存了终端痕迹的文档。假设你跟一些Linux管理员同时在系统上干活。或者说你让别人远程到你的服务器。你就会想记录下终端发生过什么。要实现它,你可以使用script命令。

记录他人活动,-q静默,-a追加

vi /etc/profile

/usr/bin/script -qa /usr/local/script/log_record_script

/usr/include/netinet/in.h 

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;                 /* Port number.  */
    struct in_addr sin_addr;            /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
                           __SOCKADDR_COMMON_SIZE -
                           sizeof (in_port_t) -
                           sizeof (struct in_addr)];
  };

/* Ditto, for IPv6.  */
struct sockaddr_in6
  {
    __SOCKADDR_COMMON (sin6_);
    in_port_t sin6_port;        /* Transport layer port # */
    uint32_t sin6_flowinfo;     /* IPv6 flow information */
    struct in6_addr sin6_addr;  /* IPv6 address */
    uint32_t sin6_scope_id;     /* IPv6 scope-id */
  };

/usr/include/sys/socket.h

/* Create a new socket of type TYPE in domain DOMAIN, using
   protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
   Returns a file descriptor for the new socket, or -1 for errors.  */
extern int socket (int __domain, int __type, int __protocol) __THROW;


socketpair()系统调用只能用在unix domain中,
/* Create two new sockets, of type TYPE in domain DOMAIN and using
   protocol PROTOCOL, which are connected to each other, and put file
   descriptors for them in FDS[0] and FDS[1].  If PROTOCOL is zero,
   one will be chosen automatically.  Returns 0 on success, -1 for errors.  */
extern int socketpair (int __domain, int __type, int __protocol,
                       int __fds[2]) __THROW;

/* Allocate SIZE bytes of memory.  */
extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;
头文件:#include <stdlib.h>
定义函数:void *malloc(size_t size);
函数说明:malloc()用来配置内存空间,其大小由指定的size 决定。
返回值:若配置成功则返回一指针,失败则返回NULL。
范例
void p = malloc(1024);  //配置1k 的内存

getenv()函数能够从进程环境中检索单个值
头文件:#include <stdlib.h>
定义函数:char * getenv(const char *name);
函数说明:getenv()用来取得参数name 环境变量的内容. 参数name 为环境变量的名称, 如果该变量存在则会返回指向该内容的指针. 环境变量的格式为name=value.
返回值:执行成功则返回指向该内容的指针, 找不到符合的环境变量名称则返回NULL.
范例
[root@250-shiyan c]# cat env.c
#include <stdlib.h>
#include <stdio.h>
main()
{
   char *p;
   if((p = getenv("USER")))
   printf("USER = %s ", p);
}
[root@250-shiyan c]# gcc -o en env.c
[root@250-shiyan c]# ./en
USER = root

getpid()函数返回调用进程的pid

头文件:#include <unistd.h>
定义函数:pid_t getpid(void);
函数说明:getpid ()用来取得目前进程的进程识别码,许多程序利用取到的此值来建立临时文件, 以避免临时文件相同带来的问题。
返回值:目前进程的进程识别码
范例
#include <unistd.h>
main()
{
    printf("pid=%d ", getpid());
}
执行:
pid=1494 /*每次执行结果都不一定相同 */

原文地址:https://www.cnblogs.com/createyuan/p/4151297.html