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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:38:26+00:00 2026-05-29T17:38:26+00:00

I am trying to create a program where it performs some task and exits.

  • 0

I am trying to create a program where it performs some task and exits. however, I want to exit from the program if the user enters some string on the terminal.

The way I thought to solve it:
Create a parent process where it is performing the main task. the main task has a loop.
create a child process where it is waiting for an input from the user. if the user inputs the exit string, the child process will update a variable and kills itself. The loop in the parent process will check for the value of the variable, if that value is changed by the child process, it will also exit. If the user doesn’t input the exit string, the parent process will complete the task and kills itself. at this moment I want to kill the child process also.
How can I do that?

Thanks

  • 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-29T17:38:27+00:00Added an answer on May 29, 2026 at 5:38 pm

    Thank you for saying what you are actually trying to accomplish here.

    You may find it much easier to create a single multithreaded process rather than create two single threaded processes.

    1. Thread #1 does work in a loop and periodically checks a variable exit_flag. If the variable is set, then it calls exit. If it finishes the work, it calls exit.

    2. Thread #2 waits for input from the user. If it gets input, it sets the variable exit_flag.

    This way, when you call exit, both threads will automatically exit. You will either need to use a mutex/semaphore or another kind of synchronization, e.g., if you have libatomic-ops:

    #include "atomic_ops.h"
    
    AO_t exit_flag;
    
    // In thread #2
    AO_store(&exit_flag, 1);
    
    // In thread #1
    while (!AO_load(&exit_flag)) {
        ...
    }
    

    Warning about the above code: Setting a global flag only works this way because the main thread only reads the global flag and nothing else. If you need to send more than one word between threads you’ll need some kind of synchronization mechanism, e.g., a mutex, semaphore, or AO_store_release paired with AO_store_acquire.

    If you use processes instead of threads: Since processes don’t share mutable memory by default, it is not as easy as “updating a variable”. Each process has its own set of private variables. You would need to set up a region of shared memory or communicate some other way (e.g., signal, pipe, socket) which is extra work for you.

    Full example

    Here is a quick full example. The main thread does “work” that involves counting down from 5. When it reaches 0, it exits and prints a message. The second thread reads user input, when it reads a line starting with 'e' it sets a flag which causes the main thread to exit.

    The second thread is created as a detached thread only because we do not need to join with it. It does not affect the correctness of this particular program.

    If you call exit, all threads in your program should be terminated, unless there is something wrong with your platform.

    Note: This example requires libatomic-ops and assumes you are using POSIX threads.

    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    #include <atomic_ops.h>
    #include <errno.h>
    
    static AO_t exit_flag;
    
    #define fail(x) fail_func(x, __LINE__)
    
    static void fail_func(int code, int line)
    {
        fprintf(stderr, "line %d: failed with code %d\n", line, code);
        abort();
    }
    
    static void *input_thread(void *param)
    {
        char buf[16], *p;
        (void) param;
        while (1) {
            p = fgets(buf, sizeof(buf), stdin);
            if (!p)
                break;
            if (buf[0] == 'e')
                break;
            else
                puts("unknown command");
        }
        AO_store(&exit_flag, 1);
        return NULL;
    }
    
    int main(int argc, char *argv[])
    {
        pthread_attr_t attr;
        pthread_t thread2;
        int r, c;
        struct timespec t;
        t.tv_sec = 1;
        t.tv_nsec = 0;
        r = pthread_attr_init(&attr);
        if (r) fail(r);
        // Note: making the thread detached is not actually necessary.
        r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        if (r) fail(r);
        r = pthread_create(&thread2, &attr, input_thread, NULL);
        if (r) fail(r);
        c = 5;
        while (1) {
            if (AO_load(&exit_flag)) {
                puts("thread 1 exiting (canceled)");
                exit(1);
            }
            printf("counter = %d\n", c);
            if (c == 0) {
                puts("thread 1 exiting (finished)");
                exit(0);
            }
            c--;
            r = nanosleep(&t, NULL);
            if (r) fail(errno);
        }
        return 0;
    }
    

    Sample output (user input is in bold):

    $ ./a.out
    counter = 5
    counter = 4
    counter = 3
    counter = 2
    counter = 1
    counter = 0
    thread 1 exiting (finished)
    $ ./a.out
    counter = 5
    counter = 4
    exit
    thread 1 exiting (canceled)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a program that will do some simple calculations, but
I'm trying to create a python program (using pyUNO ) to make some changes
I am trying to create a program. There is also a function I want
I was trying to create simple program that would perform some trivial operation with
I am trying to create a program that reads multiple choice answers from a
I am trying to create a program to copy all the files from one
I am trying to create a program to perform a simple task and produce
I'm trying to create a program that takes a text file of c++ code
I am trying to create a program similar to Folder Lock which prevents users
I'm currently trying to create a program that estimates location based on signal strength.

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.