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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:25:13+00:00 2026-06-15T06:25:13+00:00

I’d really love your help with understanding why doesn’t the process reach the son

  • 0

I’d really love your help with understanding why doesn’t the process reach the “son process” after using fork() command. I’m trying to write a program that runs another program, but It seems that the program dosen’t even reach the son process. I can tell that since “son process” is not being printed to the screen, and I really wonder why.

Here’s a sketch of the code- I can’t even check if it is alright since as I said, it doesn’t even reaching the son process, I always get “son exited with error”.

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>



#define MAXARGV 5;

int main() {
    char* cmd;

    int child_status;

    char* s;
    char** argv;
    int counter;

    cmd= (char*) calloc( 5, sizeof(char)*20);
    s=(char*) calloc(1,sizeof(char)*20);
    argv=(char**) calloc(5, sizeof(char*)*20);




    printf("Please write a command\n");

    gets(cmd);

    counter = 0;

    while (strcmp(cmd, "exit") != 0) {

        int pid = fork();


        if (pid == 0) {
            printf("son process");

            while (sscanf(cmd, "%s", s) == 1) {

                strcpy(argv[counter], s);
                counter++;
            }

            execv(argv[0], argv);

            printf("the command is not legal");
            assert(0);

        }

        else {

            if (wait(&child_status) == -1) {
                printf("error waiting for pid=%d\n", pid);
                exit(-1);
            }

              if(WIFEXITED(child_status)!=0)
                    printf("son status=%d\n", WEXITSTATUS(child_status));
                else
                    printf("son exited with error\n");

        }

        printf("Please write a command");

        gets(cmd);

    }

    free(s);
    free(cmd);
    free(argv);
    printf("here as well");
    return 1;
}
  • 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-15T06:25:15+00:00Added an answer on June 15, 2026 at 6:25 am
    1. The program reaches the printf("son process") just fine, but that just puts the string in a buffer inside the process and since you didn’t fflush() it, it doesn’t make it to the screen and is discarded with the rest of the process’ memory in the exec call. Note, that stdout is normally line-buffered, so if you had newline there, it would auto-flush. Also stderr is by default unbuffered and more suitable for debug prints (fprintf(stderr, "child process")).
    2. You are trying to assemble the command read from standard input in argv, but it only has memory for the actual arguments given to you, so you overrun this memory and get segmentation fault.
    3. if WIFEXITED gives zero, you should use WIFSIGNALED and WTERMSIG to confirm that the error is indeed SIGSEGV.
    4. assert(0) is not a good way to terminate process after error. exit(1) is. Assertions are only for conditions that indicate bug in the code itself if they happen and are often eliminated (by defining NDEBUG) from production code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.