fork multiple shells - Unix

This is a discussion on fork multiple shells - Unix ; 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. 1 /* 2 * The idea is to fork multpile(equal to $ULIMIT) childs 3 * and replace ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: fork multiple shells

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




    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
    15 #include
    16 #include
    17 #include
    18 #include
    19 #include
    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 }



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

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

    [1]+ Stopped ./fork-csh

    [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
    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

  2. Re: fork multiple shells

    In article
    ,
    "amit4g@gmail.com" wrote:

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


    The child csh is making itself the foreground process on your terminal.
    This is causing fork-csh to be treated as a background process. You
    apparently have "stty tostop" enabled on your terminal, so when fork-csh
    tries to print "count reached", it's stopped, and it never gets to the
    kill loop.

    >
    >
    > 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
    > 15 #include
    > 16 #include
    > 17 #include
    > 18 #include
    > 19 #include
    > 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 }
    >
    >
    >
    > root@localhost ]# gcc -Wall -Werror fork-csh.c -o fork-csh
    >
    > [root@localhost ]# ./fork-csh
    > # child id is 14318
    >
    > [1]+ Stopped ./fork-csh
    >
    > [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
    > 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


    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

+ Reply to Thread