Sort Numbers from File Using Unix Pipes in C Last Updated : 30 Jan, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will write a straightforward C program that sorts numerical data from a file using pipes and forks.The sorting process is carried out using the sort command and the execlp function of UNIX. Working of the ProgramWe need to include the following header files to use the relevant functions:#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/wait.h>#include <fcntl.h>The name of the file to be read will be taken as the command line argument.After that, we create a pipe and store the file descriptor for it in the fd[] array.Then we create another process using fork() call.In the child process, sort() system call is called for the file passed as a command line argument, and the data returned by the sort() call is sent to the pipe. In the parent process, the data is received and read using the buffer and is printed on the console.C Program to Sort Numbers from File using Unix Pipes C // C Program to illustrate how to sort numbers from file using unix pipes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> #include <fcntl.h> #define buffer 1024 int main(int argc, char *argv[]) { int id, fd[2]; char my_copy[buffer]; // Check if the user provides the correct number of arguments if (argc != 2) { printf("Usage: %s <input_file>\n", argv[0]); exit(1); } // Create a pipe if (pipe(fd) == -1) { perror("Pipe creation failed"); exit(2); } // Fork a child process id = fork(); // Check for fork errors if (id == -1) { printf("An error occurred with fork\n"); exit(3); } // Child process if (id == 0) { //Close read of the pipe close(fd[0]); //redirect stdout to pipe dup2(fd[1], STDOUT_FILENO); //execute 'sort' command execlp("sort", "sort", "-n", argv[1], NULL); perror("Error calling execl"); exit(4); } else { // Parent process //Close write of the pipe close(fd[1]); //wait for the child process to finish wait(NULL); // Open the read the pipe as a file FILE *pipe_fd = fdopen(fd[0], "r"); // Checking if the pipe is null if (pipe_fd == NULL) { perror("fdopen"); exit(5); } // Reading the data from the pipe and print them while ((fgets(my_copy, sizeof(my_copy), pipe_fd)) != NULL) { //Checks when there is a line break and prints the data if (strchr(my_copy, '\n') != NULL) { printf("Data received through pipe:%s", my_copy); } } // Close the FILE fclose(pipe_fd); } return 0; } OutputWhile running program, provide the file name you want to read, ./your_program_name [input file]Now assume that your file contains the the following data file.txt 28432168432184Then the output will be Data received through pipe:2Data received through pipe:84Data received through pipe:84Data received through pipe:321Data received through pipe:321Data received through pipe:684 Comment More infoAdvertise with us Next Article C program for pipe in Linux 8rnnv764ekbx7dz44cilg9q1g2z7dr09nwcyg9c4 Follow Improve Article Tags : C Language cpp-file-handling C++ File Programs Similar Reads Sort an array using socket programming in C Given an array of unsorted positive integer, sort the given array using the Socket programming . Examples: Input : 4 5 6 1 8 2 7 9 3 0 Output :0 1 2 3 4 5 6 7 8 9 Input : 9 8 1 4 0 Output : 0 1 4 8 9 Compile these files using gcc command (gcc client.c -o client and gcc server.c -o server). Run the p 3 min read Named Pipe or FIFO with example C program In computing, a named pipe, also known as a FIFO (First In, First Out), is a powerful mechanism for inter-process communication (IPC). Unlike unnamed pipes, which are temporary and exist only as long as the process that created them is running, named pipes provide a persistent communication channel 5 min read C program for pipe in Linux Working and implementation of Pipe in Linux. Prerequisite : Pipe in Linux Approach : Pipe is highly used in Linux. Basically, pipe has 2 parts, one part is for writing and another is used for reading. So, an array of size 2 is taken. a[1] is used for writing and a[0] for reading.After reading from p 1 min read C program for pipe in Linux Working and implementation of Pipe in Linux. Prerequisite : Pipe in Linux Approach : Pipe is highly used in Linux. Basically, pipe has 2 parts, one part is for writing and another is used for reading. So, an array of size 2 is taken. a[1] is used for writing and a[0] for reading.After reading from p 1 min read C program for pipe in Linux Working and implementation of Pipe in Linux. Prerequisite : Pipe in Linux Approach : Pipe is highly used in Linux. Basically, pipe has 2 parts, one part is for writing and another is used for reading. So, an array of size 2 is taken. a[1] is used for writing and a[0] for reading.After reading from p 1 min read Pass the value from child process to parent process Prerequisite: Pipe() and Fork() Basic Write a C program in which the child process takes an input array and send it to the parent process using pipe() and fork() and then print it in the parent process. Examples: Suppose we have an array a[]={1, 2, 3, 4, 5} in child process, then output should be 1 2 min read Like