管道linuxc
A. 用linux C命名管道實現進程間通信的題目,已寫出代碼,但有一個神奇的小問題,open路徑正確卻有時失敗
通常管道創建的時候會出現創建不成功的情況,要在管道創建的時候就進行異常處理,如果創建不成功就重新創建,這個錯誤是找不到管道文件
B. Linux C,有名管道寫端每1秒寫入一次數據,讀端每5秒讀出一次數據。讀端為什麼每次只能讀出最新的數據,
01 #includestdio.h02 #includeunistd.h0304 int main()05 {06 int n,fd[2]; // 這里的 fd 是文件描述符的數組,用於創建管道做准備的07 pid_t pid;08 char line[100];09 if(pipe(fd)0) // 創建管道10 printf("pipe create error\n");1112 if((pid=fork())0) //利用 fork()創建新進程13 printf("fork error\n");1415 else if(pid0){ //這里是父進程,先關閉管道的讀出端,然後在管道的寫端寫入「hello world"16 close(fd[0]);17 write(fd[1],"hello word\n",11);18 }19 else{20 close(fd[1]); //這里是子進程,先關閉管道的寫入端,然後在管道的讀出端讀出數據21 n= read(fd[0],line,100);22 write(STDOUT_FILENO,line,n);23 }24 exit(0);25 }
如果瀏覽器不支持 cookie 該怎麼辦?
如果您的應用程序涉及不支持 cookie 的瀏覽器,您就不得不採取其他方法在應用程序中從一張頁面向另一張頁面傳遞信息。一種方式是從表單傳遞數據(有關表單和用戶輸入的內容,稍早前我們已經在本教程中介紹過了)。
下面的表單在用戶單擊提交按鈕時向 "welcome.php" 提交了用戶輸入:Name:Age:
C. 在linux下可以用命名管道實現c程序與qt的數據通信嗎
當然可以了。不過可以直接使用dbus進行進程間通訊,C程序發送數據(libdbus),Qt去捕獲信號(QDbus),這樣來的更方便點,否則你要自己封裝管道的收發介面。
D. Linux C: 1、啟動A進程,創建一有名管道,並向其寫入一些數據 2、啟動B進程,從A創建的有名管道中讀出數據
01 #include<stdio.h>
02 #include<unistd.h>
03
04 int main()
05 {
06 int n,fd[2]; // 這里的 fd 是文件描述符的數組,用於創建管道做准備的
07 pid_t pid;
08 char line[100];
09 if(pipe(fd)<0) // 創建管道
10 printf("pipe create error\n");
11
12 if((pid=fork())<0) //利用 fork()創建新進程
13 printf("fork error\n");
14
15 else if(pid>0){ //這里是父進程,先關閉管道的讀出端,然後在管道的寫端寫入「hello world"
16 close(fd[0]);
17 write(fd[1],"hello word\n",11);
18 }
19 else{
20 close(fd[1]); //這里是子進程,先關閉管道的寫入端,然後在管道的讀出端讀出數據
21 n= read(fd[0],line,100);
22 write(STDOUT_FILENO,line,n);
23 }
24 exit(0);
25 }
E. linux c語言 管道pipe的問題
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int fd[2];
int pid;
if (argc != 2)
{
printf("Usage:\n\t%s string\n", argv[0]);
return 1;
}
if (pipe(fd) < 0)
{
printf("Unable to create pipe!\n");
return 1;
}
// fork child process
pid = fork();
if (pid == 0) //child
{
close(fd[0]); //close read end
write(fd[1], argv[1], strlen(argv[1])); //write message
close(fd[1]); //close before exit
}
else if (pid > 0) //parent
{
char buf[1024];
int len;
close(fd[1]); //close write end
len = read(fd[0], buf, sizeof(buf)); //read from the pipe
buf[len] ='\0';
printf("<PARENT> message from child: %s\n", buf);
wait(NULL); //wait for child exit
close(fd[0]); //close before exit
}
else
{
printf("Unable to fork!\n");
return 1;
}
return 0;
}
F. linux下c語言pipe無名管道 main函數創建兩個進程p1和p2,p1把一個文件名通過管道給main進程,main進程打開
#include <stdio.h>
main()
{
int i,r,p1,p2,fd[2];
char buf[50],s[50];
pipe(fd); //創建匿名管道,fd[0]為讀端,fd[1]為寫端
while((p1=fork())==-1); //創建子進程P1,直至成功為止(p1!=-1)
if(p1==0) //子進程P1執行邏輯
{
lockf(fd[1],1,0); //鎖定管道寫端,保證寫入數據的完整性
sprintf(buf,"child process P1 is sending messages!\n"); //在buf中填入准備寫入管道的信息數據
printf("child processP1!\n"); //列印「子進程P1正在運行」
write(fd[1],buf,50); //向管道寫端fd[1]寫入buf中的數據,寫完後該數據即可以從讀端fd[0]讀出
sleep(5); //睡眠5秒
lockf(fd[1],0,0); //解鎖管道寫端
exit(0); //子進程P1退出
}
else //主進程的執行邏輯
{
while((p2=fork())==-1); //創建第二個子進程P2
if(p2==0) //子進程P2的執行邏輯
{
lockf(fd[1],1,0); //鎖定管道寫端,保證數據寫入完整
sprintf(buf,"child process P2 is sending messages!\n"); //在buf中填入准備寫入管道的信息數據
printf("child processP2!\n"); //列印「子進程P2正在運行」
write(fd[1],buf,50); //向管道寫端fd[1]寫入buf中的數據,寫完後該數據即可從讀端fd[0]讀出
sleep(5); //睡眠5秒
lockf(fd[1],0,0); //解鎖管道寫端
exit(0); //子進程P2退出
}
//以下為主進程執行邏輯
wait(0); //等待某個子進程退出
if(r=read(fd[0],s,50)==-1) //從管道讀端fd[0]讀取P1或者P2寫入的數據(視哪個子進程搶先執行到lockf函數)
{
printf(:can't read pipe\n"); //讀取失敗,列印錯誤信息
}
else
{
printf(:%s\n",s); //列印出讀到的信息數據
}
wait(0); //等待第二個子進程退出
if(r=read(fd[0],s,50)==-1) //從管道讀端fd[0]讀取出P1或者P2寫入的數據(視哪個子進程後執行到lockf函數)
{
printf(:can't read pipe\n"); //讀取失敗,列印錯誤信息
}
else
{
printf(:%s\n",s); //列印讀取到的信息數據
}
exit(0); //主進程退出
}
}
總的說來,就是主進程創建了兩個子進程P1、P2,這兩個子進程分別向管道寫入了一行文字,然後主進程從管道另一端將這兩行文字讀出並列印出來
由於進程的並發執行性,哪個子進程的信息先寫到管道是隨機的,因此該程序每次運行的輸出可能並不相同,兩行文字之間可能會相互交換
G. Linux用C語言怎麼將telnet反饋的結果寫入管道
將標准輸出重定向到管道寫端
H. linux下C進程之間管道通信的問題,懂的進來看下這段程序怎麼運行起來不對
你的要求 「管道尾寫個數據後,管道頭讀,然後在讓管道尾寫,一直循環」,其實是一個進程同步的問題,即一邊寫完了,另外一邊讀,讀完了,才能再寫。所以要用到進程間通信的方式來讓讀段通知寫端我已經讀完了,你可以寫下一個了。 有很多種方法來實現這個進程同步,比如用信號量等。不過既然你是一個管道的代碼,我就用了管道的方式來實現,讓你參考。下面的代碼創建了兩個管道,pipe_a2b 這個就是你原來的那個管道, pipe_b2a 這個是新加的,用來讓讀端通知寫端可以繼續的。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main()
{
char r_buf[10];
char w_buf[10];
pid_t pid;
int pipe_a2b[2]; /* parent write, child read */
int pipe_b2a[2]; /* child write, parent read */
memset(r_buf,0,sizeof(r_buf));
if(pipe(pipe_a2b)<0)
{
printf("creat pipe error\n");
exit(1);
}
if(pipe(pipe_b2a)<0)
{
printf("creat pipe error\n");
exit(1);
}
pid=fork();
if (pid == 0)
{
/* child, reader */
close(pipe_a2b[1]);
close(pipe_b2a[0]);
while(1)
{
read(pipe_a2b[0],r_buf,10);
printf("FIFO :%s\n",r_buf);
write(pipe_b2a[1], "c", 1); /* tells parent continue */
}
close(pipe_a2b[0]);
close(pipe_b2a[1]);
}
if(pid>0)
{
/* parent, writer */
close(pipe_a2b[0]);
close(pipe_b2a[1]);
while(1)
{
printf("please input w_buf:\n");
scanf("%s",w_buf);
write(pipe_a2b[1],w_buf,strlen(w_buf));
/* wait for the "c" from child to continue to next write */
read(pipe_b2a[0], r_buf, 10);
}
close(pipe_a2b[1]);
close(pipe_b2a[0]);
}
exit(0);
}
I. linux下C語言編程,管道,p,fork,疑問的是,為什麼連用那麼多close必須要close 代碼如下
文件描述符0,1,2分別表示標准輸入標准輸出,標准錯誤輸出, 所以在子進程里close(1)是關閉了標准輸出, 然後用p(fda[1]);此時未用的最小文件描述符就是1(被關閉);這里關閉fda[0]就是為了說明在子進程是管道的寫端(fda[0],不關閉是可以的為了保險起見關閉).然後子進程退出會調用系統程序ls,於是當前的文件目錄就被發送到管道中.父進程同理, 就是將標准輸出作為管道的讀端,它讀到的是子進程ls後的內容,對文件計數,
J. Linux C 通過管道實現文件復制
#include"stdio.h"
#include"stdlib.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"string.h"
#include"fcntl.h"
#include"errno.h"
#define FIFO1 "/tmp/fifo"
#define MAXLINE 100
void client1(int);
void client2(int);
int main(int argc,char **argv)
{
int writefd;
writefd=open(FIFO1,O_WRONLY,0);
client1(writefd);
client2(writefd);
close(writefd);
unlink(FIFO1);
exit(0);
}
void client1(int writefd1)//實現從文件寫到管道,自己創建一個aa.txt文件
{
char buff[MAXLINE];
int fd;
memset(buff,0,sizeof(buff));
fd=open("aa.txt",O_CREAT|O_RDWR,S_IRWXU);
read(fd,buff,sizeof(buff));
write(writefd1,buff,sizeof(buff));
close(fd);
}
void client2(int writefd2)//)//實現從管道寫到另一個文件
{
char buff[MAXLINE];
int fd;
fd=open("bb.txt",O_CREAT|O_RDWR,S_IRWXU);
read(writefd2,buff,sizeof(buff));
write(fd,buff,sizeof(buff));
close(fd);
}
我這里省略了許多判斷,自己加哈
如有問題,在線交流