newbie forking

newbie forking

Post by ypan » Sun, 03 Dec 2000 04:00:00



i tested fork like this:

  if ((pid = fork()) == 0) {
    if (r=execl("./myserv", "myserv", argv[1], (char*)NULL));
      cerr<<"err"<<endl;
  }

the program "myserv <port_number>" is a tcp socket listener.

question:
how can i redirect the output of myserv to /dev/null in an execl()?
and, how can i get the return value of myserv, i mean, not execl()?
and, how to kill the spawned myserv in parent process?

thanks a lot!

 
 
 

newbie forking

Post by ypan » Sun, 03 Dec 2000 04:00:00


sorry, it was

  if ((pid = fork()) == 0) {
    if ((r=execl("./myserv", "myserv", argv[1], (char*)NULL)) < 0)
      cerr<<"err"<<endl;
  }

 
 
 

newbie forking

Post by NOWIES » Sun, 03 Dec 2000 04:00:00



>i tested fork like this:

>  if ((pid = fork()) == 0) {
>    if (r=execl("./myserv", "myserv", argv[1], (char*)NULL));
>      cerr<<"err"<<endl;
>  }

>the program "myserv <port_number>" is a tcp socket listener.

>question:
>how can i redirect the output of myserv to /dev/null in an execl()?

