0

Is there a possibility to know if my program, written in C, is running inside a subshell or a normal shell?

I want to know because I am unable to suspend it when it's running inside a subshell.

Glitch
  • 155
  • 1
  • 9
  • maybe you can follow the process parents till "init" and if you find 2 shells assume it's running inside a subshell. – pmg Sep 17 '19 at 20:27
  • 2
    Interesting question ... can you please focus a bit on what you would *do* with that information? E.g. how would your program behave differently? (Might be an XY problem) – Daniel Jour Sep 17 '19 at 20:58
  • 1
    When you say "unable to suspend it", how do you try suspending it? Have you checked signal handlers in your program (or, more importantly, which signals are ignored)? Some of the job control signals can't be ignored; others can, IIRC. Your program is always running as itself — so I guess your concern is related to how it is invoked. Is it inside `(…; your-program; …)` sub-shell, or in `some-prog | your-program | another-prog` pipeline, or what; and is the pipe-line or sub-shell itself run in background. Why are you worried? Why do you need to suspend your program? And only your program? – Jonathan Leffler Sep 17 '19 at 21:16
  • @JonathanLeffler You see, I study in a programming school, and I am obliged to create a program that's able to be suspended and retrieved, even from a subshell. I'm making a terminal-based item selection program that when I run it for example like this:```ls -la `./select *` ```, it gives me a grid of items to select from. – Glitch Sep 18 '19 at 17:56
  • OK; school rules rule. But I've no idea what you're trying to do or what problems you're facing. There's not enough information in the question, and no code, and "able to be suspended and retrieved" doesn't mean a great deal to me (but since I've been programming 30+ years, it could be premature fossilization that's the problem). I suspect you're talking about job control. Please try to create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve)) (or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). – Jonathan Leffler Sep 18 '19 at 18:00
  • In case you've not already gathered from the lack of a quick answer and my befuddled comments, there isn't a simple solution to what you're asking on the surface ("how to tell if a program is running in a sub-shell"), not least because it is not clear what _you_ mean by that phrase. – Jonathan Leffler Sep 18 '19 at 18:03
  • 1
    @JonathanLeffler https://stackoverflow.com/questions/57962161/why-cant-i-retrieve-my-program-that-was-suspended-from-inside-backticks – Glitch Sep 18 '19 at 18:04
  • OK; there's a lot more context in that other question. This question doesn't stand on its own. You're likely to need to look at process groups, sessions, parent pids, signal handling configurations and so on to make some sort of guess at whether your process is in some sort of sub-shell. You should find a way of recording the different aspects of the processes when run directly and when run in `$(…)` notation (preferable to back-ticks, but near enough the same thing for simple contexts). I'm not clear that running a program in command substitution normally counts as a sub-shell, but … – Jonathan Leffler Sep 18 '19 at 18:15
  • You could improve this question by asking something like: _How can a process tell whether it is being run as part of (or all of) a command substitution `$(…)` operation instead of directly from the command line?_ You might need to look at the standard I/O channels too — command substitution will probably not be going to a terminal, but `./program > file` doesn't go to a terminal either. This is why the usage patterns are so crucial. I don't understand what you're meant to be learning, either — really take a good look at your class notes and exercise. It probably isn't meant to be this hard. – Jonathan Leffler Sep 18 '19 at 18:19
  • ```aihya@e2r1p4 ~/Documents/1337/ft_select/src : echo `echo $ZSH_SUBSHELL` 1 aihya@e2r1p4 ~/Documents/1337/ft_select/src : echo $ZSH_SUBSHELL 0```. I think this is possible with zsh because you can see that it differentiate between a normal shell and a subshell. The problem is that I can get it from tha main function using that ```envp``` argument because it only get the global variables not the local ones. – Glitch Sep 18 '19 at 18:19
  • I'm also not convinced that the shell (Bash or whatever) thinks that control-Z should send a signal to the process in the command substitution operation. Probably not; that would be why typing control-Z doesn't work. Can you send the SIGTSTP or whatever directly via `kill`? Why are you capturing the output of the program via `$(…)`? – Jonathan Leffler Sep 18 '19 at 18:20
  • Are you using `zsh`? If so, I'm out of here — I've not used it and know very little about it, especially not about how it handles command substitution and signals and job control. – Jonathan Leffler Sep 18 '19 at 18:22
  • Ok, thank you. I think my reputation has dropped down hh. – Glitch Sep 18 '19 at 18:24

0 Answers0