Writing c code for who | sort | lp


 
Thread Tools Search this Thread
Top Forums Programming Writing c code for who | sort | lp
# 8  
Old 01-29-2003
To redirect as in "2 > log.dat", you close() fd 2 and then open() log.dat prior to the exec().
# 9  
Old 05-23-2007
Hi Perderabo

I have got a similar problem, hope you can help Smilie

I want to execute sort | uniq | wc.

So, on the command line it would be:

sort file1 | uniq -c | wc -l

But if for some reason sort fails (for e.g: if the /tmp fir is full) the above command returns 0 as the answer. I am not able to catch the status of each individual command in the pipe.


So the code I had before is:

Code:
FILE *fptr;

fptr = popen("sort file1 | uniq -c | wc -l" , "r");

if (fptr != NULL) {
     fscanf(fptr, "%lu", &uniqLines);
} else {
    //ERROR
}


What I want is, if sort fails, I should be able to catch it.

Therefore I had to implement pipe, to get the status of each individual calls.

So now, based on your program above, I have the following code:

Code:
#include <unistd.h>

int main()
{
        int pipe1[2]; /* pipe for:  sort ------> uniq */
        int pipe2[2]; /* pipe for:  uniq ------> wc   */

        pipe(pipe1);

        if(!fork()) {
                close(1);
                dup(pipe1[1]);
                close(pipe1[0]);
                close(pipe1[1]);
                execlp("sort","sort","work.fp",NULL);
                /* sort failed */
                printf("\n sort failed");

        } else {
                
                pipe(pipe2);

                if (!fork()) {
                        close(0);
                        dup(pipe1[0]);
                        close(1);
                        dup(pipe2[1]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execlp("uniq","uniq","-c",NULL);
                } else {
                        close(0);
                        dup(pipe2[0]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execlp("wc","wc","-l",NULL);
                }
        }

}

This works great !!!

However, what I want is to capture the output of sort | uniq | wc in a variable (like I did before) and not print it on stdout.

Code:
fscanf(fptr, "%lu", &uniqLines);

What to do ???
# 10  
Old 05-23-2007
Quote:
Originally Posted by the_learner
What to do ???
Open a pipe for the last process's stdout and read from it.
# 11  
Old 05-24-2007
Thanks porter..

Does this mean I will need another child-parent processes using fork().. that means another level of if-else ??

Do you mean something like:

Code:
#include <unistd.h>

int main()
{
        int pipe1[2]; /* pipe for:  sort ------> uniq */
        int pipe2[2]; /* pipe for:  uniq ------> wc   */
        int pipe3[2];

        pipe(pipe1);

        if(!fork()) {
                close(1);
                dup(pipe1[1]);
                close(pipe1[0]);
                close(pipe1[1]);
                execlp("sort","sort","work.fp",NULL);
                /* sort failed */
                printf("\n sort failed");

        } else {
                
                pipe(pipe2);

                if (!fork()) {
                        close(0);
                        dup(pipe1[0]);
                        close(1);
                        dup(pipe2[1]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execlp("uniq","uniq","-c",NULL);
                } else {
                        
                        pipe(pipe3);
                        if (!fork()) {
                                 close(0);
                                 dup(pipe2[0]);
                                 close(1);
                                 dup(pipe3[1]);                                
                                 close(pipe1[0]);
                                 close(pipe1[1]);
                                 close(pipe2[0]);
                                 close(pipe2[1]);
                                 execlp("wc","wc","-l",NULL);
                         } else {
                                 //read from pipe 3
                        }
                }
        }

}

# 12  
Old 06-25-2007
Hi, it seems that a few posts got deleted..

anyway,

I think the following code works to get the status of sort (thats what I wanted)

I call waitpid() to get the status of sort. And then WEXITSTATUS(status) is non-zero if sort had failed.

But As you can see..the code is just too big.. it has lots of if-else conditional statements and too many nestings.. it is getting very complicated. Can someone please help me out here. Porter suggested that I should Use functions to encapsulate. Porter, can you please tell me which piece of code here should I encapsulate. Thanks in advance. !!

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
        int pipe1[2]; /* pipe for:  sort ------> uniq */
        int pipe2[2]; /* pipe for:  uniq ------> wc   */
        int pipe3[2]; /* pipe for:  read result from wc */

        u_long uniqLines;
        pid_t pid;
        int   status;

        pipe(pipe1);

        pid = fork();

        if(pid == 0) {

                /*
                 * Child Process - sort
                 */

                close(1);
                dup(pipe1[1]);
                close(pipe1[0]);
                close(pipe1[1]);
                execlp("sort","sort","work.fp.big",NULL);

        } else {
                /*
                 * Parent Process
                 */

                pipe(pipe2);

                if (!fork()) {

                        /*
                         *     uniq
                         */       
                        close(0);
                        dup(pipe1[0]);
                        close(1);
                        dup(pipe2[1]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execlp("uniq","uniq","-c",NULL);

                } else {

                        pipe(pipe3);

                        if (!fork()) {

                               /*
                                 *      wc
                                 */

                                close(0);
                                dup(pipe2[0]);
                                close(1);
                                dup(pipe3[1]);
                                close(pipe1[0]);
                                close(pipe1[1]);
                                close(pipe2[0]);
                                close(pipe2[1]);
                                close(pipe3[0]);
                                close(pipe3[1]);
                                execlp("wc","wc","-l",NULL);

                        } else {
                                close(0);
                                dup(pipe3[0]);
                                close(pipe1[0]);
                                close(pipe1[1]);
                                close(pipe2[0]);
                                close(pipe2[1]);
                                close(pipe3[0]);
                                close(pipe3[1]);
                                scanf("%lu",&uniqLines);
                        }

                }

                       /*
                        * Get status of sort
                        */

                        if (waitpid (pid, &status, 0) != pid) {
                                perror("waitpid");
                                fprintf(stderr, "\nstatus not available\n");
                        } else {
                                fprintf(stderr, "\n status = %d\n", status);
                        }

                        if ( WEXITSTATUS(status) == 0 ) {
                                  printf("Sort Success \n");
                        } else {
                                  printf("Sort failed \n");
                        }
                
        }


}

# 13  
Old 06-25-2007
Quote:
Originally Posted by the_learner
Porter, can you please tell me which piece of code here should I encapsulate. Thanks in advance. !!
I will leave that as an exercise for you but consider the following:

General:

1. If you have lot's of lines that all look similar, then try and make those a function.

2. Use structures to hold related information rather than lot's of individual variables.

3. Extract pipe file descriptor pairs from their arrays and put them in structure elements that reflect their purpose.

Specific case:

1. rather than having lot's of "close" in the child fork, you could mark each file descriptor to be closed as "close-on-exec" using either

Code:
fcntl(fd,F_SETFD,FD_CLOEXEC);

or
Code:
int b=1;
ioctl(fd,FIOCLEX,(char *)&b);

# 14  
Old 06-26-2007
Quote:
Originally Posted by porter
1. If you have lot's of lines that all look similar, then try and make those a function.
Do you mean I can put all the close() calls in a function ? Something like this..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int pipe1[2]; /* pipe for:  sort ------> uniq */
int pipe2[2]; /* pipe for:  uniq ------> wc   */
int pipe3[2]; /* pipe for:  read result from wc */

void
close_des()
{
        close(pipe1[0]);
        close(pipe1[1]);
        close(pipe2[0]);
        close(pipe2[1]);
        close(pipe3[0]);
        close(pipe3[1]);
}

int main()
{
        u_long uniqLines;
        pid_t pid;
        int   status;

        pipe(pipe1);

        pid = fork();

        if(pid == 0) {
                /*
                 * Child Process - sort
                 */

                dup2(pipe1[1],1);
                close_des();
                execlp("sort","sort","work.fp",NULL);

        } else {
                /*
                 * Parent Process
                 */

                pipe(pipe2);

                if (!fork()) {

                        /*
                         *     uniq
                         */
                        dup2(pipe1[0],0);
                        dup2(pipe2[1],1);
                        close_des();
                        execlp("uniq","uniq","-c",NULL);

                } else {
                        pipe(pipe3);
                        if (!fork()) {
                                dup2(pipe2[0],0);
                                dup2(pipe3[1],1);
                                close_des();                                
                                 execlp("wc","wc","-l",NULL);
                        } else {
                                dup2(pipe3[0],0);
                                close_des();
                                scanf("%lu",&uniqLines);
                        }
                }


  ..... and so on ....

}


Quote:
Originally Posted by porter
1. rather than having lot's of "close" in the child fork, you could mark each file descriptor to be closed as "close-on-exec" using either

fcntl(fd,F_SETFD,FD_CLOEXEC);
Do you mean something like this...

Code:
int main()
{
        int pipe1[2]; /* pipe for:  sort ------> uniq */
        int pipe2[2]; /* pipe for:  uniq ------> wc   */
        int pipe3[2]; /* pipe for:  read result from wc */

        u_long uniqLines;
        pid_t pid;
        int   status;

        pipe(pipe1);
        fcntl(pipe1[0],F_SETFD,FD_CLOEXEC);
        fcntl(pipe1[1],F_SETFD,FD_CLOEXEC);
        pid = fork();

        if(pid == 0) {
                /*
                 * Child Process - sort
                 */

                dup2(pipe1[1],1);
                execlp("sort","sort","work.fp",NULL);

        } else {
                /*
                 * Parent Process
                 */

                pipe(pipe2);
                fcntl(pipe2[0],F_SETFD,FD_CLOEXEC);
                fcntl(pipe2[1],F_SETFD,FD_CLOEXEC);
                if (!fork()) {

                        /*
                         *     uniq
                         */
                        dup1(pipe1[0],0);
                        close(1);
                        dup(pipe2[1]);
                        execlp("uniq","uniq","-c",NULL);

                } else {

                        pipe(pipe3);
                        fcntl(pipe3[0],F_SETFD,FD_CLOEXEC);
                        fcntl(pipe3[1],F_SETFD,FD_CLOEXEC);
                        if (!fork()) {

                                /*
                                 *      wc
                                 */

                                dup2(pipe2[0],0);
                                dup(pipe3[1],1);
                                execlp("wc","wc","-l",NULL);

                        } else {
                                dup2(pipe3[0],0);
                                fprintf(stderr, "\n reached after wc");
                                scanf("%lu",&uniqLines);
                        }

                }



In both the occasion..the code seems to hang.. I dont know why..

But for some cases, if I leave some of the close statements in there.. it seems to work..

I really dont understand this pipe() and fork() and dup() and fnctl()... I think I need to buy that book which Perderabo suggested.. Advanced Unix Programming..

But I need to get this done urgently Smilie Porter please help...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help writing code

Hi, i need a string that take from a log file just two words (ngwan0) and his related ip address and then execute 2 commands in a shell. Any ideas where to start? Thanks (3 Replies)
Discussion started by: Board27
3 Replies

2. Shell Programming and Scripting

Writing a UNIX script from LOG to provide return code.

Folks - Firstly, I do apologize that my first post here is a question. I am quite familiar with UNIX since our application is running on it. We are trying to automate a few things on our end and I am challenged with a task in hand that requires UNIX scripting. I am totally a newbie in UNIX... (4 Replies)
Discussion started by: sk72
4 Replies

3. Shell Programming and Scripting

Sort error code 512

Hi Friends, I am back with small issue which i tried thinking but could not get much!!!! issue here is..i m using kind of extraction query using perl code in that i do sort with few number of csv files. each of them will be around 50 to 100 M size. sometime i get following error message... (1 Reply)
Discussion started by: Shahul
1 Replies

4. UNIX for Dummies Questions & Answers

sed remove two headers; writing more elegant code

Hi there, I have two questions. First, I was wondering how to use sed to remove two header lines or two tail lines. Here I just do the same operation twice...I'm sure there is a better way. Second, and more importantly, is there a better way to have these operations use files other than... (5 Replies)
Discussion started by: mikey11415
5 Replies

5. Programming

Vi question for writing source code

Hi guys, I'm modifying an old f77 code using vi. I'm modifying the code remotely from my windows machine using xming using vi. I'm using tabs to move past the first 6 columns of the code and to keep my loops and if statements neat, but when I hit the tab key, vi displays a big red block which is... (7 Replies)
Discussion started by: rks171
7 Replies

6. Homework & Coursework Questions

Writing Code

1. The problem statement, all variables and given/known data: Write a script that will perform the grep command on a user defined file and a user defined pattern 2. Relevant commands, code, scripts, algorithms: must use grep command (that is the only piece of information that was given) ... (2 Replies)
Discussion started by: akjim101
2 Replies

7. Shell Programming and Scripting

script or piece of code where the data returned by a stored procedure you are writing

hi fndz. Can you please help me with the code if I call a stored procedure from my shell script and stored procedure returns a cursor, cursor output should be saved to a file (3 Replies)
Discussion started by: enigma_83
3 Replies

8. Shell Programming and Scripting

Help in writing the code

hi i am studying unix with c language ,and completely new to this .I have question about how to pass the control from one program file to another.My first program has the main process and it should go into the second program where i can create six processes with also the usage of pipes to... (3 Replies)
Discussion started by: soumya_v7
3 Replies

9. Shell Programming and Scripting

writing code!!!

suppose by writing this code "$ /sbin/ifconfig eth0| grep 'inet addr:' " i've got this output "inet addr:192.168.2.1 Mask:255.255.255.0" now if i want to see the output like this "IP address: 192.168.2.1 Subnet Mask: 255.255.255.0" what is the code should be? can anyone... (3 Replies)
Discussion started by: shelly
3 Replies

10. Shell Programming and Scripting

conditional writing of sql code

Hello again... I have a request from another department to list for them all the columns and tables we use in this certain database. I have spooled the oracle stored procedured into 1 file. I need a way to write out parts of that file. The criteria is to to start the block to be written when... (0 Replies)
Discussion started by: kburrows
0 Replies
Login or Register to Ask a Question