please help clarify these two fork tests

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Re: please help clarify these two fork tests

    In article <slrnbhbl0s.bck.drejcica@sonet.utopija.linux>,
    andrej hocevar <andrejh@volja.net> wrote:
    >Hello,
    >I've been trying to fathom fork() and came up with these two
    >programs. I'd like to ask why exactly is it that they behave
    >differently? Version 1 will always yield the same output whereas
    >version 2 won't. In fact, I wanted to see something like the output
    >from the second version -- when I fork(); I expect the program to
    >continue with its job and at the same time start a new process with
    >its own task. So when the output always comes in the same order, I
    >think I must be doing something wrong. Is the waitpid() call still
    >blocking? Why are the two tests different?
    >If it matters, I'm using Linux.
    In version 1, the parent sleeps for 10 seconds before continuing. That
    gives the child plenty of time to print its output. So the child's output
    will practically always come out first.

    In version 2, the OS gets to decide which process should run first.
    There's nothing specifying which one has priority, but it doesn't have to
    be a random choice, either. For instance, some scheduler designs give more
    priority to processes that have used less CPU time recently (this is good
    for making interactive applications more responsive when the system is
    heavily loaded); a brand-new process, which has used 0 CPU time, would have
    priority over its parent for a short while, and this could be enough time
    for it to do everything before the parent calls waitpid().
    >
    >Thanks,
    > andrej
    >
    >test 1:
    >================================================= ===================
    >#include <sys/types.h>
    >#include <sys/wait.h>
    >#include <signal.h>
    >#include <stdio.h>
    >#include <unistd.h>
    >
    >void myfinish()
    >{
    > waitpid(-1, NULL, WNOHANG);
    >}
    >
    >int main()
    >{
    > int i, j, k;
    > long x;
    >
    > struct sigaction MySignalAction;
    > MySignalAction.sa_handler = myfinish;
    >
    > sigaction(SIGCHLD, &MySignalAction, NULL);
    >
    > for (i = 1; i < 10; i++)
    > {
    > if (fork() == 0)
    > {
    > printf("I'm a child\n");
    > for (k = 0; k <= 10000; k++);
    > _exit(i);
    > }
    > else
    > {
    > sleep(10);
    > for (j = 1; j <= 10; j++)
    > printf("%i ", j);
    > printf("\n");
    > }
    > }
    >
    > return 0;
    >}
    >
    >test 2
    >================================================= ===================
    >
    >#include <sys/types.h>
    >#include <sys/wait.h>
    >#include <signal.h>
    >#include <stdio.h>
    >#include <unistd.h>
    >
    >int main()
    >{
    > pid_t pid;
    > int i, j, k;
    > long x;
    >
    > for (i = 1; i < 10; i++)
    > {
    > if (fork() == 0) /* child */
    > {
    > printf("I'm a child \n");
    > for (k = 0; k <= 100000; k++);
    > _exit(i);
    > }
    > else
    > {
    > while( waitpid(0, NULL, WNOHANG) > 0 ) ;
    >
    > for (j = 1; j <= 10; j++)
    > printf("%i ", j);
    > printf("\n");
    > }
    > }
    >
    > return 0;
    >}
    >

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  2. Similar Questions and Discussions

    1. Can someone please clarify something VERY simple for me?
      Hi. I have a few questions conercning both cfm and connectting databases. the SERVER has the have cfm installed, right? So that means I can just...
    2. httpwebrequest please help clarify
      I am posting an xml document using httpwebrequest post method using a x509certificate, my question is this, is this secure if I am posting to an...
    3. CGI Script Fork
      I'm working on a cgi script that basically, upon running it, runs a separate script in the background, and displays a message saying "Script is...
    4. housekeeping a filesystem, but met inner filesytesms... clarify.
      df shows my /usr 97%. so i intend to do some housekeeping. however, in /usr , there are other filesystems as well, like /usr/sap/SID and others. ...
    5. fork not available?
      I am running windows 2000 using the PragProgs install. I have tried fork but get an unimplemented on this machine error. I have tried this on...
  3. #2

    Default Re: please help clarify these two fork tests


    "Barry Margolin" <barry.margolin@level3.com> wrote in message
    news:DllRa.240$0z4.80@news.level3.com...
    > the OS gets to decide which process should run first.
    > There's nothing specifying which one has priority, but it doesn't have to
    > be a random choice, either. For instance, some scheduler designs give
    more
    > priority to processes that have used less CPU time recently (this is good
    > for making interactive applications more responsive when the system is
    > heavily loaded); a brand-new process, which has used 0 CPU time, would
    have
    > priority over its parent for a short while, and this could be enough time
    > for it to do everything before the parent calls waitpid().
    Some operating systems even deliberately let the child run first in the
    hopes that it might complete its job before the parent waits for it.
    Personally, I think this is a mistake. Unless a process does something that
    requires blocking, or it must be pre-empted because of static priorities or
    external events, a process should be allowed to use up its complete
    timeslice.

    DS


    David Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139