In ksh, how does an in-line child sub-process get its own PID?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In ksh, how does an in-line child sub-process get its own PID?
# 1  
Old 05-25-2006
Bug In ksh, how does an in-line child sub-process get its own PID?

This is not the same as a few of the other posted items dealing with sub-process pids (that I saw anyway).

If zot contains:
echo "main mypid: $$ - lastpid: $!"
(
echo "block mypid: $$ - lastpid: $! - ppid: $PPID"
ps -ef > xxx
sleep 5
echo "block mypid: $$ - lastpid: $! - ppid: $PPID"
) &
echo "main mypid: $$ - lastpid: $! - ppid: $PPID"
sleep 6
Then: ksh zot (under Solaris and MKS) results in:
main mypid: 7318 - lastpid:
block mypid: 7318 - lastpid: - ppid: 607
main mypid: 7318 - lastpid: 7320 - ppid: 607
block mypid: 7318 - lastpid: - ppid: 607
It surprises me that in the forked child process, that $$ still returns the pid of the main process. i.e. both show $$ as 7318 - I would have expected the child to have $$ be $7320. (You can ignore the ppid - I was looking at related notions.)

If you do: "grep 7320 xxx" you get:
root 7320 7318 0 13:59:45 pts/9 0:00 ksh zot
root 7321 7320 1 13:59:45 pts/9 0:00 ps -ef
which clearly shows the child process and if you were to "kill -9" that pid before the 5 seconds runs out, you would not get the second block line.

My intention was for the child process to record its own pid rather than the parent doing it on its behalf. I realise that the parent can use $! but why can the child not use $$ for its own PID? By the way, if you extract the block of code and put it in a separate file and call it, then you get the expected results.

Thank you in advance for any insights.
Michel
# 2  
Old 05-25-2006
ksh compiles the code and then executes it. The $$ is gone long before the fork occurs. So replace the ( stuff ) notation with
ksh -c "stuff"

Not a great solution, but it works.
# 3  
Old 05-25-2006
Then how would the following ever work:
xxx=$(date)
( sleep 5
xxx=$(date)
echo $xxx
) &
echo $xxx
if $xxx were coerced when the (...)& is being scanned, then the two echo statements would always show the same time but they don't (you can try it). Are you suggesting that $$ is coerced by different rules than $xxx?
# 4  
Old 05-25-2006
Yes. Special parameters and named parameters are different. I would post some sample code to prove this, but you you beat me to it. Smilie
# 5  
Old 05-26-2006
MySQL

Thanks for the insights.

I do find the behaviour borderline nuts presuming different rules for coercing variables.

An arguably valid definition I can think of as an alternate is that "$$" is by definition the initial shell's PID as opposed to the current process's PID, independent of sub-shells much like PPID is the parent of the initial shell regardless of being in a sub-shell or not. This is consistent with a few ksh man pages I dug up though not obvious.

It also seems to be consistent with a few experiments I tried with "eval" with a composite string that results in "$$" which would preclude the initial parsing of the (....)& from coercing a manifest $$ and that also produced the same result.

That ksh treats a subshell differently from a forked instance of ksh is consistent with other aspects of behaviour (e.g. variables need not be exported to be seen by a sub-shell) but it sure messes up the notion of using inline code vs external scripts in any consistent manner.

I will derive an alternative strategy (messing with ps -ef is not an option because my code needs to run under Solaris and Windows/MKS wherein ps differs due to the information about process parameters maintained by Windows).

Thanks again.
Michel
# 6  
Old 05-26-2006
A better way to think about it is that "subshell" is not guaranteed to be a new process. Look carefully at the definition of ( ) and you find "separate environment" rather than "new process". As long as no side effects propogate into the parent environment, it could be done without a fork and forking is expensive. So this leaves the door open for faster implementations. Also Dave Korn wanted ksh to be universal and some OS's do not allow fork(). (They may have a spawn() which is like a fork()/exec() combo or something like that.)

Anyway, I thought of a way to do it...I think. Background the subshell. Have the parent obtain $! and send it into the subshell via a named pipe. Then the parent waits for the subshell to exit. Smilie
# 7  
Old 05-27-2006
Quote:
Anyway, I thought of a way to do it...I think. Background the subshell. Have the parent obtain $! and send it into the subshell via a named pipe. Then the parent waits for the subshell to exit.
if the subshell that is spawned is sent to the background process group,
there are subtle points to be noted:::

if the subshell is interactive one and if it has to be switched to foreground process group and that must be explicitly done by the parent and it cannot do that by itself; only thing that could be done by the child itself is being stopped by generating SIGTTIN/SIGTTOU

if the subshell is non-interactive, care must be taken that the subshell should not support any of the job control activities.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh child process not ignoring SIGTERM

My ksh version is ksh93- =>rpm -qa | grep ksh ksh-20100621-3.fc13.i686 I have a simple script which is as below - #cat test_sigterm.sh - #!/bin/ksh trap 'echo "removing"' QUIT while read line do sleep 20 done I am Executing the script From Terminal 1 - 1. The ksh is started... (3 Replies)
Discussion started by: rpoornar
3 Replies

2. Shell Programming and Scripting

Getting child process id for a given PID

HI Am trying to get child process id for a PID using ksh.. ps -ef | grep xntpd root 3342472 2228308 0 12:17:40 - 0:00 /usr/sbin/xntpd root 4522024 6488316 0 12:18:56 pts/0 0:00 grep xntpd root 6291614 3342472 0 12:17:40 - 0:00 /usr/sbin/xntpd Here now i... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

5. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

6. Shell Programming and Scripting

Killing child process in ksh

I have a script that (ideally) starts tcpdump, sleeps a given number of seconds, then kills it. When I do this for 10 seconds or and hour, it works fine. When I try it for 10 hours (the length I actually want) it just doesn't die, and will actually stick around for days. Relevant part of my... (1 Reply)
Discussion started by: upnix
1 Replies

7. Shell Programming and Scripting

child pid in ZSH

I am using ZSH shell in Linux. I am calling a child program in background mode parallely (say 2-3 threads). I have problem in handling the temporary files of these child programs since the temp file names are unique for all the child process. To distinguish i want to use the pid in the temp... (3 Replies)
Discussion started by: dhams
3 Replies

8. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies

9. Programming

Child Process PID

Hi, Can anybody solve this query? A parent process forks 2 child processes. How does the child process know it's PID without the parent process sending it. Apart from the "ps-ef" option, what other options are there if any? (2 Replies)
Discussion started by: skannan
2 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question