简体   繁体   English

C 程序使用 fork 转换为大写

[英]C program to convert to upper case using fork

I need to create a program that has a child process and a parent process.我需要创建一个具有子进程和父进程的程序。 The child process has to convert lines sent by the parent proccess in to upper case, and the parent proccess has to send lines to the child proccess to convert, and show this converted lines by stdin.子进程必须将父进程发送的行转换为大写,父进程必须将行发送给子进程进行转换,并通过标准输入显示转换后的行。 I already have this, but at the time when i execute on my terminal, the upper case lines doesn't show.我已经有了这个,但是当我在终端上执行时,大写行没有显示。

Any sugestion任何建议

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

int main(void) {
  int p1[2];
  int p2[2];
  pid_t pid;
  char buffer[1024];
  FILE *fp1;
  FILE *fp2;

  pipe(p1);
  pipe(p2);

  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Error fork\n");
    exit(1);
  }

  if (pid == 0) {
    // Close pipes entrances that aren't used
    close(p1[1]);
    close(p2[0]);

    // Open file descriptors in used entrances
    fp1 = fdopen(p1[0], "r");
    fp2 = fdopen(p2[1], "w");

    // Read from the corresponding file descriptor (pipe extreme)
    while (fgets(buffer, 1024, fp1) != NULL) {
      for (int i = 0; i < strlen(buffer); i++) {
        buffer[i] = toupper(buffer[i]);
      }
      fputs(buffer, fp2);
    }

    // Once finished, close the reaming pipes entrances
    fclose(fp1);
    fclose(fp2);
    exit(1);
  }

  // Close unused pipes entrances
  close(p1[0]);
  close(p2[1]);

  // Open dile descriptors
  fp1 = fdopen(p1[1], "w");
  fp2 = fdopen(p2[0], "r");

  while (fgets(buffer, 1024, stdin) != NULL) {
    fputs(buffer, fp1);       // Send buffer to write line pipe
    fgets(buffer, 1024, fp2); // Get buffer readed from read line pipe
    printf("%s", buffer);     // Print in stdout the buffer
  }

  // Once finished, close the reaming pipes entrances
  fclose(fp1);
  fclose(fp2);

  // Wait fork
  wait(NULL);

  return 0;
}

When using a FILE * stream and the C library stream API, it's important to keep in mind that I/O operations can be "buffered". When using a FILE * stream and the C library stream API, it's important to keep in mind that I/O operations can be "buffered". In most cases, by default, when performing writes via fputs(...) the bytes will not actually be sent to the underlying file object (a pipe end in this case) until the buffer is flushed.在大多数情况下,默认情况下,当通过fputs(...)执行写入时,字节实际上不会被发送到底层文件 object(在这种情况下是 pipe 结束),直到缓冲区被刷新。 In your code above, you could add calls to fflush(fpN) (where N matches the numbers in your code) after both calls to fputs(...) .在上面的代码中,您可以在两次调用fputs(...)之后添加对fflush(fpN)的调用(其中 N 与代码中的数字匹配)。 This should help fix your problem.这应该有助于解决您的问题。

Note that alternatively there are ways of manually changing the buffering mode of a given file stream.请注意,另外还有一些方法可以手动更改给定文件 stream 的缓冲模式。 This information can be found in man setbuf .此信息可以在man setbuf中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C中的函数将大写字符串转换为小写的程序中的问题 - Problem in a program to convert upper case string to lowercase using function in C 将小写的字符串转换为大写? 在C中 - Convert string of lower case to upper case? In C C 程序检查字符是否为小写字母并转换为大写字母,反之亦然 - C Program to check if character is a lower case letter and convert to upper case letter and vice versa 在 C 中转换为所有大写字母错误 - Convert to all upper case letter bug in C 使用 fork() 的 C 程序的行为 - Behavior of a C program using fork() C语言如何支持大写字母而不提供或提供有关程序中大写的信息 - How does the C language support upper case letters without giving or providing information about upper case in program 我正在写 C function 使用 ASCII 将小写字符转换为大写字符,但 Output 不正确 - I am writing C function that convert lowercase char to upper case char with using ASCII but Output is not correct 在C中,为什么程序没有重新协调第二个if语句或大写char变量? - in C why the program is not reconizing the second if statement or upper case char variable? 使用fork和exec在C linux中执行程序 - executing a program in C linux using fork and exec 使用管道和分支用C语言编程? - Program in C language using pipe and fork?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM