Sponsored Content
Full Discussion: fork multiple shells
Top Forums Programming fork multiple shells Post 302190593 by amit4g on Wednesday 30th of April 2008 04:35:28 AM
Old 04-30-2008
Data fork multiple shells

Hi,

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>

Code:
root@localhost ]# gcc -Wall -Werror fork-csh.c -o fork-csh

[root@localhost ]# ./fork-csh
# child id is 14318

[1]+ Stopped ./fork-csh

Code:
[root@localhost ]# ps -el|grep csh|grep -v grep
4 T 0 14317 12639 0 75 0 - 344 finish pts/1 00:00:00 fork-csh
4 Z 0 14318 14317 0 80 0 - 0 do_exi pts/1 00:00:00 csh <defunct>
4 T 0 14319 14317 0 75 0 - 421 finish pts/1 00:00:00 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
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Shells

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)
Discussion started by: dino_leix
3 Replies

2. UNIX for Dummies Questions & Answers

How to open multiple shells while the scripts keeps running.

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)
Discussion started by: Closed_Socket
5 Replies

3. Programming

Multiple process using fork()

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)
Discussion started by: shashi
1 Replies

4. Shell Programming and Scripting

running Multiple terminals/shells

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)
Discussion started by: Caradoc
1 Replies

5. Programming

Need help with fork, forking multiple childs and shared memory

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)
Discussion started by: helpmeforlinux
8 Replies

6. Programming

multiple fork() question

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)
Discussion started by: TWhitt24
1 Replies

7. UNIX for Dummies Questions & Answers

Shells

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)
Discussion started by: Bill Thompson
2 Replies

8. UNIX for Dummies Questions & Answers

Please what are shells?

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)
Discussion started by: postcd
1 Replies

9. Shell Programming and Scripting

How to call exeute multiple bash shells from one master shell?

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)
Discussion started by: cplusplus1
2 Replies

10. UNIX for Beginners Questions & Answers

One parent, multiple children pipe with fork()

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)
Discussion started by: Ildiko
2 Replies
PTHREAD_ATFORK(3)					   BSD Library Functions Manual 					 PTHREAD_ATFORK(3)

NAME
pthread_atfork -- register handlers to be called before and after fork() SYNOPSIS
#include <pthread.h> int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); DESCRIPTION
The pthread_atfork() function is used to register functions to be called before and after fork(). The prepare handler is called before fork(), while the parent and child handlers are called after fork() in the parent and child process respectively. The prepare handlers are called in reverse order of their registration, while parent and child handlers are called in the order in which they were registered. Any of the handlers may be NULL. Important: only async-signal-safe functions are allowed on the child side of fork(). See sigaction(2) for details. RETURN VALUES
If successful, the pthread_atfork() function will return zero; otherwise an error number will be returned to indicate the error. ERRORS
pthread_atfork() will fail if: [ENOMEM] The system lacked the necessary resources to add another handler to the list. SEE ALSO
fork(2) STANDARDS
pthread_atfork() conforms to ISO/IEC 9945-1:1996 (``POSIX.1''). BSD
August 12, 2004 BSD
All times are GMT -4. The time now is 12:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy