(這是友善之臂的**和備註。希望初學者有用~~~~~~~~)
一.簡介
本列程是由友善之臂說明書中的管道列程的備註版。有助於大家了解select函式以及程式的實現方式。
二.重點掌握函式:
select; struct timeval結構體; fd_zero; fd_set;
三.要求水平
可使用內來進行程序通訊。
#include #include #include #include #include #include #include #include #include static int led_fd;
static int type = 1;//貌似模式選擇
static void push_leds(void)
if (step < 3) else
break;
case 1: //級數燈
if (step > 255)
led_bitmap = step;
break;
default:
led_bitmap = 0;
} step++;
for (i = 0; i < 4; i++)
}int main(void)
if (led_fd < 0)
unlink("/tmp/led-control"); //清空名字為"/tmp/led-control"的檔案
mkfifo("/tmp/led-control", 0666); //建立管道
led_control_pipe = open("/tmp/led-control", o_rdonly | o_nonblock);//唯讀 非阻塞
if (led_control_pipe < 0)
null_writer_fd = open("/tmp/led-control", o_wronly | o_nonblock);//只寫 非阻塞
if (null_writer_fd < 0)
for (;;)
if (ret == 0) else if (fd_isset(led_control_pipe, &rds))
if (read(led_control_pipe, &c, 1) != 1)
if (c == '\r')
if (c == '\n')
fprintf(stderr, "type is %d, period is %lf\n", type, period);
memset(buffer, 0, sizeof buffer);
break;
}buffer[len] = c; //將管道中的資料裝入buffer中
}} }
close(led_fd);
return 0;
}
管道及有名管道
管道和有名管道是最早的程序間通訊機制之一,管道可用於具有親緣關係程序間的通訊,有名管道克服了管道沒有名字的限制,因此,除具有管道所具有的功能外,它還允許無親緣關係程序間的通訊。認清管道和有名管道的讀寫規則是在程式中應用它們的關鍵,本文在詳細討論了管道和有名管道的通訊機制的基礎上,用例項對其讀寫規則進...
有名管道管道程式設計
linux程序和程序之間有多種通訊方式。linux程序間通訊的主要方式有 1 無名管道 2 有名管道 3 訊號 4 訊息佇列 5 共享記憶體 6 訊號量 7 套接字 管道操作是比較簡單的通訊方式,乙個程序往管道中寫入資料,另乙個程序從管道中讀出資料。管道包括無名管道和有名管道。前者只能用於父程序和子...
FIFO 有名管道
有名管道相關的關鍵概念 管道應用的乙個重大限制是它沒有名字,因此,只能用於具有親緣關係的程序間通訊,在有名管道 named pipe或fifo 提出後,該限制得到了克服。fifo不同於管道之處在於它提供乙個路徑名與之關聯,以fifo的檔案形式存在於檔案系統中。這樣,即 使與fifo的建立程序不存在親...