当前位置:首页 » 编程语言 » 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 10:00:55 浏览:27
javaip端口 发布:2025-02-04 09:27:09 浏览:857
国产存储科技进步二等奖 发布:2025-02-04 09:13:00 浏览:693
编程课v 发布:2025-02-04 08:45:00 浏览:108
模拟器能有手机脚本么 发布:2025-02-04 08:39:50 浏览:762
android显示html图片 发布:2025-02-04 08:35:31 浏览:795
如何查学信网账号及密码 发布:2025-02-04 08:33:55 浏览:506
linux32位jdk 发布:2025-02-04 08:33:55 浏览:250
康佳服务器连接失败是怎么回事 发布:2025-02-04 08:18:51 浏览:919
编译编译有什么 发布:2025-02-04 08:05:52 浏览:739