i was trying to play with fork,exec and signal for spawning multiple new shells, but it seems that i'm doing blunder somewhere.
<sample code>
1 /*
2 * The idea is to fork multpile(equal to $ULIMIT) childs
3 * and replace their images with process:csh
4 * during this the parent will save the pids of all the spawned childs
5 * till count reaches $ULIMIT and will start killing all the
6 * childs.
7 */
8
9 /*
10 * The implementation to store the PIDs of all the child
11 * forked here is using an array
12 */
13
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <errno.h>
20
21 #define ULIMIT 2
22 int main(void){
23 pid_t pid;
24 int count;
25 int ret;
26 int childpids[ULIMIT];
27 for(count = 0; count < ULIMIT; count++){
28 pid = vfork();
29 if(pid < 0){
30 perror("fork error");
31 exit(1);
32 }
33 if(pid == 0){ /*In child,Replace it with a new process: csh*/
34 ret = execlp("csh", "/bin/csh",(char *)0);
35 if(ret){
36 fprintf(stderr,"could not exec csh for count: %d\n:",count);
37 exit(1);
38 }
39 }
40 else{ /*In Parent*/
41 printf("child's pid is %d\n",pid);
42 printf("count reached = %d\n",count);
43 childpids[count] = pid;
44 }
45 }
/* Now start killing all the childs(c- shells)*/
46 while(--count){
47 if(kill(SIGKILL,childpids[count]) == -1){
48 perror("kill error");
49 exit(1);
50 }
51 }
52 kill(SIGKILL,childpids[count]);
53 exit(EXIT_SUCCESS);
54 }
<sample code>
[root@localhost ]# ./fork-csh
# child id is 14318
[1]+ Stopped ./fork-csh
instead of my logic working i get "zombie process"
please let me know how to let my logic working.
~amit
Last edited by Yogesh Sawant; 06-23-2009 at 04:17 AM..
Reason: added code tags
You are getting zombies, because the child from childpids[0] isn't killed.
See in while(--count) : When count is 1 -> --count gives 0 and loop exits. The program "forget" to kill first child and when the father program closes, that child becomes a zombie.
You can replace the while loop with:
for(count-=1; count >= 0; count--) { ...
or something equivalent.
A note, I see you use vfork() for creating childrens.... which will always work fine in your example. However, its behaviour will become undefined when you will call other functions than execl after the fork, or modify other variables. Fork() would be safer for programs which are not using execl.
That's not going to be a terrifically useful enterprise without giving the shells the environment they expect. See man forkpty if on linux/*BSD.
Also you could handle child exits asynchronously by using waitpid in a handler for SIGCHLD and avoid your zombie woes.
An example: rmathew: Terminal Sickness
The task I have to do is something along the lines "I receive some input and based on the first character I send it through pipe to one of the children to print".
The scheme it is based on is 1->2; 1->3; 1->4; 2 will print all the input that starts with a letter, 3 will print all the input that... (2 Replies)
I have few bash shells, which i want to run sequentially,
how to create a shell file, and execute/call one after other shell file. I am very new to shell programming. Bult some and running individually and also with crontab scheduler.
never had a shell calling other shells, kindly would like... (2 Replies)
I mean like this: http://shells.red-pill.eu/
Can anyone explain how this works? I hope my post is not spam. I think its related to linux. Thank you (1 Reply)
Lets say my default shell is bash and then i load up csh and then ksh. How would i exit csh without exiting ksh?
so basically i gone from bash > csh > ksh and i wish to close csh (2 Replies)
I writing a program that forks three times but only on the parent process. The three children processes then produces output in order. 1, 2, 3. I am confused on how to do this. I have tried multiple if and else if statements but the output does not come out right. How should I go about doing this? (1 Reply)
Hi all,
I m writing an application, where i need to fork multiple childs and those child should handle particular task given to them.
More descriptive.
For example, suppose i have 4 Network, each network has multiple nodes. Now on the basis of network child should be forked and these child... (8 Replies)
Hi, I'm looking for a way to send commands through multiple shells/terminals (not sure which is proper syntax). Basically, I have to open 3 different shells/terminals and run separate parts of a program suite in each of them. I find this annoying. The commands I have to do are simple, and could... (1 Reply)
I have a code which has four different process, each printing different message. I have provided it with user input (implemented using thread), depending on which the corresponding message from that process has to be printed.The code is shown below.when I run the pgm, it takes input such as... (1 Reply)
Hello, I've tried for a while now to run a bash script that continues to the end, while opening new shells as needed.
I've tried
xterm -e "somecommand"; &
xterm -e " somecommand";
I've also tried
screen -S "somecommand"; &
screen -S "somecommand";
All without any luck, they... (5 Replies)
I have came across the definitions of these shells korn bourne c etc .. but honestly till now i din't get the exact difference between these threes , the advantages ..... can anyone pinpoint me where it actually lies ..... don;t include me answers like aliasing in c is posible and not in bourne ..... (3 Replies)