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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:23:24+00:00 2026-06-05T11:23:24+00:00

My question sounds the same as this but it isn’t: Start a process in

  • 0

My question sounds the same as this but it isn’t:

Start a process in the background in Linux with C

I know how to do fork() but not how to send a process to the background. My program should work like a simple command unix shell that supports pipes and background processes. I could do pipe and fork but I don’t know how to send a process to the background with & like the last line of the program:

~>./a.out uname
SunOS
^C
my:~>./a.out uname &

How to achieve the background process?

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define TIMEOUT (20)

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

  if(argc > 1 && strncmp(argv[1], "-help", strlen(argv[1])) == 0)
    {
      fprintf(stderr, "Usage: Prog [CommandLineArgs]\n\nRunSafe takes as arguments:\nthe program to be run (Prog) and its command line arguments (CommandLineArgs) (if any)\n\nRunSafe will execute Prog with its command line arguments and\nterminate it and any remaining childprocesses after %d seconds\n", TIMEOUT);
      exit(0);
    }

  if((pid = fork()) == 0)        /* Fork off child */
    {
      execvp(argv[1], argv+1);
      fprintf(stderr,"Failed to execute: %s\n",argv[1]);
      perror("Reason");
      kill(getppid(),SIGKILL);   /* kill waiting parent */
      exit(errno);               /* execvp failed, no child - exit immediately */
    }
  else if(pid != -1)
    {
      sleep(TIMEOUT);
      if(kill(0,0) == 0)         /* are there processes left? */
    {
      fprintf(stderr,"\Attempting to kill remaining (child) processes\n");
      kill(0, SIGKILL);      /* send SIGKILL to all child processes */
    }
    }
  else
    {
      fprintf(stderr,"Failed to fork off child process\n");
      perror("Reason");
    }
}

The solution in plain English appears to be here:
How do I exec() a process in the background in C?

Catch SIGCHLD and in the the handler, call wait().

Am I on the right track?

  • 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-05T11:23:25+00:00Added an answer on June 5, 2026 at 11:23 am

    Q: How do I send a process to the background?

    A: In general, exactly what you’re already doing: fork()/exec().

    Q: What’s not working as you expect?

    I suspect maybe you also want a “nohup” (to completely disassociate the child from the parent).

    The key to doing this is to run “setsid()” in the child process:

    • How to use fork() to daemonize a child process independant of it's parent?

    • http://www.enderunix.org/docs/eng/daemon.php

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

Sidebar

Related Questions

I know the question sounds silly, but consider this: I have an array of
Sorry if this question sounds a little silly, but I am not sure what
This sounds related to this question , but they aren't the same thing. That
I know this sounds like a dumb question, but it's to settle an argument
Hey everyone quick question, I know this sounds strange to do in javascript but
This question sounds outrageous, but VS is giving me an error when I check
this might question might sounds stupid, but I could'nt figure it out. How can
I know there is this question that is very similar, but I wanted to
This isn't exactly specifically a programming question (or is it?) but I was wondering:
I know that the question sounds like this is going to be an easy

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.