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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:09:30+00:00 2026-06-18T02:09:30+00:00

My professor shows the following example in C: #include <sys/types.h> #include <stdio.h> #include <unistd.h>

  • 0

My professor shows the following example in C:

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main() {
   pid_t pid;

   /* fork another process */
   pid = fork();
   if (pid < 0) { /* error occurred */
         fprintf(stderr, "Fork Failed");
         return 1;
   }
   else if (pid == 0) { /* child process */
         execlp("/bin/ls", "ls", NULL);
   }
   else { /* parent process */
        /* parent will wait for the child */
        wait (NULL);
        printf("Child Completed its execution\n");
   }

return 0;
}

I compiled it and ran it. I saw a strange behavior in this code:

The result of ‘ls’ program/command is printed which is in the else if condition but also the string “Child Completed its execution\n” printed too which is in else.

Isn’t this a strange behavior?

  • 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-18T02:09:32+00:00Added an answer on June 18, 2026 at 2:09 am

    No, you forked it. There are two processes running. One reported the ls and the other reported the printf().

    Specifically, the child/forked process executed /bin/ls and the parent called printf(), the output you see.

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

Sidebar

Related Questions

Consider the following x86 code example: #include <stdlib.h> static int i; static inline __attribute__((always_inline))
My professor gave the following code to show an example of inheritance: //Base class
CPU-Z is able to determine the instructions your processor supports. For example the following
I have the following code public static int unknown(String x) { if ((x.length()==1) &&
When I open the tester for my SOAP webservice it shows the following error
I have a template class defined as: #include <stdio.h> #include <queue> using namespace std;
the following code doesnot shows the version information correctly. ManagementObjectSearcher mos = new ManagementObjectSearcher(new
My professor provided me with a file called CursorList.cpp that implements a Cursor Linked
I'm presenting a problem my professor showed in class, with my O(n*log(n)) solution: Given
I have two tables Department Professor in which Department has an attribute called HeadID

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.