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 1086859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:51:54+00:00 2026-05-16T22:51:54+00:00

I am writing in my grammar, in LEX, some code to fork() my process

  • 0

I am writing in my grammar, in LEX, some code to fork() my process and run a child. The child actually gets some input from the parent, and then returns a result.

I need to call exec on the same binary that loaded the parent, but there I am having an issue. I know that exec does not mean complete sense, but I do this because I have some previous grammar in LEX that I simply want to get rid off, so reloading the process is easier.

I have the following code in the children, after the fork():

char *path = strdup(getenv("PWD"));
size_t size = strlen(path) + strlen("/shell") + 1;
path = (char *) realloc(path, sizeof(char) * size);
path = strcat(path, "/shell");

// call exec
execl(path, NULL);

The issue with this code, is that it works if the process is launched from the same directory, but if I try to load from a folder within this directory, like say, ../shell, then the path is actually wrong, it will include this directory.

I would like to please know how I could get the correct path of the process, and if there is a way to also get the process actual name please? I have looked at the environments variable but have not found anything useful.

Thank you very much,

Jary

  • 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-16T22:51:55+00:00Added an answer on May 16, 2026 at 10:51 pm

    The objective, as I understand it, it to have the child process re-execute the same program that represents the parent process.

    In the absence of tricks using data from the /proc file system, I don’t believe there is a completely reliable way to do it.

    Normally, you rely on the value of argv[0] being sufficient to find the program, and use execvp() to find the program via $PATH.

    Attempt 2 – maybe less confusing

    I will rephrase [the question] if you don’t mind: Right now, execl() does execute the binary, but only if I load the parent directly from its folder. If I go in a subfolder, let’s call it fd, then the path will be “correctPath/fd/shell” rather than “correctPath/shell”. The issue seems to be that calling getenv(“PWD”) to find the path is not always right.

    So the goal is to load the same process, or the binary; let’s assume the binary is called “shell”. The issue is to find the path. The code I am showing works in the case that the shell is loaded (parent) from the folder it is in itself, otherwise it does not work. I suspect calling getenv(“PWD”) is not right, but I am not sure what else to call.

    You are correct that using getenv("PWD") or getcwd() is not usually correct.

    char *arg0 = 0;
    
    int main(int argc, char **argv)
    {
        ...declarations...
    
        arg0 = argv[0];
    
        ...actions...
    }
    

    The main program, therefore, stashes the value of its argv[0] in the global variable arg0 to make it available to other parts of the process – in particular, the code that is going to (re)run the command.

    If the program is invoked in the same directory as where the executable resides using “./shell”, then argv[0] (and hence arg0) will contain that pathname. If it is executed using “shell” relying on $PATH to locate the program, then argv[0] will contain either just “shell” or the absolute pathname of “shell” (less common).

    If the program is invoked from the fd subdirectory, it might be invoked as “../shell”, or it might be invoked as “/absolute/path/to/shell” or it might be invoked as “shell” relying on $PATH to find the executable. Again, in any of these cases, the value in arg0 is the name by which the program was originally invoked, or is equivalent to it.

    The only time this fails is if someone is deliberately setting out to confuse the executable, which is (fortunately) seldom the case.

    So, in the child code, you can use:

    char *args[] = { arg0, 0 };
    execvp(arg0, args);
    

    to re-execute the command as it was originally executed (apart from any auxilliary arguments that were passed originally).

    Attempt 1 – somewhat confusing

    Assuming that the value of argv[0] is available via a variable char *arg0, all that’s necessary is:

    char *args[] = { arg0, 0 };
    execvp(arg0, args);
    

    If you must use execl(), then you need:

    execl(arg0, arg0, (char *)0);
    

    The cast is necessary; execl() is a variable argument list function, and if you write 0, it will be converted to int which will fail on a 64-bit system where int is 32-bits and a pointer is 64-bits. However, that will fail if arg0 does not represent the path (relative or absolute) to the executable. You’d then have to decide what to do if execl() returns – you can either give up or search for the program via $PATH, but in that case, why not use execvp() in the first place to save yourself the pain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing some docs with code snippets which I want to be copyable to run
I'm writing a grammar in YACC (actually Bison), and I'm having a shift/reduce problem.
Writing some test scripts in IronPython, I want to verify whether a window is
Writing the code for the user authentication portion of a web site (including account
I writing a report in Visual Studio that takes a user input parameter and
When writing production-quality VC++ code, is the use of recursion acceptable? Why or why
As a purely academic exercise, I'm writing a recursive descent parser from scratch --
I need a little guidance in writing a grammar to parse the log file
Writing my first JQTouch app. When I go from #login to #home , a
After writing and reading an xml string to and from a stream, it ceases

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.