Я пытаюсь узнать трубы, и я пытаюсь эту программу:трубы и от дочернего процесса не работает
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<sys/types.h>
#include<fcntl.h>
#define MAXLINE 100
void main(){
int pipe1[2],pipe2[2];
pid_t childpid;
if(pipe(pipe1)<0){
perror("Unable to create the pipe for pipe1");
exit(-1);
}
if(pipe(pipe2)<0){
perror("Unable to create the pipe for pipe1");
exit(-1);
}
childpid=fork();
printf("The child PID is:%d\n",childpid);
if(childpid==0){
printf("In the child process");
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0],pipe2[1]);
exit(0);
}
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0],pipe1[1]);
waitpid(childpid,NULL,0);
exit(0);
}
void client(int readfd,int writefd){
int n,len;
char buff[MAXLINE];
printf("Please enter the name of the file to be read:");
fgets(buff,MAXLINE,stdin);
len=strlen(buff);
if(buff[len-1]=='\n')
len--;
write(writefd,buff,len);
printf("File name written into the pipe\n");
printf("The num of bytes written are:\n",read(readfd,buff,MAXLINE));
while((n-read(readfd,buff,MAXLINE))>0){
printf("Trying to read the content\n");
write(STDOUT_FILENO,buff,n);
}
}
void server(int readfd,int writefd){
int fd,n;
char buff[MAXLINE + 1];
write(writefd,"Yello in the server process",strlen("Yello in the server process"));
if((n=read(readfd,buff,MAXLINE))==0)
perror("End of file while reading");
buff[n]='\0';
if((fd=fopen(buff,O_RDONLY))<0){
snprintf(buff+n,sizeof(buff)-n,"Can't open, %s",strerror(errno));
n=strlen(buff);
write(writefd,buff,n);
}
while((n=read(fd,buff,MAXLINE))>0){
write(writefd,buff,n);
close(fd);
}
}
Проблема, я введите имя файла и программа просто выходит. Я попытался выполнить дочерний процесс gdb, установив «дочерний режим« follow-fork-mode », и до сих пор ничего не происходит. Любые идеи относительно того, где я могу ошибиться?
Хорошо, еще одна дополнительная информация об отладке: Я установил режим follow-fork в child.and он вызывает ошибку сегментации при открытии файла.
Программный сигнал SIGSEGV, ошибка сегментации. [Переключение на обработку 28025] в _IO_file_fopen() 0x00197f08 из /lib/libc.so.6
Прежде всего, ваша программа не содержит необходимых включать i.e. '#include'. –
0decimal0
Вы скопировали и вставили свои сообщения об ошибках, поэтому, если вам не удалось создать 'pipe2', вам будет интересно узнать, почему' pipe1' не удалось.Возвращаемым типом 'main()' является 'int', а не' void'. Не забудьте завершить сообщения с помощью новой строки; они не появятся до вывода новой строки. –
@ user1295872 В какой системе вы работаете? – 0decimal0