VERY confused about forking of child process


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users VERY confused about forking of child process
# 1  
Old 02-21-2008
Question VERY confused about forking of child process

hi,

I thought that when a child shell is forked, it will inherit all the variables of the parent

now in my .cshrc I have

Code:
setenv X x

then I do at command line

Code:
setenv X y

and X is now y. So far so good!

I then have a very simple script, y.csh

Code:
#!/usr/bin/csh

echo X

Now when I do

Code:
%: y.csh

x gets printed. Why? I thought the child will inherit all the variables of its parent. So it should inherit X as y, yes?

thanks.
# 2  
Old 02-21-2008
Works as expected for me

Code:
%setenv X x
%echo $X
x
%setenv X y
%echo $X
y
%cat t
#!/usr/bin/csh

echo $X
%./t
y
%

# 3  
Old 02-22-2008
no, I think you missed the bit that my .cshrc also sets X (to x)

thanks
# 4  
Old 02-22-2008
Quote:
Originally Posted by JamesByars
hi,

I thought that when a child shell is forked, it will inherit all the variables of the parent

now in my .cshrc I have

Code:
setenv X x

then I do at command line

Code:
setenv X y

and X is now y. So far so good!

I then have a very simple script, y.csh

Code:
#!/usr/bin/csh

echo X

Now when I do

Code:
%: y.csh

x gets printed. Why? I thought the child will inherit all the variables of its parent. So it should inherit X as y, yes?

thanks.
There is nothing strange in the output.
going through your "y.sh", you gave "echo X" - which will print "X"
if you want to print value in variable X, you should have given "echo $X"
# 5  
Old 02-22-2008
Unless it is a requirement, consider using a shell that does not have the problems csh has.

Here is an article, feel free to disagree:
into the wibble [csh is bad]
# 6  
Old 02-22-2008
sorry, I DO have echo $X.

Code:
#!/usr/bin/csh

echo $X

To sum up, this is what is happening:

1) .cshrc sets X to x
2) I setenv X y in my shell
3) I then do y.csh

Now I know .cshrc will be called everytime a csh script is called. And obviously in this script we;re setting X to x. But I also thought that the child shell will inherit all the parents variables. So I would have expected it to inherit X = y, and for this to overrride whatever is happening in the .cshrc!

thanks.
# 7  
Old 02-22-2008
Please do a "man csh"
-f Suppress execution of the .cshrc file

change your script from:

Code:
#!/usr/bin/csh
echo $X

to:
Code:
#!/usr/bin/csh -f
echo $X

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

3. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 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. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

6. Shell Programming and Scripting

Forking a bunch of processes and filling up the process table

I have a bash script that has been used for months here at work for doing an SSH into other machines both Linux and Solaris and running a script on the remote machine. Recently I have started to noticed that things are being left being on the maching doing the SSH. For example.... tivoli ... (1 Reply)
Discussion started by: LRoberts
1 Replies

7. UNIX for Advanced & Expert Users

Forking a new process without parent dependance

hi, I want my program to fork a new process and then I want to kill the parent process. The parent program before dying will issue a SIGTERM to all its childs. Which eventually kills all Children. I cant handle the SIGTERM at the child level.:( What I was thinking of was the Parent... (3 Replies)
Discussion started by: tyler_durden
3 Replies

8. UNIX for Dummies Questions & Answers

Testing the forking process.

Hey, first time poster and a new UNIX user here. My question is regarding the forking process. I logged in to tty1, and typed the command ls -1 and hit enter. How can i tell that the ls -1 command ran in a subshell? Thanks. (0 Replies)
Discussion started by: Vitamin254
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. Programming

forking a new process

Hi I'm currently working with C on UNIX (HPUX) and need to be able to fork a seperate Java process from within a running C process. I can run the following code from the command line via a script but am having difficulty getting it to work from within the code. I am trying to use execl. Is... (4 Replies)
Discussion started by: themezzaman
4 Replies
Login or Register to Ask a Question