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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:23:28+00:00 2026-06-18T11:23:28+00:00

I am new to C programming. I am trying to run a program given

  • 0

I am new to C programming. I am trying to run a program given by the path specified by the user using the fork(), exec(), and waitpid() commands. I have been trying to get this to run correctly for hours now and I keep getting errors I am not sure how to troubleshoot, as soon as I solve one error, new ones arise. I was wondering if someone can help me understand why my implementation does not work smoothly?

Many thanks

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(void)  {
    char command1[256], command2[256], path[556];
    printf("# ");
    scanf("%s", command1);
    scanf("%s", command2);
    scanf("%s", path);
    if(strcmp(command1,"quit")==0)
        exit(0);
    else if(strcmp(command1, "run")==0 && strcmp(command2, "command")==0){

            printf("%s", path);

            pid_t process;

            process = fork();

            //fork error
            if (process < 0){
               perror("fork");
               exit(0);
            }
            else if (process > 0){  //this is the parent process
               execl(path, "sh" , "-c", ":ls -l *.c", 0);
            }
            else {//this is the child process
               waitpid(process); //waits until the program terminates 
            }

    }




return 0;

}
  • 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-06-18T11:23:29+00:00Added an answer on June 18, 2026 at 11:23 am

    It looks to me like you have things swapped. With fork/exec, you generally do the exec in the child process, and the waitpid in the parent process.

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

Sidebar

Related Questions

I am very new to MPI programming, I am trying to run a program
i am new kernel programming.i have been trying to load this driver program for
I am new to Java and am trying to run a program using Eclipse.
I'm very new to programming and I am trying to write a program that
I'm new to programming with linux and I was trying to understand how fork()
I am new to android and java programming and I am trying to program
Very new to PHP/programming in general, and I've been trying to run a PHP
I am new to programming and Java and trying to write a program which
I'm new to Qt , and trying to compile and run this Qt program
I'm a new stackoverflow user! I'm writing because I have some problems programming Bluetooth

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.