为什么这个命名管道不打印发送的行?

发布于 2024-10-05 06:29:38 字数 3268 浏览 6 评论 0原文

以下服务器在运行时创建一个命名管道:

./serverprogram -p nameofthepipe -t 99

t 后面的 optarg 表示要创建的线程数(此处未完成)。

无论如何,管道在这里不起作用:

/* Open the first named pipe for reading */
    int rdfd = open(pipeName, O_RDONLY);

 /* Read from the first pipe */
int numread = read(rdfd, command_and_pid, 280);


printf("what's being read is %s \n", command_and_pid);  // not printing!!1! 

为什么?

服务器程序:

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>



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

char pipeName[30];
int  numThreads; 

char command_and_pid[280];


  int opcion;
        if (argc < 2) {
        printf ("ERROR: Missing arguments\n");//
        exit(1);
   }
  opterr = 0;




while ((opcion = getopt (argc, argv, "p:t:w")) != -1)
{
        switch (opcion) {

               case 'p': // -p indica el nombre del pipe
               printf("The name of the pipe is: %s\n",optarg);
               strcpy(pipeName, optarg);

               break;

               case 't'://-t indica los hilos 
        printf("The number of threads is: %s\n",optarg);
               numThreads= atoi(optarg);

               break;

               case '?':
        fprintf(stderr,"no reconozco esa opcion\n");
               break;
        }
}





    int ret_val = mkfifo(pipeName, 0666);

    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (0);
    }


     /* Open the first named pipe for reading */
    int rdfd = open(pipeName, O_RDONLY);

     /* Read from the first pipe */
    int numread = read(rdfd, command_and_pid, 280);


    printf("what's being read is %s \n", command_and_pid);  // not printing!!1! 


    close(rdfd);


   return 0;
}

客户端程序:

#include <unistd.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>  
#include <pthread.h>





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



        char pipeName[30]; 

        printf("write the name of the pipe used to write to the server \n");

        fgets(pipeName,30, stdin);

         /* Open the first named pipe for writing */
        int wrfd = open(pipeName, O_WRONLY);


         printf("write the name of the command you want to execute \n");  

         char command_and_pid[280];
         char command[250];


          fgets(command,250, stdin);
          puts(command); //quitar

         strcpy(command_and_pid,command);
         strcat(command_and_pid," ");


         int pipeIntId; 

         char pidstring [30];

         int pid= getpid(); 

         sprintf(pidstring,"%d", pid);

         strcat(command_and_pid,pidstring); 

         int written;

         written=write(pipeIntId,command_and_pid,280); 
         //write to the pipe          
         // send the command and pid 


         close(pipeIntId); // close write pipe      


 return 0;
}

The following server creates a named pipe when it's run like this:

./serverprogram -p nameofthepipe -t 99

the optarg after t indicates a number of threads to be created (not done here).

Anyway, the pipe isn't working here:

/* Open the first named pipe for reading */
    int rdfd = open(pipeName, O_RDONLY);

 /* Read from the first pipe */
int numread = read(rdfd, command_and_pid, 280);


printf("what's being read is %s \n", command_and_pid);  // not printing!!1! 

Why?

Server program:

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>



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

char pipeName[30];
int  numThreads; 

char command_and_pid[280];


  int opcion;
        if (argc < 2) {
        printf ("ERROR: Missing arguments\n");//
        exit(1);
   }
  opterr = 0;




while ((opcion = getopt (argc, argv, "p:t:w")) != -1)
{
        switch (opcion) {

               case 'p': // -p indica el nombre del pipe
               printf("The name of the pipe is: %s\n",optarg);
               strcpy(pipeName, optarg);

               break;

               case 't'://-t indica los hilos 
        printf("The number of threads is: %s\n",optarg);
               numThreads= atoi(optarg);

               break;

               case '?':
        fprintf(stderr,"no reconozco esa opcion\n");
               break;
        }
}





    int ret_val = mkfifo(pipeName, 0666);

    if ((ret_val == -1) && (errno != EEXIST)) {
        perror("Error creating the named pipe");
        exit (0);
    }


     /* Open the first named pipe for reading */
    int rdfd = open(pipeName, O_RDONLY);

     /* Read from the first pipe */
    int numread = read(rdfd, command_and_pid, 280);


    printf("what's being read is %s \n", command_and_pid);  // not printing!!1! 


    close(rdfd);


   return 0;
}

Client program:

#include <unistd.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>  
#include <pthread.h>





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



        char pipeName[30]; 

        printf("write the name of the pipe used to write to the server \n");

        fgets(pipeName,30, stdin);

         /* Open the first named pipe for writing */
        int wrfd = open(pipeName, O_WRONLY);


         printf("write the name of the command you want to execute \n");  

         char command_and_pid[280];
         char command[250];


          fgets(command,250, stdin);
          puts(command); //quitar

         strcpy(command_and_pid,command);
         strcat(command_and_pid," ");


         int pipeIntId; 

         char pidstring [30];

         int pid= getpid(); 

         sprintf(pidstring,"%d", pid);

         strcat(command_and_pid,pidstring); 

         int written;

         written=write(pipeIntId,command_and_pid,280); 
         //write to the pipe          
         // send the command and pid 


         close(pipeIntId); // close write pipe      


 return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2024-10-12 06:29:38

在客户端中,fgets 将换行符保留在行尾,因此您需要在打开文件之前将其删除。

另外,在给定的代码中,您正在打开 wrfd,但写入未初始化的 pipelineIntId(尽管您可能正在从此处的函数中提取某些内容)。

In the client, fgets keeps the newline at the end of the line, so you'll need to strip that before opening the file.

Also, in the code as given, you're opening wrfd but writing to pipeIntId, which is uninitialized (though perhaps you are extracting something from a function here).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文