Processes in Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Processes in Unix
# 1  
Old 09-30-2010
Processes in Unix

I am trying to play and create processes and found this piece of code....

Code:
int myMain(int argc , char* argv[]){
 
  childID = 0;
for (j=1;j<3;j++)
   if (childID = fork())
       break;

fprintf(stderr, " process ID:%d  parent ID:%d  child ID:%d\n",  getpid(), getppid(), childID);

 return 0;
}

I am confused by following. Main process will get to for loop and create first child. Then it will continue and print (fprint process id,child id parent id). When it is done with that should that main process go back up at the beginning of the loop. while at same time child that was created will print and go back up to the beginning of the loop.Am i looking at this right way?
# 2  
Old 09-30-2010
Quote:
Originally Posted by joker40
I am confused by following. Main process will get to for loop and create first child. Then it will continue and print (fprint process id,child id parent id). When it is done with that should that main process go back up at the beginning of the loop. while at same time child that was created will print and go back up to the beginning of the loop.Am i looking at this right way?
The parent process will break out of the loop at the if() because its return value will be zero. So it should be
  • parent creates child and quits
  • child prints, forks again, and quits
  • child's child prints, forks again, and quits

Which you're right, is odd. It's backwards I think. if(! (childID = fork()) ) break;, and putting the if after the print, would make every process print and only the parent process fork.
# 3  
Old 09-30-2010
?

Hi againSmilie....thanks for reply..
You said parent would break out of loop because it child value be zero.
When parent forks shouldn't his child value be value of child just forked. Child just created will have child id =zero?

---------- Post updated at 06:35 PM ---------- Previous update was at 06:19 PM ----------

Lets say first time process fork.....at that point its child id is not zero......child that parent created has child Id =0.
# 4  
Old 10-02-2010
You're right, I had it backwards. Don't know how I managed that while referring to the manpage!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SPAWN Multiple Processes in Unix

Hi, I have three files in my IN directory.Each file should be copied 25 times using for loop.Each file processing should run in parallel?How to spawn multiple processes in unix?Any help would be appreciated. Thanks, Liyakath (7 Replies)
Discussion started by: liyakathali
7 Replies

2. AIX

How to Identify long running unix processes

Hi All, Need an urgent help, I have a requirement to find long running unix processes.. I have tried the below commands, but not succeed. I need to arrange the unix processess in an order of elapsed time (high to low) that runs in a system. For Eg: Consider we have 3 processes, Pid 1 pid 2... (5 Replies)
Discussion started by: mohamedirfan
5 Replies

3. UNIX for Dummies Questions & Answers

Need Unix command to get all processes running on a port

Hi, Can someone provide me the Linux command to get the list of all processes running on a particular port. Thanks, Sandy (1 Reply)
Discussion started by: sandy8765
1 Replies

4. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

5. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

6. Shell Programming and Scripting

Communication between processes in Unix

hi folks!! I was stuck with a little problem where i needed to communcate between two processes, i.e., send a command to the parent process from the child process as soon as the child process terminates.This was required when i wanted to create a log using the script command in a bourne shell.The... (3 Replies)
Discussion started by: bdiwakarteja
3 Replies

7. HP-UX

HP-Unix; .net application creates processes

I have a .net application that works with an Oracle database in HP-Unix. Problem: .net application creates Oracle processes that don't close. By default Oracle can work with 200 active processes and after that crashes. I doubled this number and I don't have to many problems. I want: to... (12 Replies)
Discussion started by: NicoMan
12 Replies

8. Shell Programming and Scripting

Check for the Processes in Unix and also CPU utilization

What is the command for checking all the processes running on UNIX. Alos can any one share the CPU utilization script to know what are all the processes running and what is the cpu utilization ... thanks in advance Perla Mohan (3 Replies)
Discussion started by: perlamohan
3 Replies

9. UNIX for Dummies Questions & Answers

UNIX processes

hey, I know that there are bash commands like top or ps that allow me to monitor processes. But how can i monitor 1 process and log the output in a file? thx in advance Stephane Rufer (3 Replies)
Discussion started by: rufman
3 Replies

10. Shell Programming and Scripting

Unix Kill processes

Hi guys, I am new to Unix shell scripting. Can anyone of you tell me how to kill all the processes at a time for a particular user?(No listing the process ID of each process in the kill -9 command). Thanks in Advance, -Hary (5 Replies)
Discussion started by: tadi18
5 Replies
Login or Register to Ask a Question