Write a C program to implement the ls | sort command. (Use unnamed Pipe)
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int fd[2];
if(pipe(fd) == -1) {
perror("ERROR creating a pipe\n");
exit(1);
}
if (!fork()) {
close(1); // close the standard output file
dup(fd[1]); // now fd[1] (the writing end of the pipe) is the standard output
close(fd[0]); // I am the writer. So, I don’t need this fd
execlp("ls", "ls", NULL);
}
else {
close(0); // close the standard input file
dup(fd[0]);
close(fd[1]);
execlp("sort", "sort", "-r", NULL);
}
return(0);
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int fd[2];
if(pipe(fd) == -1) {
perror("ERROR creating a pipe\n");
exit(1);
}
if (!fork()) {
close(1); // close the standard output file
dup(fd[1]); // now fd[1] (the writing end of the pipe) is the standard output
close(fd[0]); // I am the writer. So, I don’t need this fd
execlp("ls", "ls", NULL);
}
else {
close(0); // close the standard input file
dup(fd[0]);
close(fd[1]);
execlp("sort", "sort", "-r", NULL);
}
return(0);
}
This comment has been removed by the author.
ReplyDeleteDude can u write a c program to mimic sort command
ReplyDeleteOhkk 😁😁
DeleteHey share the sort command code as well
ReplyDeletehttps://github.com/jbabuc/nweb
ReplyDelete