Forking with Tclsh vs Wish


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Forking with Tclsh vs Wish
# 1  
Old 05-05-2008
Forking with Tclsh vs Wish

Hello,

I am new to this site, so sorry ahead of time if this is not the right place for this question.......anywhooooo

I am having troubles with forking new processes in wish. Take the following code example:

****************************
package require Tclx
puts "TCL VER: [info tclversion]"

proc spawnChildren {} {
for {set i_vii 0} {$i_vii < 5} {incr i_vii} {
set temp_vii [fork]
if {$temp_vii == 0} {
# Child Process
puts "child pid is: [pid]"
exit 1
} else {
# Parent Process
puts "parent\[[pid]\]: sees the child pid as $temp_vii"
# Store off child pids to collect later
lappend finalPid_vil $temp_vii
}
}
foreach pid_vii $finalPid_vil {
# Collect all zombie child process
wait $pid_vii
}
puts "parent out of loop: $finalPid_vil"
}
# Spawn 5 children off
spawnChildren
puts "out of spawning"
***************************

If I run this TCL script with tclsh I get the following output:

Monday|May.05|03:25PM> tclsh temp.tcl
TCL VER: 8.4
child pid is: 10469
parent[10468]: sees the child pid as 10469
child pid is: 10470
parent[10468]: sees the child pid as 10470
child pid is: 10471
parent[10468]: sees the child pid as 10471
parent[10468]: sees the child pid as 10472
child pid is: 10473
parent[10468]: sees the child pid as 10473
child pid is: 10472
parent out of loop: 10469 10470 10471 10472 10473
out of spawning

PERFECT...Just like I expect

But now I run it with Wish:
Monday|May.05|03:27PM> wish temp.tcl
TCL VER: 8.4
child pid is: 10481
parent[10480]: sees the child pid as 10481
child pid is: 10482
X connection to :0.0 broken (explicit kill or server shutdown).
parent[10480]: sees the child pid as 10482
child pid is: 10483
X connection to :0.0 broken (explicit kill or server shutdown).
parent[10480]: sees the child pid as 10483
child pid is: 10484
X connection to :0.0 broken (explicit kill or server shutdown).
parent[10480]: sees the child pid as 10484
child pid is: 10485
parent[10480]: sees the child pid as 10485
X connection to :0.0 broken (explicit kill or server shutdown).
parent out of loop: 10481 10482 10483 10484 10485
out of spawning
X connection to :0.0 broken (explicit kill or server shutdown).


Why am I receiving these X Errors? Is wish overriding the normal exit procedure and then causing havoc?

I am running redhat 4.0 Enterprise with Tcl 8.4. Any help would be greatly appreciated. Thanks ahead of time!!! Smilie
# 2  
Old 05-06-2008
I also just learned if I do the following then the errors go away:

package require Tclx

proc spawnChildren {} {
for {set i_vii 0} {$i_vii < 5} {incr i_vii} {
set temp_vii [fork]
if {$temp_vii == 0} {
# Child Process
puts "child pid is: [pid]"
exec kill -9 [pid]
} else {
# Parent Process
puts "parent\[[pid]\]: sees the child pid as $temp_vii"
# Store off child pids to collect later
lappend finalPid_vil $temp_vii
}
}
foreach pid_vii $finalPid_vil {
# DO NOT Collect all the zombie child process
}
puts "parent out of loop: $finalPid_vil"
}

# Spawn 5 children off
spawnChildren
puts "out of spawning"


So now by calling the linux system kill command and not waiting for any of the Zombie Processes the script works just fine. Now the only problem is that a bunch of defunct/zombie processes are now taking up space in the process table (which is not a problem unless the system runs out of entries). So any ideas of why this might be happening as I can recreate the same issue with perl/tk as well Smilie
# 3  
Old 05-06-2008
Wish is the graphical toolkit 'shell' of TCL/TK. Obviously it tries to connect to the X server on your platform. If you don't want the errors output catch the kill and wait commands.