Try this (but I'm not sure):
fd=open("/dev/null",......);
close(1);
dup2(fd,1);

Quote:>and, how can i get the return value of myserv, i mean, not execl()?

I heard (I'm not alse not sure) wait(...) waits for child being ended and
than returns its return value.
I don't know any other way to obtain this value. I'd also be glad if sbd
answered this question.

Quote:>and, how to kill the spawned myserv in parent process?

kill(SIGKILL,pid);

NOWIESZ

 
 
 

newbie forking

Post by Axel Wei » Tue, 05 Dec 2000 04:00:00


Hallo, Ypang


> i tested fork like this:

>   if ((pid = fork()) == 0) {
>     if (r=execl("./myserv", "myserv", argv[1], (char*)NULL));
>       cerr<<"err"<<endl;
>   }

> the program "myserv <port_number>" is a tcp socket listener.

> question:
> how can i redirect the output of myserv to /dev/null in an execl()?

The simplest way is to (temporarily) redirect the output in your parent
process, _before_ you fork. If you like, you then can reset the output
channel of the parent process.

Quote:> and, how can i get the return value of myserv, i mean, not execl()?

The return value of 'myserv' can be obtained, only _after_ 'myserv' has
been terminated. Every parent process has to organize to wait for it's
children after their termination -- otherwise you will have some
'zombies', in worst case until your next reboot! Wait(pid) will deliver
the return value.

Quote:> and, how to kill the spawned myserv in parent process?

int pid, ret=0;
if ((pid=fork()) == 0){
        /* child process */
Quote:}

else{
        /* parent process */
        ...
        kill(SIGTERM, pid);
        if (waitpid(pid, &ret, NULL) == pid){
                /* ret contains the return value */
        }

Quote:}

Hope I could help you
:*) Axel

--
Humboldt-Universit?t zu Berlin, Institut fr Informatik
Axel Wei?
Rudower Chaussee 25, 12489 Berlin
Postanschrift: Unter den Linden 6, 10099 Berlin
Telefon: + 49(30) 2093 3050     FAX  : + 49(30) 2093 3045

 
 
 

newbie forking

Post by Scott Neugrosch » Tue, 05 Dec 2000 04:00:00



> Hallo, Ypang


> > i tested fork like this:

> >   if ((pid = fork()) == 0) {
> >     if (r=execl("./myserv", "myserv", argv[1], (char*)NULL));
> >       cerr<<"err"<<endl;
> >   }

> > the program "myserv <port_number>" is a tcp socket listener.

> > question:
> > how can i redirect the output of myserv to /dev/null in an execl()?

> The simplest way is to (temporarily) redirect the output in your parent
> process, _before_ you fork. If you like, you then can reset the output
> channel of the parent process.

> > and, how can i get the return value of myserv, i mean, not execl()?

> The return value of 'myserv' can be obtained, only _after_ 'myserv' has
> been terminated. Every parent process has to organize to wait for it's
> children after their termination -- otherwise you will have some
> 'zombies', in worst case until your next reboot! Wait(pid) will deliver
> the return value.

> > and, how to kill the spawned myserv in parent process?

> int pid, ret=0;
> if ((pid=fork()) == 0){
>         /* child process */
> }
> else{
>         /* parent process */
>         ...
>         kill(SIGTERM, pid);
>         if (waitpid(pid, &ret, NULL) == pid){
>                 /* ret contains the return value */
>         }
> }

Even better is to redirect in the child...

if ((pid = fork()) == 0) {
        close(1);
        // note -- only redirecting stdout so that we can still send stuff to
stderr/cerr
        open("/dev/null",O_WRONLY);
        if (r = execl(...)) ...

Quote:}

//...

Scott

 
 
 

newbie forking

Post by Karl Keusge » Thu, 07 Dec 2000 04:00:00



Quote:

> i tested fork like this:

>   if ((pid = fork()) == 0) {
>     if (r=execl("./myserv", "myserv", argv[1], (char*)NULL));
>       cerr<<"err"<<endl;
>   }

> the program "myserv <port_number>" is a tcp socket listener.

> question: how can i redirect the output of myserv to /dev/null in an
> execl()? and, how can i get the return value of myserv, i mean, not
> execl()? and, how to kill the spawned myserv in parent process?

> thanks a lot!

Hi,

run this small program, it answers your questions.


/* small demo-programm which daemonizes itself and      */
/* starts one child process. The stdout of the Child-   */
/* process is redirected to /dev/null                   */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <wait.h>

int daemon_child(int param1 )
{

        long i = 0;

        int fd_null = open ("/dev/null", O_WRONLY);

        printf ("\n child process %d started\n redirect stdout NOW! \n\n",
        getpid());

        fflush(stdout);

        /* doin some errhanfding, or not */

        /* close the old stdout */
        close (STDOUT_FILENO);

        /* redirect it to fd_null */
        dup2( fd_null, STDOUT_FILENO);

        /* close it, cause after dup it's not used anymore */
        close (fd_null);

        /* do some output */
        while (1)
        {
                printf("%d\n", i);

                fflush (stdout);
        }      

        return 0;

Quote:}

int main (int argc, char** argv, char** env)
{
        pid_t   PID;
        int status;

        if (!fork())
        {      
                /* set process leader ID */
                setsid();

                /* daemonize it, nice */
                if (!(PID=fork()))
                {
                        /* start child's workin' function */
                        exit(daemon_child(20));        
                }
                else
                {
                        /* kill child-process after 10 seconds */
                        fprintf (stderr, "parent process %d is daemonized and
                        got one child, PID: %d\n", getpid(), PID );

                        fflush(stderr);

                        sleep (10);

                        fprintf (stderr, "parent process %d is goin' to kill
                        child %d\n", getpid(), PID );

                        kill(PID, SIGKILL);    

                        /*wait for killed processes, otherwise you get a zomby*/
                        /*may use waitpid if you got more than one child      */
                        wait(&status);

                        fflush (stderr);

                        sleep (10);

                        fprintf (stderr, "daemonized parent process %d exits
                        itself\n", getpid());

                        fflush (stderr);
                }
        }
        else
        {
                exit (0);
        }

        return 0;      

- Show quoted text -

Quote:}

 
 
 

1. Newbie fork() question

Hi,
I hope this is a simple question, in the following code I make a child and
parent process, but what I want to do is create one parent and an arbitrary
number, n of child processes. Okay so I thought a for loop from 0 to n-1 but
I'm unsure how to do it, how would I prevent getting n child AND n parent
processes, when I want just the one parent process. Also, will the child
processes be distinguishable, as I want to be able to use the execlp()
command to call a different function for each child process (or the same
function with different parameters for each child).
Many thanks to anyone who can help with this.
Philip.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

main()
{
    pid_t pid;
    int rv;

    pid = fork();

    if ( pid == -1 )
            perror("fork");  /* something went wrong */
            exit(1);         /* parent exits */
            }
        if ( pid == 0 )
        {
            printf(" CHILD: This is the child process!\n");
            printf(" CHILD: My PID is %d\n", getpid());
            printf(" CHILD: My parent's PID is %d\n", getppid());
            printf(" CHILD: Enter my exit status (make it small): ");
            scanf(" %d", &rv);
            printf(" CHILD: I'm outta here!\n");
            exit(rv);
        }
        else
        {
            printf("PARENT: This is the parent process!\n");
            printf("PARENT: My PID is %d\n", getpid());
            printf("PARENT: My child's PID is %d\n", pid);
            printf("PARENT: I'm now waiting for my child to exit()...\n");
            wait(&rv);
            printf("PARENT: My child's exit status is: %d\n",
WEXITSTATUS(rv));
            printf("PARENT: I'm outta here!\n");
        }

2. sis735 io-apic ide1 problem

3. ** Newbie fork question **

4. QoS

5. comsat forking, forking and forking (3.2.0)

6. diskless firewall

7. fork call without using function fork()

8. CD-R Non-recognition

9. Efficiency: fork() w/ shared libraries vs. fork()/exec()

10. to fork or to pre-fork

11. Newbie Questions (fork, etc.)

12. Help programming C question, newbie to fork twice?

13. toupper()/pipe()/fork() - problem (newbie?)