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

  • SEARCH
  • Home
  • 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 6556257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:55:18+00:00 2026-05-25T12:55:18+00:00

I am wondering how I can pass a file descriptor through the execve() command

  • 0

I am wondering how I can pass a file descriptor through the execve() command and then access it on the other side. I know that I can use dup2 to redirect the file-descriptor but I cannot do that. I am required to actually pass the file descriptor to the child and use it in the child.

What I have done so far:

Parent makes pipe + args like the following:

int pfd[2];
if(pipe(pfd) == -1)
    exitWithError("PIPE FAILED", 1);
char *args_1[] = {"reader", argv[1], (char*) pfd, (char *) 0};

Then the child calls execve after fork() like the following:

close(pfd[1]);
execve("./reader", args_1, NULL);

Then, in the reader program I try to access pipe descriptor that was passed:

int main(int argc, char* argv[]){
    ...
    write(argv[2][1], buf, read_test);

argv[2] should be referencing the pipe descriptor, then the argv[1] should go to the write end of the pipe. I am almost positive that I need to do some type-casting differently here, but everything I try doesn’t quite work out.

NOTE: I have a working version of this program using dup2 to redirect to stdin and stdout for the children, but I have to actually pass the pipe descriptor to a child as per instructions of the project.

Any help is appreciated.

  • 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-25T12:55:18+00:00Added an answer on May 25, 2026 at 12:55 pm

    Simply casting the file descriptor to a char* isn’t a good idea. It could cause data loss if the OS chooses to copy the strings and stops at the 0 bytes, so the data isn’t entirely copied. It would be safer to create a string containing the file descriptor.

    int pfd[2];
    if(pipe(pfd) == -1)
        exitWithError("PIPE FAILED", 1);
    char string1[12]; // A 32 bit number can't take more than 11 characters, + a terminating 0
    snprintf(string1,12,"%i",pfd[1]); // Copy the file descriptor into a string
    char *args_1[] = {"reader", argv[1], &string1[0], (char *) 0};
    

    The reader program then uses atoi to convert this back into an int.

    int fd = atoi(argv[2]);
    write(fd,buf,read_test);
    

    If you really want to use a cast, then you need to cast argv[2] as int* in your reader.

    write(((int*)argv[2])[1],buf,read_test);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how I can pass either an ArrayList, List <int > or
I'm wondering how can I put a header into my PDF file, cause I've
I noticed a lot of Android functions have a parameter that you can pass
I'm wondering if it is possible to pass form data from index.jsp file to
I am wondering if there is a way I can pass an updated PHP
I was wondering how can I set a timeout on a socket_read call? The
I'm wondering how can I submit a form via Ajax (using prototype framework) and
I was wondering how can I list the user tables name with its primary
I am wondering how can I hide/remove a tag after a certain amount of
I was wondering how can I validate a single checkbox using PHP and MySQL.

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.