📄 00000005.htm
字号:
F_SETFL 设定 O_NDELAY。比较旧的系统(Version 7, 4.1 BSD) 没有 <BR> O_NDELAY,那就得用 alarm(2) 来设定 read 的 timeout,以达成近似 <BR> nonblocking read 的功能。 <BR> <BR>------------------------------ <BR> <BR>Subject: How do I find the name of an open file? <BR>Date: Thu Mar 18 17:16:55 EST 1993 <BR> <BR>4.3) 要怎样才能得知一个已 open 档案之档名? <BR> <BR> 这个是非常困难的。若是这个 file descriptor 是对应到 pipe 或 pty 就没 <BR> 有名字了。这个 file descriptor 对应的档案也有可能已被删除。若是有 <BR> symbolic link 或 hard link,则可能有许多个名字。 <BR> <BR> 如果你经过一再考虑後别无选择一定要这麽做的话,可以用 find 的 -inum 与 <BR> -xdev 选项,或用 ncheck,或用自己写类似的程式来做。在这麽做时要耐心的 <BR> 等,因为在一个几百 megabyte 甚至几 gigabyte 的 file system中找一个档 <BR> 案,一定得花不少时间。 <BR> <BR>------------------------------ <BR> <BR>Subject: How can an executing program determine its own pathname? <BR>Date: Thu Mar 18 17:16:55 EST 1993 <BR> <BR>4.4) 一个执行中的程式如何知道自己的 pathname? <BR> <BR> 若果 argv[0] 是以 "/" 开始的字,它可能就是你的程式所在地的绝对路径。 <BR> 如果不是那就得照顺序检查 PATH 里的每一个目录看看里面是否有与 argv[0] <BR> 一样的程式。如果找得到的话将那个目录与程式名称兜起来可能就是你要的 <BR> pathname 了。 <BR> <BR> 不过上述方法找到的并不一定是正确的,因为在程式中用到 exec() 时, <BR> argv[0] 是可以随便给的。将 argv[0] 设为与要执行的程式名称相同只是一 <BR> 种惯用法罢了! <BR> <BR> 以下的例子可能会使你更清楚些: <BR> <BR> #include <stdio.h> <BR> main() <BR> { <BR> execl("/usr/games/rogue", "vi Thesis", (char *)NULL); <BR> } <BR> <BR> 这个被执行的程式就会认为它的名字(argv[0] 之值)是 "vi Thesis")。 <BR> <BR>------------------------------ <BR> <BR>Subject: How do I use popen() to open a process for reading AND writing? <BR>Date: Thu Mar 18 17:16:55 EST 1993 <BR> <BR>4.5) 如何用 popen() 对一个 process 做读写的动作? <BR> <BR> 用 pipe 把一个 process 的输出、输入转给任意的 process 所可能会发生的 <BR> 问题就是 deadlock,譬如这两个 processes 刚好同时都在等待「尚未产生」 <BR> 的输入时。唯一能避免 deadlock 的方法就是在 pipe 的两端都要遵循严格的 <BR> deadlock-free 协定,但是需要这些 processes 之间的互相合作才能达成, <BR> 而对於像 popen() 这类的函数来说并不太适合。 <BR> <BR> 在 'expect' 这个软体中附有一个能够让 C 程式直接引用的函式库。其中有 <BR> 一个函式不管是在读或写都能达到和 popen 相同的功能。但是这个函式是使 <BR> 用 ptys 而不是 pipes,也没有 deadlock 的问题,并且在 BSD 或 SV 中都 <BR> 能使用。若想对 'expect' 有进一步的了解,可参考下一个问题的解答。 <BR> <BR>------------------------------ <BR> <BR>Subject: How do I sleep() in a C program for less than one second? <BR>Date: Thu Mar 18 17:16:55 EST 1993 <BR> <BR>4.6) 在 C 程式中要怎麽用 sleep() 才能够 sleep 小於一秒? <BR> <BR> 首先要注意的是,你只能指定 delay 的「最短」时间;实际上会 delay 多久和 <BR> 系统的 scheduling 方式有关,例如系统当时的负载。如果你倒楣的话,它还可 <BR> 能会 delay 蛮长的时间。 <BR> <BR> 并没有一个标准函式能够在「小睡」(很短的 sleep)期间提供你计数的功能。 <BR> 某些系统有提供 usleep(n) 的函式,它能够暂停执行 n 微秒(microsecond) <BR> 的时间。如果你所使用的系统没有提供 usleep() 函式,那麽以下有可在 BSD, <BR> System V 使用中的作法。 <BR> <BR> 接下来的这段程式码是 Doug Gwyn 在 System V 中模拟 4BSD 并利用 4BSD <BR> 中的 select() 系统呼叫。Doung 自己都叫它为 'nap()' ;你也可以把它叫做 <BR> "usleep()"; <BR> <BR> /* <BR> usleep -- support routine for 4.2BSD system call emulations <BR> last edit: 29-Oct-1984 D A Gwyn <BR> */ <BR> <BR> extern int select(); <BR> <BR> int <BR> usleep( usec ) /* returns 0 if ok, else -1 */ <BR> long usec; /* delay in microseconds */ <BR> { <BR> static struct /* `timeval' */ <BR> { <BR> long tv_sec; /* seconds */ <BR> long tv_usec; /* microsecs */ <BR> } delay; /* _select() timeout */ <BR> <BR> delay.tv_sec = usec / 1000000L; <BR> delay.tv_usec = usec % 1000000L; <BR> <BR> return select( 0, (long *)0, (long *)0, (long *)0, &delay ); <BR> } <BR> <BR> On System V you might do it this way: <BR> <BR> /* <BR> subseconds sleeps for System V - or anything that has poll() <BR> Don Libes, 4/1/1991 <BR> <BR> The BSD analog to this function is defined in terms of <BR> microseconds while poll() is defined in terms of milliseconds. <BR> For compatibility, this function provides accuracy "over the long <BR> run" by truncating actual requests to milliseconds and <BR> accumulating microseconds across calls with the idea that you are <BR> probably calling it in a tight loop, and that over the long run, <BR> the error will even out. <BR> <BR> If you aren't calling it in a tight loop, then you almost <BR> certainly aren't making microsecond-resolution requests anyway, <BR> in which case you don't care about microseconds. And if you did, <BR> you wouldn't be using UNIX anyway because random system <BR> indigestion (i.e., scheduling) can make mincemeat out of any <BR> timing code. <BR> <BR> Returns 0 if successful timeout, -1 if unsuccessful. <BR> <BR> */ <BR> <BR> #include <poll.h> <BR> <BR> int <BR> usleep(usec) <BR> unsigned int usec; /* microseconds */ <BR> { <BR> static subtotal = 0; /* microseconds */ <BR> int msec; /* milliseconds */ <BR> <BR> /* 'foo' is only here because some versions of 5.3 have <BR> * a bug where the first argument to poll() is checked <BR> * for a valid memory address even if the second argument is 0. <BR> */ <BR> struct pollfd foo; <BR> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -