Note that there are some explanatory texts on larger screens.

plurals
  1. POExtract command from process (using PID)
    primarykey
    data
    text
    <p>I am implementig simple shell emulator in C. It should support running command in background (ex. sleep 5s &amp;). 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:</p> <pre><code>//SIGCHLD signal handler (use for notification, when background process ends) void sighandler(int sig) { int status; int pid = waitpid(-1, &amp;status, WNOHANG); if (WIFEXITED(status)) { if (pid != -1 &amp;&amp; pid != 0) { printf("\n[%d] Done: \n&gt; ", (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 .... } </code></pre> <p>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. </p> <p>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:</p> <pre><code>&gt; sleep 1m &amp; //background command - variable is "sleep" [PID of sleep process] Running in background &gt; ls //foreground command - variable is "ls" &gt; cat output //foreground command - variable is "cat" . . . [PID of sleep process] Done: sleep &gt; </code></pre> <p>Any way of doing this? Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload