read()
含义:Read from a PSL file or process channel(读取PSL文件或进程管道)
Format
read(channel,[size])
Parameters
Parameter | Definition |
channel | process I/O channel number from which the read() function reads data |
size | optional integer value controlling the amount of data that the read() function reads from channel Valid Values Default |
Description
The read() function returns the data it reads one byte at a time from channel. The read() function returns the value EOF (that is, the NULL string) on an end-of-file or error condition.Channels are created by calling the fopen() or popen() function.
To enforce serialization for shared channels, no two reader processes (that is, read() or readln() functions) can be blocked on the same channel. The second reader process that attempts to block on the shared channel will fail, returning the NULL string and setting the PSL variable errno to E_PSL_BUSY_CHANNEL. Another possible shared channel failure can be caused by a close() function being executed against a channel that also has a blocked reader process. The close() function will cause the reader process to return the NULL string and set errno to E_PSL_UNBLOCKED_BY_CLOSE.
Example
Code:
fchan=fopen("/root/1.txt","r");
if (chan_exists(fchan)) {data=read(fchan,-1);close(fchan);
}
print(data);
Output:
编辑
Code1:
pchan=popen("OS","ls /root");
if (chan_exists(pchan)) {
data=read(pchan);
close(pchan);
}
print(data);
Output1: