Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3801128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:00:01+00:00 2026-05-19T14:00:01+00:00

Trying to implement a shell, mainly piping. I’ve written this test case which I

  • 0

Trying to implement a shell, mainly piping. I’ve written this test case which I expect to simply pipe ls to wc…it definitely doesn’t work as expected. It prints ls to the terminal then prints memory exhausted.
I’m very lost in how to fix this and get it to work. find_path works in all of my tests.

Edit – I have to use execv for the project, its a class thing, but I’ve tried it with execvp just in case and it does the exact same thing. Also this is just an example, a test to see why it does not work, I call fork twice once for both commands and waitpid because I have nothing else to do.

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
int find_path(char* execname, char** dst)
{        
        char *path = getenv("PATH");
        path = strdup(path);
        char *pos;
        path = strtok_r(path, ":", &pos);
        char *originalpath  = path;
        do
        {
                char* test = (char*)calloc(strlen(path) + strlen(execname) + 2, sizeof(char));
                test = strcpy(test, path);
                int testlen = strlen(test);
                (*(test+testlen)) = '/';
                strcpy(test + testlen + 1,execname);
                struct stat buf;
                int result = stat(test, &buf);
                if (result == 0)
                {
                        *dst = test;
                        free (originalpath);
                        return 1;
                }
                else
                {
                        free(test);
                }

        } while ((path = strtok_r(NULL, ":", &pos)) != NULL);
        free(originalpath);
        return 0;
}

int main()
{
    char *cmd1 = "ls";
    char *cmd2 = "wc";
    int filedes[2];
    pipe(filedes);
    char** argv = (char**)calloc(1, sizeof(char*)); 
    argv[0] = (char*)malloc(sizeof(char*));
    argv[0] = NULL;

    pid_t pid = fork();
    if (pid == 0)
    {
        char *path;
                find_path(cmd1, &path);
        dup2(filedes[1],stdout);

        execv(path,argv); 
    }
    pid = fork();
    if (pid == 0)
    {
        dup2(filedes[0], stdin);
        char *path;
        find_path(cmd2, &path);
        execv(path, argv);

    }
    else
        waitpid(pid);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:00:02+00:00Added an answer on May 19, 2026 at 2:00 pm

    Often when it is hard to debug a program, it is best to simplify it a little to eliminate sources of error. Here is your program, simplified to remove find_path as a source of errors:

    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    
    int main(void)
    {
        int filedes[2];
        pipe(filedes);
    
        /* Run LS. */
        pid_t pid = fork();
        if (pid == 0) {
            /* Set stdout to the input side of the pipe, and run 'ls'. */
            dup2(filedes[1], 1);
            char *argv[] = {"ls", NULL};
            execv("/bin/ls", argv);
        } else {
            /* Close the input side of the pipe, to prevent it staying open. */
            close(filedes[1]);
        }
    
        /* Run WC. */
        pid = fork();
        if (pid == 0) {
            dup2(filedes[0], 0);
            char *argv[] = {"wc", NULL};
            execv("/usr/bin/wc", argv);
        }
    
        /* Wait for WC to finish. */
        waitpid(pid);
    }
    

    This should behave as you expect.

    During simplification, a few errors came out:

    • argv[] wasn’t being setup correctly, in particular, argv[0] was being set to NULL;
    • The program was not closing the input side of the pipe that was being given to ls. When ls finished, the pipe wasn’t being closed (because the wc process still had it open), preventing wc from ever finishing.
    • The program was confusing the values stdout and stdin (which are of type FILE*) with the file descriptor numbers 0 and 1 (used by dup, pipe, etc.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a linux shell that supports piping. I have already
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
In Cocoa, I am trying to implement a button, which when the user clicks
I'm trying to write a shell in Ruby, and to implement tab completion I
I'm trying to use this excellent example to implement dropping virtual files into Windows
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
Am trying to implement a generic way for reading sections from a config file.
I am trying to implement string unescaping with Python regex and backreferences, and it
I'm trying to implement a data compression idea I've had, and since I'm imagining

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.