锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Normally, opening a FIFO for read or write, it blocks until another process opens it for write or read. Write and read蹇呴』涓涓瀵瑰簲銆?br /> A read gets as much data as it requests or as much data as the FIFO has, whichever is less. A write to a FIFO is atomic, as long as the write does not exceed the capacity of the FIFO. The capacity is at least 4k. How to set flags. writefd = open (FIFO1, O_WRONLY|O_ONOBLOCK,0); 浣嗘槸pipe娌℃湁open鍑芥暟 鎵浠ュ彧鑳借繖鏍瘋瀹?/span> flags= fcntl (fd, F_GETFL,0); flag|=O_NONBLOCK; fcntl =(fd,F_SETFL,flags); Operation Existing opens of pipe or FIFO Blocking (default) O_NONBLOCK set Open FIFO for reading FIFO open for writing Returns OK Returns OK FIFO not open for writing Blocks until FIFO is opened for writing Returns OK Open FIFO for writing FIFO open for reading Returns OK Returns OK FIFO not open for reading Blocks until FIFO is opened for reading Returns an error of ENXIO Read empty pipe or FIFO Pipe or FIFO open for writing Blocked until there is data or the pipe or FIFO is closed for writing Return an error of EAGAIN Pipe or FIFO not open for writing Read returns 0 (EOF) Read return 0 (EOF) Write to pipe or FIFO Pipe or FIFO open for reading Return ok Return ok Pipe or FIFO is full Blocked until space is available, then write data Returns an error of EAGAIN Pipe or FIFO not open for reading SIGPIPE generated, write process terminated Returns an error of EPIPE
聽聽聽 鐒跺悗灝遍『鐞嗘垚绔犲湴鐐瑰惂錛岃繕鎸洪『鍒╋紝瀹冨氨寮濮嬫洿鏂頒簡銆備絾鏇存柊鐫灝卞湪閭d笉鍔ㄤ簡錛屽鎱㈡棤姣斻?br />
聽聽聽 鍦ㄨ佸﹩鐨勬寚瀵間笅錛屾垜涓婄綉鏌ヤ簡涓涓媦um鐨勭敤娉曘?br />
聽聽聽 鎵撳紑緇堢銆?br />聽聽聽 棣栧厛浣跨敤yum list鏉ユ煡鐪嬫洿鏂板垪琛紝鍒風殑涓涓嬪嚭鏉ュソ澶氥?br />聽聽聽 鐒跺悗浣跨敤yum update錛屽畠浼氭鏌ユ瘡涓鏇存柊鐨勮蔣浠跺寘鐨勫ご錛屽緱鍒頒竴浜涗俊鎭紝鍖呮嫭杞歡鍖呯殑澶у皬絳夌瓑銆?br />
聽聽聽 浼氳綆楀嚭鎬葷殑瑕佷笅杞界殑澶у皬錛岄棶浣犳槸鍚﹁涓嬭澆銆?br />聽聽聽 涔栦箹錛屼竴鍏?90m錛?鎴戦夋嫨浜唝錛屽紑濮嬩笅杞解︹?br />
聽聽聽 婕暱鐨勭瓑寰呫?br />聽聽聽 鐒跺悗浼氭彁紺轟綘錛屼綘鍐嶉夋嫨y紜畾涓涓嬪氨濂姐?br />聽聽聽 寮勫畬鍚庡崐姝伙紝閲嶅惎涓涓嬪悗涓鍒囨甯搞?br />
聽聽聽 浣嗛噸鍚椂鍙戠幇錛屽惎鍔ㄦ椂閫夋嫨緋葷粺鐨勬椂鍊檒inux鏈変簡涓や釜閫夐」錛屽彧鏈夌増鏈俊鎭殑寰皬宸紓銆傚掓槸涓嶅獎鍝嶄綘閫夋嫨榪涘叆linux
聽聽聽 灝辨槸鐪嬪緱涓嶆槸寰堢埥銆備簬鏄笂緗戞悳绱簡涓涓嬶紝鐭ラ亾榪欎釜淇℃伅鐢?鏂囦歡緋葷粺/boot/grub/grub.conf鏉ュ喅瀹氥?br />聽聽聽 浜庢槸鐮旂┒浜嗕竴涓嬪畠錛屽垹鎺変簡閭d釜鐗堟湰姣旇緝鏃х殑閫夐」錛屽啀鎶婇粯璁ゅ惎鍔ㄧ郴緇熸敼涓篸efault = 1 錛堝洜涓虹幇鍦ㄦ槸涓や釜浠?寮濮嬬紪鍙鳳紝windows鏄?錛屼竴寮濮嬩笁涓殑鏃跺欙紝windows鏄?錛?br />
聽聽聽聽 鑷蟲錛屾洿鏂板交搴曞畬鎴愶紒
]]>
FIFO鍜孭IPE鍩烘湰宸笉澶氾紝浣咶IFO鏄懡鍚嶇殑錛屼竴浜涙病鏈変翰緙樺叧緋葷殑process鑳藉叡浜畠銆?br />
涓嬮潰鐨勮〃寰堥噸瑕侊紝瑕佺湅娓呬笅闈㈢殑鍓嶆彁鎿嶄綔鍜屽綋鍓嶆搷浣滐紝涓昏姣旇緝浜咮locking鍜孫_NONBLOCK鏉′歡涓嬬殑鍖哄埆
]]>
鍒涘緩綆¢亾 1(fd1[0] 鍜?/span> fd1[1]) 鍜岀閬?/span> 2(fd2[0] 鍜?/span> fd2[1])
Fork
鐖惰繘紼嬪叧闂閬?/span> 1 鐨勮鍑虹錛?/span> fd1[0]錛?/span> [MS1] 聽
鐖惰繘紼嬪叧闂閬?/span> 2 鐨勫啓鍏ョ錛?/span> fd2[1] 錛?/span>
瀛愯繘紼嬪叧闂閬?/span> 1 鐨勫啓鍏ョ錛?/span> fd1[1] 錛?/span>
瀛愯繘紼嬪叧闂閬?/span> 2 鐨勮鍑虹錛?/span> fd2[0] 錛?br />
涓嬮潰鏄ず渚嬬▼搴忥細
#include 鈥渦npipe.h鈥?/span>
void client(int, int) ,server(int, int);
int main(int argc, char* argv[])
{
聽聽聽聽聽聽 int pipe1[2],pipe[2];
pid_t childpid;
Pipe(pipe1);
Pipe(pipe2);
if((childpid=fork())==0)
{
聽聽聽聽聽聽 Close(pipe1[1])聽;
聽聽聽聽聽聽 Close(pipe2[0])聽;
聽聽聽聽聽聽 server(pipe1[0],pipe2[1])聽;
聽聽聽聽聽聽 exit(0)聽;
}
close(pipe1[0])聽;
close(pipe2[1])聽;
client(pipe2[0],pipe1[1])聽;
waitpid(childpid,NULL,0)聽;
exit(0)聽;
}
//////////////////client
void client(int readfd, int writefd)
{
聽聽聽聽聽聽 size_t len;
ssize_t n;
char聽 buff[MAXLINE];
fgets(buff,MAXLINET,stdin);
len = strlen(buff);
if(buff[len-1]==鈥橽n鈥?
聽聽聽聽聽聽 len--;
write(writefd,buff,len);
while((n=read(readfd,buff,MAXLINE))>0)
聽聽聽聽聽聽聽 write(STDOUT_FILENO,buff,n);
}
///////////////////server
void server(int readfd, int writefd)
{
聽聽聽聽聽聽 int fd;
聽聽聽聽聽聽 ssize_t n;
聽聽聽聽聽聽 char buff[MAXLINE+1];
聽聽聽聽聽聽 if((n=read(readfd,buff,MAXLINE))==0)
聽聽聽聽聽聽聽聽聽 err_quit(鈥渆nd-of 鈥揻ile while reading pathname鈥?;
聽聽聽聽聽聽 if((fd =open(buff,O_RDONLY)<0)){
sprintf(buff+n,sizeof(buff)-n,鈥漜an鈥檛 open, %s\n鈥?strerror(errno));
n = strlen(buff);
write(writefd,buff,n);
}else{
聽While((n=read(fd,buff,MAXLINE))>0)
聽聽聽聽聽聽聽 Write(writefd,buff,n);
Close(fd);
}
}
Properties of Pipe:
1) Pipes do not have a name. For this reason, the processes must share a parent process. This is the main drawback to pipes. However, pipes are treated as file descriptors, so the pipes remain open even after fork and exec.
2) Pipes do not distinguish between messages; they just read a fixed number of bytes. New line (\n) can be used to separate messages. A structure with a length field can be used for message containing binary data.
3) Pipes can also be used to get the output of a command or to provide input to a command
聽[MS1] 鎹㈠彞璇濊灝辨槸鐖惰繘紼嬪綆¢亾 1 鍙兘鍐欙紝浠ヤ笅鍙互渚濇綾繪帹銆?/span>
File or Record Locking
int lockf (int fd, int function, long size)
鍏朵腑
fd
鏄枃浠舵弿榪扮
(file descripter)
錛?/span>
size
鏄攣瀹氱殑澶у皬
[offset, offset+size]
錛屽鏋?/span>
size=0,
灝辮〃紺烘枃浠朵綑涓嬬殑閮ㄥ垎銆傚彲浠ョ敤
lseek()
鍘葷Щ鍔ㄥ綋鍓嶇殑
offset
銆?/span>
鑰屽叾涓殑
function
鍙傛暟鏈変互涓嬪嚑縐嶏細
F_ULOCK, F_LOCK, F_TEST, F_TLOCK