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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:35:32+00:00 2026-05-16T03:35:32+00:00

I am reading GNU/Linux application programming the 2nd edition ,you can reach what am

  • 0

I am reading GNU/Linux application programming the 2nd edition,you can reach what am reading from here.After I write the code similar to his,but it work strangely:

$ ./shell
./shell>>quit
$ ./shell
./shell>>date
Sun Aug  8 21:19:37 CST 2010
./shell>>quit
$ ./shell
./shell>>abc
execlp failed: No such file or directory
./shell>>quit
./shell>>quit
$./shell 
./shell>>abcd execlp
execlp failed: No such file or directory
./shell>>quit 
./shell>>quit

The first and second cases are ok,but the third and forth ones somewhat need two quit to quit.This is not what i am expecting.I guess something is wrong with fork(),or the waitpid(),but this still got unsolved after asking a few people around me.Now is summer time, i.e. summer holiday,I got no more mates to go for.Thanks always.

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

#define LINE_LEN 80

int main(int argc,char* argv[])
{

 pid_t pid;
 char cmd[LINE_LEN+1]={'\0'};

 while(1)
 {
  printf("%s>>",argv[0]);
  if(fgets(cmd,sizeof(cmd),stdin)==NULL)
  {
   perror("fgets failed");
   break;
  }
  cmd[strlen(cmd)-1]='\0';
  if(strncmp(cmd,"quit",4)==0)
  {
   break;
  }
  if((pid=fork())==-1)
  {
   perror("fork failed");
   break;
  }else if(pid==0)
  {
   //TODO no option can be specified for cmd
   execlp(cmd,cmd,NULL);
   perror("execlp failed");
  }else
  {
   waitpid(pid,NULL,0);
  }
 }
 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-05-16T03:35:33+00:00Added an answer on May 16, 2026 at 3:35 am

    Normally, if execlp succeeds, your child process becomes the new process, and thus execution of your code effectively stops at that point.

    Now the execlp failed, you print out the error, and continue the child process as if nothing happened! You should exit(0) the child, if the execlp failed.

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

Sidebar

Ask A Question

Stats

  • Questions 544k
  • Answers 544k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the case of NoClassDefFound exceptions, double check your runtime… May 17, 2026 at 6:46 am
  • Editorial Team
    Editorial Team added an answer Firstly, Java and JavaScript are completely different and unrelated languages.… May 17, 2026 at 6:46 am
  • Editorial Team
    Editorial Team added an answer cat ~/.ssh/id_rsa.pub or cat ~/.ssh/id_dsa.pub You can list all the… May 17, 2026 at 6:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am reading chapter 2 of Advanced Linux Programming: http://www.advancedlinuxprogramming.com/alp-folder/alp-ch02-writing-good-gnu-linux-software.pdf In the section 2.1.3
I am reading the book from Andrei Alexandrescu about the D programming language. He's
Reading this question I found this as (note the quotation marks) code to solve
Reading source code of sample projects, such as Beast and Bort, are recommended as
reading excel files from C# working well in 32 bit version server. It is
I usually use Geany or Hi-Tide under Debian (GNU/Linux) for firmware development, mainly C
i'm developing a video server in C on GNU/Linux, and i'm using ffmpeg to
I have a faulty hard drive that works intermittently. After cold booting, I can
I've got an issue with inserting/reading utf8 content from a db. All verifications I'm
After reading this thread What does the explicit keyword mean in C++? I made

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.