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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:02:08+00:00 2026-05-30T19:02:08+00:00

I am implementig simple shell emulator in C. It should support running command in

  • 0

I am implementig simple shell emulator in C. It should support running command in background (ex. sleep 5s &). So I run this command using fork() -> exec() sequence and wait for finish of that command using SIGCHLD signal handler. My question is, if there is any possibility to get process command (name), that was specified in exec() call. I give you example:

//SIGCHLD signal handler (use for notification, when background process ends)
void sighandler(int sig) {
    int status;
    int pid = waitpid(-1, &status, WNOHANG);

    if (WIFEXITED(status)) {
        if (pid != -1 && pid != 0) {
            printf("\n[%d] Done: \n> ", (int) pid); //also need to provide user name (command) of exited process (ex. [PID] Done: sleep)
            fflush(stdout);
        }
    }
}

pid_t fork_pid = fork()

if (fork_pid == 0) { //child
    execl("sleep", "sleep", "5s");
} else { //parent
    ....
}

What I need, is somehow access name of command, that was specified in exec() call (see execl(“sleep”, “sleep”, “5s”);) in signal handler (see signalhandler(int sig)) to output something like [PID] Done: sleep.

I cannot use simple global variable of last runned command, because after running some command in background, more new command in foreground can appear and will rewrite that global variable. Example:

> sleep 1m & //background command - variable is "sleep"
[PID of sleep process] Running in background
> ls //foreground command - variable is "ls"
> cat output //foreground command - variable is "cat"
.
.
.
[PID of sleep process] Done: sleep
>

Any way of doing this? Thanks a lot!

  • 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-30T19:02:09+00:00Added an answer on May 30, 2026 at 7:02 pm

    You can create a list to associate pids with commands:

    void sighandler(int sig) {
                ...
                char *cmd = task_list.get(pid);
                printf("\n[%d] %s, Done: \n> ", (int) pid, cmd);
                ...
    }
    
    ...    
    
    pid_t fork_pid = fork();
    char command[1024] = "sleep";
    if (fork_pid == 0) { //child
          execl(command, command, "5s");
    } else { //parent
          task_list.insert(pid, command);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented a simple c shell to take in commands like sleep 3 &
I am using Term::Shell package to implement a CLI tool. This package provides a
I'm currently trying to implement a custom shell command for Felix using iPOJO. My
My project is to implement a simple shell program with background processing by way
I am implementing a simple version of a linux shell in c. I have
I have implemented a simple linux shell in c. Now, I am adding some
I'm implementing a simple Graph library for my uni project and since this is
I am trying to implement a shell in C. I can execute simple commands
I am beginning to design a shell application to run within a Linux terminal
I implemented a very simple sample mobile app using phonegab. I used Visual Studio

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.