當前位置:首頁 » 操作系統 » linuxpipe

linuxpipe

發布時間: 2022-03-04 22:52:37

linux pipe 判斷是否有數據

判斷是否為空可以用if來判斷
if [ "$str" = "" ]

❷ 請教!LINUX下的pipe和fork輸出的問題,高分懸賞,有加分!

你的問題其實很簡單,就是一個多進程同步的問題, 解決同步問題有很多種方法,最常見的是用信號量,消息隊列等。既然你題目要求使用pipe,我就在下面的例子中用pipe來實現。 思路很簡單,通過兩個pipe實現 父子進程間的同步, 父進程總是向第一個pipe寫,從第二個pipe讀,子進程相反。 當進程讀到內容後,則它可以print,print完一個字元後,它寫另一個pipe來通知另一個進程print。依次交替。
在我的例子中,由父進程式控制制總共列印60個字元後結束(兩個進程分別列印30次)。
這個例子很簡單,就沒什麼需要再多解釋的了,自己看吧,如果對這個例子還有什麼不懂的,可以 hi我。 別忘加分, :)

#include <stdio.h>
#include <unistd.h>

/* delay 500ms for each print */
#define DELAY_TIME (1000*500)

int main()
{
int pid, i = 0, cmd;
int p1[2], p2[2];
pipe(p1);
pipe(p2);

pid = fork();

if (pid > 0)
{
/* parent */
close(p1[0]);
close(p2[1]);

for (i = 0; i < 30; i++)
{
printf("%d", (i%10));
fflush(stdout);
usleep(DELAY_TIME); /* sleep to make print slow */

/* write to p1 to tell child continue */
cmd = i;
write(p1[1], &cmd, sizeof(cmd));

/* read from p2 to wait for child complete */
read(p2[0], &cmd, sizeof(cmd));
}

/* -1 to tell child quit */
cmd = -1;
write(p1[1], &cmd, sizeof(cmd));
wait(NULL);
printf("Parent done, quit...\n");
}
else
{
close(p1[1]);
close(p2[0]);

while(read(p1[0], &cmd, sizeof(cmd)) == sizeof(cmd)
&& cmd >= 0)
{
i = cmd;
printf("%c", 'A'+(i%26));
fflush(stdout);
usleep(DELAY_TIME); /* sleep to make print slow */
/* write to p2 to tell parent continue */
write(p2[1], &cmd, sizeof(cmd));
}
printf("\n\nChild receive finish command from parent, quit...\n");
}

return 0;
}

❸ Linux 中 gcc -v ,-### ,-pipe ,-combine 分別是意思,怎麼用

一次問的真不少,不過都不怎麼常用。
使用man gcc來找答案。
終端里man gcc後鍵入
/pipe來查找,然後按n查找下一個。
重復以上動作查找/v /### /combine
-v
這個一般是查看版本信息的。
-pipe
Use pipes rather than temporary files for communication between the
various stages of compilation. This fails to work on some systems
where the assembler is unable to read from a pipe; but the GNU
assembler has no trouble.(在不同的編譯階段使用管道替代臨時文件,某特別匯編器無法從管道讀取的系統無法使用這個參數,但是GNU的匯編器沒有這個錯誤)
-###
Like -v except the commands are not executed and arguments are
quoted unless they contain only alphanumeric characters or "./-_".
This is useful for shell scripts to capture the driver-generated
command lines. (跟-v一樣)

奇怪。我沒查到combine ????

❹ linux pipe();父進程需要close(fd[0]);子進程close(fd[1]);為什麼都需要關閉一個

管道裡面是位元組流,父子進程都寫、都讀,就會導致內容混在一起,對於讀管道的一方,解析起來就比較困難。常規的使用方法是父子進程一方只能寫入,另一方只能讀出,管道變成一個單向的通道,以方便使用。

❺ linux中的pipe和fifo的區別

linux中的pipe和fifo的區別
在linux進程間通信(IPC)可以通過信號量、文件系統、消息隊列、共享內存還有管道來實現的。其中消息隊列、內存管理是在System V中提出的。
進程通信間涉及到了管道,而且管道在shell命令中也大有用處。那就簡要說說管道:
管道顧名思義,你可以將其理解為日常生活中的管子,一邊流入,一邊流出。它可以有半雙工和全雙工。半雙工就是只能一邊流入,另一邊流出;全雙工則是一邊可以流入,也可以流出。
pipe就是一種半雙工的管道。其中,fd[1] 用來向管道中寫入數據,而fd[0]在另一端用來讀出數據。如果現有兩個進程要利用pipe進行通信。此時,就要保證只能有一個寫入端和一個讀出端,即:fd[1]和fd[0]只能有一個。
如下程序:
/*實現子進程向管道中寫入數據,父進程讀出數據*/

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
pid_t childpid;
int fd[2],nbytes;
char string[]="data from child process\n";
char buf[100];

if(pipe(fd)<0)
{
perror("pipe");
exit(1);
}
if((childpid=fork())==-1)
{
perror("fork");
exit(1);
}
if(childpid==0)
{
close(fd[0]);
printf("childpid =%2d\n",getpid());
write(fd[1],string,strlen(string));
exit(0);
}
else
{
close(fd[1]);
printf("parentpid =%2d\n",getppid());
nbytes=read(fd[0],buf,sizeof(buf));
printf("Received string:%s\n",buf);
}
return 0;
}
下面來說道fifo:
fifo是一種全雙工,即:它的一端既可以進行讀取fd[0],也可以進行寫入fd[1]。
正因為它的這種通信方式,使其可以用來涉及基於C/S模式的網路通信。具體做法:
首先讓伺服器產生一個伺服器端的FIFO,然後讓各個客戶端產生以其PID為名稱的客戶端的FIFO,在客戶於伺服器進行通信時,客戶端向伺服器端發送自己的PID,以使伺服器對客戶的請求進行響應時,向其客戶端的FIFO寫入響應信息。代碼實現客戶端和伺服器進行各自的名稱和PID交換。

❻ linux/unix 的 shell編程里 pipe功能指的是什麼功能

管道功能,簡單說就是將一個命令的輸出傳遞給另一個命令作為輸入或寫入某個文件。就是命令間的數據交換渠道。

熱點內容
伺服器的遠程埠被關了如何打開 發布:2024-09-23 18:33:22 瀏覽:228
phpjs注入 發布:2024-09-23 18:31:51 瀏覽:595
高性能php應用開發 發布:2024-09-23 18:23:56 瀏覽:208
廣東雲存儲空間開發 發布:2024-09-23 18:21:47 瀏覽:383
易語言怎麼架伺服器 發布:2024-09-23 18:21:46 瀏覽:789
hibernate緩存清除緩存 發布:2024-09-23 18:11:01 瀏覽:364
安卓導航模式在哪裡 發布:2024-09-23 18:05:22 瀏覽:55
吉利博瑞ge配置有哪些不同 發布:2024-09-23 18:05:21 瀏覽:114
紅米手機刷新密碼是多少 發布:2024-09-23 17:59:26 瀏覽:699
codeblocks帶編譯器下載 發布:2024-09-23 17:58:03 瀏覽:925