Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c中使用管道()的程序在read()上卡住_C_Ubuntu_Pipe_Fork_System Calls - Fatal编程技术网

在c中使用管道()的程序在read()上卡住

在c中使用管道()的程序在read()上卡住,c,ubuntu,pipe,fork,system-calls,C,Ubuntu,Pipe,Fork,System Calls,我的代码如下: 我正在使用c语言中的管道系统调用。这里我的程序被卡在读取(管道1[0],接收,20) 行前: printf(“duckkkk\n”) 输出为: In parent Enter the value In Child 守则: #include <unistd.h> #include <stdio.h> #include <string.h> #include<sys/wait.h> int isPalindrome(char st

我的代码如下: 我正在使用c语言中的管道系统调用。这里我的程序被卡在
读取(管道1[0],接收,20)
行前:
printf(“duckkkk\n”)

输出为:

In parent 
Enter the value
In Child
守则:

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include<sys/wait.h>

int isPalindrome(char str[]);

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

int the_pipe_1[2];
int the_pipe_2[2];

char recieved[20];
char input[20];
char pal[10];
int palchid;

int  pipeVal = pipe(the_pipe_1);
if (pipeVal == -1 ) {
   printf("Pipe 1 failed\n");
 }
if (pipe(the_pipe_2) == -1 ) {
   printf("Pipe 2 failed\n");
 }

 int forkVal =  fork();

 if (forkVal > 0) {
    printf("In parent \n");
    close(the_pipe_1[0]);
    printf("Enter the value\n");
    gets(input);
    write(the_pipe_1[1],(char) input,20);

    wait(NULL);
    close(the_pipe_2[1]);
    read(the_pipe_2[0],pal,10);
    if(pal == "0")
    {
       printf("Not plaindrome\n");
    }
    else
       printf("Plaindrome\n");

     }


  else if(forkVal == 0)
  {
     printf("In Child\n");
     close(the_pipe_1[1]);

     read(the_pipe_1[0],recieved,20);
     printf("DUCKKKKKK\n");

     printf("Val of recieved %s \n",&recieved );

     palchid = isPalindrome(recieved);
     close(the_pipe_2[0]);
     write(the_pipe_2[1],(char)palchid,10);


   }
   return 0;
 }

int isPalindrome(char str[])
{
   int l = 0;
   int h = strlen(str) - 1;

   while (h > l)
   {
      if (str[l++] != str[h--])
      {
        return 0;
      }
   }
return 1;
}
#包括
#包括
#包括
#包括
int isPalindrome(字符str[]);
int main(int argc,char const*argv[]{
对管道进行int_1[2];
对_管道_2[2];
收到的字符[20];
字符输入[20];
char-pal[10];
内帕奇德;
int pipeVal=管道(管道1);
如果(pipeVal==-1){
printf(“管道1失败\n”);
}
如果(管道(管道2)=-1){
printf(“管道2失败\n”);
}
int-forkVal=fork();
如果(forkVal>0){
printf(“在父项中”);
关闭(_管道_1[0]);
printf(“输入值”);
获取(输入);
写入(_管道_1[1],(char)输入,20);
等待(空);
关闭(管道2[1]);
读取(管道2[0],pal,10);
如果(pal=“0”)
{
printf(“非平原区”);
}
其他的
printf(“平原区”);
}
else if(forkVal==0)
{
printf(“子项中的\n”);
关闭(管道1[1]);
读取(管道1[0],已接收,20);
printf(“duckkkk\n”);
printf(“已接收%s\n的值,&Received”);
palchid=isPalindrome(已接收);
关闭(_管道_2[0]);
写(the_pipe_2[1],(char)palchid,10);
}
返回0;
}
int isPalindrome(字符str[])
{
int l=0;
inth=strlen(str)-1;
而(h>l)
{
如果(str[l++]!=str[h--])
{
返回0;
}
}
返回1;
}

简单。将行更改为:

write(the_pipe_1[1], input, 20);
而且效果很好

说明: 当您将输入(通常是32位指针)强制转换为char(8位)时,您创建了一个指向非法地址的指针。在某些访问上,这将导致分段错误。例如,您可以尝试:

//segmentation fault
printf("input - %s\n", (char)input);
但是对于write(),它的工作方式不同(没有深入探讨原因),这反而导致程序卡住

附言。 这一行永远不会为真:

if(pal == "0")

它没有被卡住,您可以调用
get
来接收来自stdin的输入,只要您不键入一些输入,您的程序就会被卡住。也可以考虑使用<代码> fgs<代码>,而不是<代码>获取。您应该检查编译器版本/编译器选项——由于该代码不应该出现的几个错误。compile@HarianjaLundu我给出了输入,但之后它就卡住了input@Odysseus我正在使用gcc最新版本version@AbdullahSultan使用gcc-7.4.0(ubuntu-18-04.1)由于使用gets()和2个无效转换,无法编译代码:
write(管道1[1],(char)输入,20)
写入(管道2[1],(char)palchid,10)-奥德修斯13分钟前工作得很出色,请你解释一下这个问题!根本不需要强制转换:
write(管道1[1],输入,20)
What heck我刚刚删除了类型强制转换,它正在工作。在它不起作用之前。但现在,我的系统疯了吗?