TclX and wish or expect and wish are not guaranteed to work well together.
See here: TclX
# 4  
Old 05-06-2008
Ok...well I can get the same X Errors without even using wish. If I take the same example in my first post but also do a "package require Tk" then source that file with tclsh, I will receive the same X Errors. So it has nothing to do with wish (I just realized this today). It has to do something with sourcing the Tk library. This was evident to me when I was able to recreate the same problem with Perl/Tk. Is there anyway to gracefully close the X Server connection in Tk?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

passing arguments to unix command or script inside tclsh

hi everobody kindly consider the following in tclsh I understand that we can do the following %exec UnixCmd arg1 arg2 but if I assinged the arguments to a list insde tclsh how can I use them back i.e %set ArgList %exec UnixCmd %exec Unixcmd $list %exec all the... (1 Reply)
Discussion started by: Blue_shadow
1 Replies

2. Programming

need help in forking

I have an input file with contents like: 5785690|68690|898809 7960789|89709|789789 7669900|87865|659708 7869098|65769|347658 so on.. I need to pass this file to 10 parallely running processes (forking)so that each line is processed by a process and no line is processed twice and write the... (1 Reply)
Discussion started by: rkrish
1 Replies

3. Programming

Parent forking

My question is, how do you fork only the parent processes in unix? For example how would I use the fork function to fork the parent process more than once and leave the children processes alone. This way I do not have children of children. The way I have it set up now it the parent process forks 3... (7 Replies)
Discussion started by: TWhitt24
7 Replies

4. Shell Programming and Scripting

Forking and Pinging

Keep in mind that I haven't done Perl scripting for a LONG time, so I'm quite rusty. This is what I would like to do: - using fork, create 3 or 4 processes to read 3 or 4 different text documents containing server names or IP addresses - in each of those processes, Perl will ping each of those... (7 Replies)
Discussion started by: kooshi
7 Replies

5. Shell Programming and Scripting

exec tclsh

As a part of learning shell scripting I was just going through some already created scripts there I found- exec tclsh "$0" $(1+"$@") Here I was able to find what exec ,tclsh does & o/p of same but I could not find usage & output of $(1+"$@"). Can anybody pls expalain me usage details of it? (5 Replies)
Discussion started by: rediffmail
5 Replies

6. Shell Programming and Scripting

How to set Field Separator for TCLSH??? :S

Hi, I am having a problem, with setting the FS in TCLSH, maybe someone knows the answer. I have a Tclsh/Tk script, and i´m trying to get all (only) the running processes for the given user like this: foreach process { puts stdout $process; } This also works, but... (2 Replies)
Discussion started by: laptop87
2 Replies

7. Shell Programming and Scripting

Can you explain me a tclsh file?

Hello, I got a file I am using, but I cannot understand what this file is doing... Can someone explain? Here is a part of the file: #! /usr/bin/tclsh set mctal r] set pow set SS set a set b set c set d set e set f set g set h (0 Replies)
Discussion started by: jolecanard
0 Replies

8. Shell Programming and Scripting

tclsh

In bash wen we press tab the name of the files get completed automatically. Likewise there is no auto completion feature in tclsh. Any idea if there is any way to get this feature or should we implement ?? Please suggest (0 Replies)
Discussion started by: Laksmi
0 Replies

9. Programming

forking process.

#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pID; int i; for (i = 0; i < 3; i++) { pID = fork (); if (pID == 0) { printf ("Value of i --> %d... (2 Replies)
Discussion started by: kymthasneem
2 Replies

10. UNIX for Advanced & Expert Users

Forking

When I compile this C programme I get different outputs each time I run it Please explain to me whats happening in the code if you can give me a detailed explanation with the schedular functionality it will help a lot. Because I am stuck with this. #include <stdio.h> main(){... (3 Replies)
Discussion started by: manjuWicky
3 Replies
Login or Register to Ask a Question