Re: gtk and system



Matt,

    If I have this correct, you are doing something like the following:


     loop through selected rows (tasks) which need to be killed
     retrieve child PID from tasks[] array (you might look into
     gtk_clist_set/get_row_data for this . . . very easy)
     create command line string to kill the task
     fork program (why?)
     PARENT:
         kill selection task
         continue running
     CHILD:
         continue running (this is bad)
     execute waitpid on most recently created child (still running . . .
     probably just hangs, right?)


I *think* what you want to do is more something like this:

     loop through selected rows (tasks) which need to be killed
     retrieve child PID from wherever
     kill(child_pid, 9);   <-- this is a C system call - include
     <sys/types.h> and forget the extra overhead
     waitpid(child_pid, NULL, 0);  <-- wait for child to exit and return
     resources to system;

In forking and exec'ing another process, you are adding children you don't
need.   Simply kill the selected processes in order and continue on.  Much
simpler and *I think* what you intended in the first place.

Matt Eisemann wrote:

> > ----------
> > From:         Matt Eisemann[SMTP:MEISEMANN DSRNET COM]
> > Sent:         Thursday, October 12, 2000 10:21:39 AM
> > To:   'Lance Capser'
> > Cc:   gtk-list gnome org
> > Subject:      RE: gtk and system
> > Auto forwarded by a Rule
> >
> do you have any example code.  I added waitpid and it still does not
> work.......
>
> PS thanks for your help and here is what I did:
>
> for(count = 0; count < length; count++)
> {
>   row = (int)g_list_nth_data(GTK_CLIST(display_tasks)->selection, count);
>   /* sequentially get row to stop */
>   strcpy(stop_task,"kill -9 ");
>   sprintf(process_id,"%d",tasks->task_list[row].process_id;
>   strcat(stop_task,process_id);
>   printf("execute command: %s \n",stop_task);
>
>   if ((pid = fork()) < 0)
>   {
>     perror("fork error");
>     result = -1;
>     exe_status = FALSE;
>   }
>   else if(pid == 0)
>   {
>     if(execl("/bin/sh","sh","-c",stop_task,(char *) 0) < 0)
>     {
>       perror("execlp error");
>     }
>   }
>   if(waitpid(pid,NULL,0) != pid)
>   {
>     printf("wait error \n");
>   }
>
> } /* end for */
>
> Any good examples I can look at.  This piece of code goes through a CLIST
> and get the row the user has selected and attempts to stop that task.
> Thanks ahead of time for your help.
>
> -----Original Message-----
> From:   Lance Capser [SMTP:lmc cyberhighway net]
> Sent:   Tuesday, October 10, 2000 11:28 AM
> To:     Matt Eisemann
> Cc:     gtk-list gnome org
> Subject:        Re: gtk and system
>
> Matt,
>
>     Under UNIX, child processes will wait around until notification that
> the
> parent is "aware" of the childs <defunct> status.  In the GTK application,
> after you kill the child's PID, execute a waitpid() on the child's process
> to
> allow the system to clean up all the child's resources.  You can choose to
> stop execution in the parent and wait for the child to die, or simply get
> the
> status and continue.  Either way, the child process will go away and you
> won't be left with a bunch of zombie processes lying around.
>
> Lance
>
> Matt Eisemann wrote:
>
> > > ----------
> > > From:         Matt Eisemann[SMTP:MEISEMANN DSRNET COM]
> > > Sent:         Tuesday, October 10, 2000 8:59:34 AM
> > > To:   gtk-list gnome org
> > > Subject:      gtk and system
> > > Auto forwarded by a Rule
> > >
> > This may not be a gtk problem, but let me explain my problem.  I have an
> > GTK+ application which starts processes using fork and execle, which
> works
> > great.  I save the process id (pid) of that process .  Later on, a user
> > pushes a button called 'stop tasks' and when called it concatenates the
> > 'kill' command with the saved pid.  Below is some basic code.
> > When I try to kill the processes I started the system command does not
> > return an error code but the process consistently goes into a <defunct>
> > mode instead of dying.  However, when I leave my GTK+ application, the
> > <defunct> process does die.  But why not immediately.  Is this a gtk
> > problem, or a UNIX limitation or is the fact that I started the processes
> > with the GTK application cause some sort of a parent - child
> relationship.
> >  Thanks ahead of time for your help.
> >
> > sprintf(process_id,"%d",tasks->task_list[row].process_id);
> > strcpy(executable,"kill -9 ");
> > strcat(executable,process_id);
> >
> >   system(process_id);
> >
> > _______________________________________________
> > gtk-list mailing list
> > gtk-list gnome org
> > http://mail.gnome.org/mailman/listinfo/gtk-list
>
>   ------------------------------------------------------------------------
>   ___                                          Lance Capser
>  /\_ \                          Freelance Web Page Designer / Web Master /
>  \//\ \     ___ ___     ___          Unix SysAdmin / C,C++ Programmer
>    \ \ \  /' __` __`\  /'___\
> ----------------------------------------------
>     \_\ \_/\ \/\ \/\ \/\ \__/
>     /\____\ \_\ \_\ \_\ \____\         E-Mail: lmc cyberhighway net
>     \/____/\/_/\/_/\/_/\/____/            Phone: (208) 461-0222
>                                            Pager: (208) 391-0448
>  "If DOS had been better in the first place, I might not have started
> this.-
>                                 Linus Torvalds
>
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list

--
  ------------------------------------------------------------------------
  ___                                          Lance Capser
 /\_ \                        Freelance Web Page Designer / Web Master / Unix
 \//\ \     ___ ___     ___             SysAdmin / C,C++ Programmer
   \ \ \  /' __` __`\  /'___\ -----------------------------------------------
    \_\ \_/\ \/\ \/\ \/\ \__/
    /\____\ \_\ \_\ \_\ \____\         E-Mail: lmc cyberhighway net
    \/____/\/_/\/_/\/_/\/____/             Phone: (208) 461-0222
                                           Pager: (208) 391-0448
  "If DOS had been better in the first place, I might not have started this.-
                                Linus Torvalds







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]