Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a list of names, that when you run the command line sort < Names.txt | uniq | wc -l gives a list of

I have a list of names, that when you run the command line "sort < Names.txt | uniq | wc -l" gives a list of 23 unique names. my task is to write a program to complete the same task however I am getting 0 as my output and i'm not sure why. below is the list of names and my code

Names.txt ----------------

Tony

Linda

Mark

Adam

Lewis

Zelda

Ben

Abe

Sally

Joey

Mark

David

William

Bill

David

Linda

Sue

Tom

Zelda

Alfred

Waldo

Loenard

Fred

Betty

Iris

Dana

Joey

Sally

George

Joey

Mark

David

William

Bill

David

Linda

Adam

Lewis

Zelda

Ben

Abe

Sally

Joey

Mark

David

William

Zelda

Ben

Abe

Sally

Joey

Mark

David

William

Bill

David

Linda

Sue

Tom

Zelda

Sally

George

Joey

Mark

David

William

code--------------------------

#include

#include

#include

#include

#include

#include

int main(int argc, char *arv[])

{

int fd1[2]; //making file descriptor 1

int pid;

if (pipe(fd1) == -1)

{

fprintf(stderr, "fork error");

return 1;

}

pid = fork();

//printf("the pid for pipe parent is %d and the child pid is %d", getppid(), getpid());

if (pid < 0)

{

fprintf(stderr, "fork error");

return 1;

}

if (pid == 0)

{

dup2(fd1[1],1);

close(fd1[0]);

//printf("the child process running sort is %d ", getpid());

execlp("sort","sort", NULL);

printf("sort exec - should not be here");

exit(0);

}

wait(NULL);

int fd2[2];

pid = fork(); //second child

if (pipe(fd2) == -1)

{

fprintf(stderr, "fork error");

return 1;

}

if (pid < 0)

{

fprintf(stderr, "fork error ");

return 1;

}

if (pid == 0)

{

dup2(fd1[0],0);

dup2(fd2[1],1);

close(fd1[1]);

close(fd2[0]);

execlp("usr/bin/uniq", "uniq", NULL);

printf("uniq exec - you shouldnt be here");

exit(0);

}

wait(NULL);

pid = fork(); //3rd child process

if (pid < 0)

{

fprintf(stderr, "fork failed ");

return 1;

}

if (pid == 0)

{

dup2(fd2[0],0);

close(fd2[1]);

close(fd1[0]);

close(fd1[1]);

execlp("wc", "wc", "-l", NULL);

printf("wc exec - you shouldnt be here ");

exit(0);

}

//parent

close(fd1[0]);

close(fd1[1]);

close(fd2[0]);

close(fd2[1]);

wait(NULL);

//printf("CHILD COMPLETE ");

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions