當前位置:首頁 » 編程語言 » c語言管道

c語言管道

發布時間: 2022-07-01 20:54:32

A. 利用c語言寫一個程序實現兩個進程間進行管道通信

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#define N 10

#define MAX 100

int child_read_pipe(int fd)

{

char buf[N];

int n = 0;

while(1)

{

n = read(fd,buf,sizeof(buf));

buf[n] = '\0';

printf("Read %d bytes : %s.\n",n,buf);

if(strncmp(buf,"quit",4) == 0)

break;

}

return 0;

}

int father_write_pipe(int fd)

{

char buf[MAX] = {0};

while(1)

{

printf(">");

fgets(buf,sizeof(buf),stdin);

buf[strlen(buf)-1] = '\0';

write(fd,buf,strlen(buf));

usleep(500);

if(strncmp(buf,"quit",4) == 0)

break;

}

return 0;

}

int main()

{

int pid;

int fd[2];

if(pipe(fd) < 0)

{

perror("Fail to pipe");

exit(EXIT_FAILURE);

}

if((pid = fork()) < 0)

{

perror("Fail to fork");

exit(EXIT_FAILURE);

}else if(pid == 0){

close(fd[1]);

child_read_pipe(fd[0]);

}else{

close(fd[0]);

father_write_pipe(fd[1]);

}

exit(EXIT_SUCCESS);

}

B. c語言管道通信能向管道中輸入int型變數嗎

普通管道就可以
只要發送和接收約定好就行
寫的時候sizeof int寫進去
讀的時候同樣讀到int變數就好

C. C語言創建管道

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int main(int argc,char *argv[])
{
int pd[2];/*用於保存管道文件描述符*/

char out[80],str[]="safasfsa";/*str是要寫入的字元串,out用於保存從管道讀取的字元串*/

assert(pipe(pd)!=-1);/*斷言用於確定pipe()函數執行成功,如果管道創建失敗,則pipe()返回-1*/

if (!fork()) write(pd[1],str,strlen(str));/*創建子進程,並將字元串寫入管道*/
else {
read(pd[0],out,strlen(str));/*在主進程中從管道中讀取子進程寫入的字元串*/
printf("%s\n",out);/*主進程中輸出。*/
}
return 0;
}

D. C語言 命名管道

先准備好數據比如data.txt 然後編譯好源代碼,得到a.exe 吧兩個文件放在同一個目錄下, 在該目錄下運行命令行 輸入: a << data.txt 回車 就可以了
希望對你能有所幫助。

E. C語言,輸油管道問題

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

intmain(intargc,constchar*argv[])
{
intfd[2];
intpid;

if(argc!=2)
{
printf("Usage: %sstring ",argv[0]);
return1;
}

if(pipe(fd)<0)
{
printf("Unabletocreatepipe! ");
return1;
}

//forkchildprocess
pid=fork();

if(pid==0)//child
{
close(fd[0]);//closereadend
write(fd[1],argv[1],strlen(argv[1]));//writemessage
close(fd[1]);//closebeforeexit
}
elseif(pid>0)//parent
{
charbuf[1024];
intlen;

close(fd[1]);//closewriteend
len=read(fd[0],buf,sizeof(buf));//readfromthepipe
buf[len]='';
printf("<PARENT>messagefromchild:%s ",buf);
wait(NULL);//waitforchildexit
close(fd[0]);//closebeforeexit
}
else
{
printf("Unabletofork! ");
return1;
}

return0;
}

F. C語言管道將父進程的標准輸入,重定向到子進程。怎麼寫

你好,

用gets會有越界問題,建議使用fgets。

代碼如下:

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>


int main(int argc, const char *argv[])

{

pid_t pid;

int fd[2];


pipe(fd);


pid = fork();

if (pid == -1) {

perror("fork");

return -1;

} else if (pid == 0) { //子進程

char buf[128] = {0};

close(fd[1]);//關閉了寫,具有讀管道許可權


read(fd[0], buf, sizeof(buf));

printf("read from parent:%s ", buf);

close(fd[0]);

} else { //父進程

char buf[128] = {0};

close(fd[0]);//關閉讀,具有寫管道許可權


gets(buf);

printf("write to child ");

write(fd[1], buf, sizeof(buf));


close(fd[1]);

}

return 0;

}

祝你生活愉快。

G. C語言 為什麼管道的任務是固定的f[0]讀f[1]寫

創建管道時返回的是一對文件描述符,fd[0]讀,fd[1]寫,這個是pipe()函數的固定實現。

要說為什麼的話,管道是半雙工的,一端寫入數據流,一端讀出數據流,所以至少需要兩個文件描述符,一個讀一個寫。

H. C語言之油井管道問題

求出所有油井y坐標的平均值,以其為主管道的y值建一條與x軸平行的東西走向的管道即可。

I. C語言中怎麼用管道和進程實現雙向通信

#include "dpopen.h"
#define MAXLINE 80
int main()
{
char line[MAXLINE];
FILE *fp;
fp = dpopen("sort");
if (fp == NULL) {
perror("dpopen error");
exit(1);
}
fprintf(fp, "orange\n");
fprintf(fp, "apple\n");
fprintf(fp, "pear\n");
if (dphalfclose(fp) < 0) {
perror("dphalfclose error");
exit(1);
}
for (;;) {
if (fgets(line, MAXLINE, fp) == NULL)
break;
fputs(line, stdout);
}
dpclose(fp);
return 0;
}

輸出結果為:
apple
orange
pear

熱點內容
編譯編譯有什麼 發布:2025-02-04 08:05:52 瀏覽:732
讓外網訪問內網伺服器 發布:2025-02-04 08:02:20 瀏覽:781
奶塊腳本菜地 發布:2025-02-04 07:46:35 瀏覽:238
條形碼識別源碼 發布:2025-02-04 07:45:55 瀏覽:457
mysql資料庫數據同步 發布:2025-02-04 07:41:07 瀏覽:760
安卓手機下載哪個北斗地圖 發布:2025-02-04 07:35:26 瀏覽:854
查詢伺服器ip地址代碼 發布:2025-02-04 07:08:28 瀏覽:675
python全雙工 發布:2025-02-04 06:57:46 瀏覽:196
c語言動態內存 發布:2025-02-04 06:57:06 瀏覽:78
sql倒序查詢 發布:2025-02-04 06:49:18 瀏覽:196