Saturday, March 31, 2012

Display menu until press 0 by using child process.

Display menu until press 0 by using child process.

Code;-

/*A small program that display menu until press 0*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
   int choice;
   do {
      printf("---------------=====00=====---------------");
      printf("

        Main Menu
");
      printf("Please select an option that you need:
");
      printf("    1. Executer ls command
");
      printf("    2. Execute ps command
");
      printf("    3. Execute who command
");
      printf("    0. exit
");
      printf("
Your choice: ");
      scanf("%d", &choice);
      //while (getchar() != '
');
      switch (choice){
          case 1:
               if (fork())
                  wait(0);
                else
                  execlp("ls", "ls", (char *)NULL);
                  break;
          case 2:
               if (fork())
                  wait(0);
               else
                  execlp("ps", "ps", (char *)NULL);
                  break;
          case 3:
               if (fork())
                  wait(0);
               else
                  execlp("who", "who", (char *)NULL);
                  break;
          case 0:
               break;
          default:
               printf("Please enter only 0-3
");
       }
   }
   while (choice != 0);
}
 

No comments:

Post a Comment