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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:06:26+00:00 2026-06-01T03:06:26+00:00

I have a situation like this: start(); <Some code> end(); I want start() function

  • 0

I have a situation like this:

start();
<Some code>
end();

I want start() function to launch a process (not separate threads) that does something asynchronously (by that I mean immediately after spawning the process, the control should return to parent and the spawned process going in ‘background’) with the parent process, and then after <Some code> block finishes, end() function would kill the process id associated with start().
I am not sure how to do this, especially the part to make parent and child code blocks asynchronous; need some help. Thanks.

EDIT: After getting help from the members, I was able to write this code, but this has problems, it would be great if someone could point out the errors. All I want for _do_() is to initiate a child process, which could never end if _stop_() is unable to kill it. But for some reason the parent process is getting lost and the program runs indefinitely on the while() loop.

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

pid_t id = 0;

int _do_()
{
//int status;
pid_t childpid; /* variable to store the child's pid */
FILE *fp= NULL;
childpid = fork();

//child stuff
if (childpid == 0) 
{
    id = getpid();
    fp = fopen ("Log.txt", "w+");
    while (1)
    {
        fprintf(fp, "Logging info...\n");
        fflush(fp);
    }
    fclose(fp);
}

if (childpid < 0) /* fork returns -1 on failure */
{
    perror("fork"); /* display error message */
    exit(-1); 
}

 return 0;

}

 //kill child process in _do_()
 int _stop_()
 {
  int en;
  printf("Going to kill child ID: %d\n", id);
  if (kill ( id , SIGKILL ) < 0)
  {
    en = errno;
    printf("Error while trying to KILL process: %s\n", strerror(en));
  }
  printf("Logging ended\n");

    return 0;

 }

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

   _do_();
   for (i = 0; i < 200; i++)
    ;
   _stop_();
   printf("\nEnded...\n");
   return 0;
}

EDIT2: I couldn’t do what I wanted, killing a process started at start() from end(), instead launched a daemon process and let it dump values on a file till the filesize reached to some pre-specified limit. This is the nearest to what I wanted.

  • 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-01T03:06:28+00:00Added an answer on June 1, 2026 at 3:06 am

    You want to find some samples regarding how fork/exec/kill work.

    normally you fork a process, which creates two processes: a child and a parent. The child returns from ‘fork’ with return code 0. The parent process returns pid of child – that’s how you know if you’re in child or parent.

    Now if you want to execute some other program, you can call ‘exec’ from child process. However you might as well have code like:

    pid = fork();
    if (pid == 0)
    {
      // do child stuff here
      exit (0);
    }
    if (pid == -1)
     // failed to fork, deal with it
    
    // parent code goes here
    ...
    ...
    kill(pid, SIGKILL); // you can kill your child process here if you want to.
                        // thanks to Adam Rosenfield for pointing out the signal number has to be sent
    

    It’s simple once you look at a tutorial on it. Windows works differently, so if you want to port your code to windows, use both fork and exec, as windows doesn’t actually create an identical copy of program – it actually always spawns a new program.

    I believe, don’t quote me, than windows programmers tend to use threads more than unix programmers – as creating a new process on windows is a big deal. On unix it’s not as big of a deal, but it’s bigger deal than a thread. But thread programming is so much harder, in comparison, you probably want to stay away from it unless you really really need it.

    edit:
    before doing ‘kill(pid, SIGKILL)’, you really want to make sure your child is still alive. If your child died, the pid might have gotten reused by another process, in which case you may terminate some random process.

    edit2:
    There’s a bug in your code.

    1 - remove 'int id...'
    2 - move pid_t childpid; from function into global scope
    3 - kill childpid, not 'id'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation. I have some javascript that looks like this: function onSubmit()
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation like this: user submits form with action='/pay' in '/pay' I have
I haven't been able to find anything for a situation like this. I have
Anybody have any ideas? The situation is like this: I have a primary rails
Hi this may seem like a weird question, but here's my situation: I have
Suppose we have a situation like this. Suppose instead of p = &global; we
In my code I have often situations like this: public void MyMethod(string data) {
Let's say I have a situation like so: $(window).load(function() { $(body).append(<div id='container'></div>); var i=1;
I'm new to NHibernate and Fluent NHibernate. Assuming I have a situation like the

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.