Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Mar 1999 13:08:08 +0000
From:      Mark Ovens <marko@uk.radan.com>
To:        hackers@freebsd.org
Subject:   Problem with fork() in 3.1
Message-ID:  <36F79238.5FC1DA7C@uk.radan.com>

next in thread | raw e-mail | index | archive | help
Hi,

Firstly, my apologies if this is an inappropriate question to post
here, but I have asked in -questions and received no response and I am
completely stumped. If anyone could spare a couple of minutes to look
at this for me I would be very grateful (my learning curve is
currently flat-lining).

I have been learning about Inter Process Comms and have written the 2
progs included below. It all worked under 2.2.8 however when I u/g to
3.1-R (from the CD) it no longer works :-(.

Under 2.2.8 the output was:

  % demo
  RESULT! - Circumference-106.814150
  %

however, under 3.1 this happens:

  % demo
  RESULT! - 
  %

The string returned from d2c doesn't appear. This happens (in 3.1)
with both the 2.2.8 compiled progs (aout) and the 3.1 compiled progs
(ELF).

If I add ``sleep(5)'' to both progs ``ps'' confirms that the exec()'d
program (d2c) is running.

Someone else posted a message to -questions with exactly the same
problem using rfork().

FWIW, as well as working under 2.2.8, these programs run properly
under SunOS 4.1.3.

BTW. I realize that this code is not good enough for real-world
applications but, as I said, I'm learning so starting simple seems
that best way.

TIA for your time.

-----file - demo.c--------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

void main()
        {
        int fd[2];
        int df[2];
        pid_t pid;

        char input[BUFSIZ], result[BUFSIZ];

        if (pipe(fd) < 0)
                {
                perror("pipe failed");
                exit(EXIT_FAILURE);
                }
        if (pipe(df) < 0)
                {
                perror("pipe failed");
                exit(EXIT_FAILURE);
                }
        if ((pid = fork()) < 0)
                {
                perror("fork failed");
                exit(EXIT_FAILURE);
                }

        if (pid > 0)            /* parent                       */
                {

                close(fd[0]);

                close(df[1]);

                read(df[0], input, BUFSIZ - 1);

                if (strstr(input, "Diameter"))
                        write(fd[1], "34\n", 3);

                read(df[0], result, BUFSIZ - 1);

                fprintf(stderr, "RESULT! - %s\n", result);

                close(fd[1]);
                close(df[0]);

                }
        else
                {               /* child                        */
                if (fd[0] != STDIN_FILENO)
                        {
                        if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
                                {
                                perror("dup2 fd[0] failed");
                                exit(EXIT_FAILURE);
                                }
                        }

                if (df[1] != STDOUT_FILENO)
                        {
                        if (dup2(df[1], STDOUT_FILENO) !=
STDOUT_FILENO)
                                {
                                perror("dup2 df[1] failed");
                                exit(EXIT_FAILURE);
                                }
                        }

                close(fd[1]);

                close(df[0]);

                if (execvp("./d2c", NULL) < 0)
                        {
                        perror("execvp failed");
                        exit(EXIT_FAILURE);
                        }

                close(fd[0]);  /* Don't think these are really needed
*/
                close(df[1]);  /* as execvp() only returns on failure
*/
                               /* & then exit() will be called above 
*/
                }
        }

-----file - d2c.c----------

#include <stdio.h>
#include <stdlib.h>
#include <float.h>

#define PI      3.141592653

int main(void)
        {
        double circ;

        char input[BUFSIZ];

        printf("%s", "Diameter: ");

        fflush(stdout);

        fgets(input, BUFSIZ-1, stdin);

        circ = atof(input) * PI;

        printf("Circumference-%f\n", circ);

        fflush(stdout);

        return(0);
        }

-----------------------------------


-- 
      FreeBSD - The Power To Serve http://www.freebsd.org
      My Webpage http://www.users.globalnet.co.uk/~markov
_______________________________________________________________
Mark Ovens, CNC Apps Engineer, Radan Computational Ltd. Bath UK
CAD/CAM solutions for Sheetmetal Working Industry
mailto:marko@uk.radan.com                  http://www.radan.com


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?36F79238.5FC1DA7C>