• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

17. Server Operating System > How Quickly Can My Server Fork New Processes?

How Quickly Can My Server Fork New Processes?

That evil little program can be modified to do something useful: it can tell us how quickly we can fork new processes. We have to add a little timing code, and a limit on the number of children to fork, and then we just use the unfashionable goto statement to have each child go back up to the fork statement. Because each child process is created by a fork system call rather than an execl call, it is identical to its parent and the counter is preserved. You can download it from http://patrick.net/software/forktime.c.

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

main(  ) {
     int pid;
     int count = 0;
     time_t      t;
     struct tm * tp;
     char       timestr[20];

    t  = time(NULL);
     tp = localtime(&t);
     strftime(timestr, 20, "%Y %m %d %H %M %S", tp);
     printf(timestr);
     printf("\n");

    there: pid= fork(  );

    if (pid== -1) perror("failed: ");

    if (pid==  0) {                  /* child */
         if (++count > 10000) {
             t  = time(NULL);
             tp = localtime(&t);
             strftime(timestr, 20, "%Y %m %d %H %M %S", tp);
             printf(timestr);
             printf("\n");

            _exit(0);
         }
        ....

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint