is it possible in c program to run two processes at the same time.For example i want to run a timer along with my application .The timer must be shown on the screen.Thanks in advance

Dani AI

Generated

asked for a quiz program with a visible countdown while the user types; pointed toward threading. Two practical approaches work well:

  • Use a separate timer thread that updates a shared remaining-time value and redraws only the timer area. This keeps timing precise even while the main thread blocks for input. Protect shared state with an atomic or a mutex, and avoid simultaneous raw writes that scramble the prompt—use a small UI library (ncurses on Unix, Console APIs on Windows) or carefully coordinate screen writes.
  • Or use a single-thread event loop: make stdin nonblocking or use select/poll with a timeout. On each timeout tick, decrement the timer and redraw; when input arrives, process it. This avoids threads but needs terminal-mode handling if line editing is wanted.

A minimal POSIX-style timer-thread pattern (conceptual):

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdatomic.h>
#include <unistd.h>

atomic_int remaining;

void *timer_thread(void *arg) {
    int total = *(int*)arg;
    free(arg);
    for (int i = total; i >= 0; --i) {
        atomic_store(&remaining, i);
        printf("\rTime left: %2d s  ", i);
        fflush(stdout);
        sleep(1);
    }
    return NULL;
}

/* main creates the thread, prints the question, reads answer (fgets),
   then joins the timer thread. Compile with: gcc -std=c11 -pthread quiz.c -o quiz */

Cautions and tips:

  • Passing a pointer to a stack variable into a thread can be unsafe; allocate the argument or copy it inside the thread.
  • Stdio output from two threads can interleave; use a mutex or a proper UI library to avoid garbled lines.
  • On Windows, use CreateThread/_beginthreadex and Sleep; console handling differs from Unix.
  • If the quiz must stop when time runs out, set a shared flag the timer thread writes and the input-handling code checks.

This pattern lets a visible countdown run concurrently with user input while keeping the design simple and predictable.

Recommended Answers

All 3 Replies

is it possible in c program to run two processes at the same time.For example i want to run a timer along with my application .The timer must be shown on the screen.Thanks in advance

The short answers is yes with clarification as to what you mean by "same time". Do you mean multi-tasking where the processor appears to be running multiple processes at the same time or do you mean true "same time" where a multi-core processor is required?

With that divergence out of the way check out threads, they allow a single process(or single thread process) to spawn additional processes(or threads/light weight processes).

So yes its possible for a C program to run/spawn more than one process...

Here's a link for POSIX threads

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

What i would like to do is,i want to run a quiz program where the users can see a countdown timer attached to the screen and at the same time should answer my questions.Can you guide me how to do this.Thank tou in advance

What i would like to do is,i want to run a quiz program where the users can see a countdown timer attached to the screen and at the same time should answer my questions.Can you guide me how to do this.Thank tou in advance

This sounds like a candidate for threading. The best thing to do is Google a few thread tutorials, these with some experimenting will determine if threads will work for your program....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.