#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main_menu(void)
{
int main_choice = 0;
int child_choice = 0;
while (!(main_choice == 1 || main_choice == 2 || main_choice == 3))
{
printf("choose a menu option..1,2 or 3:\n");
printf("1) fork children\n");
printf("2) list children\n");
printf("3) kill children\n");
printf(">>> <<<\b\b\b\b");
scanf("%1d", &main_choice);
while (getchar() != '\n');
}
printf("%d\n", main_choice);
if (main_choice == 2) {
printf("listing all child process id's\n");
}
if (main_choice == 1) {
while (!(child_choice >= 1 && child_choice <= 9))
{
printf("how many children... 1 - 9 :\n");
printf(">>> <<<\b\b\b\b");
scanf("%1d", &child_choice);
while (getchar() != '\n');
}
}
if (main_choice == 3)
{
printf("killing all child proccess's's's's's\n");
}
main_choice *= 10;
// multiplying by 10 so i can return 2 values with one integer
// 12 = fork 2 children
// 19 = fork 9 children
// 20 = list children
// 30 = kill children
return main_choice + child_choice;
}
int main(void)
{
int i;
int user_choice;
int child_proccess_array[10] = {0};
user_choice = main_menu();
int parent_pid = getpid();
printf("i am %d\n",getpid());
if ((user_choice > 10) && (user_choice < 20)) {
int num_of_children = user_choice % 10;
printf("forking %d child proccesses.....\n",num_of_children);
for ( i = 1; i <= num_of_children ; i++) {
pid_t child_pid = fork();
child_proccess_array[i] = child_pid;
printf("i am %d\n",getpid());
printf("I %d forked my %d child proccess with a pid of %d\n",getpid(),i, child_pid);
if (child_pid == 0)
printf("%d says child pid is 0 and parent_pid = %d \n",getpid(),parent_pid);
if (getpid() != parent_pid)
break;
}
}
if (user_choice == 20){
for ( i = 1; i <10 ; i++) {
if ( child_proccess_array[i] >= 1 ) {
printf("child proccess %d = %d\n", i, child_proccess_array[i]);
}
}
}
if (user_choice == 30){
for ( i = 1; i <10 ; i++) {
if ( child_proccess_array[i] >= 1 ) {
printf("killing child proccess %d = %d\n", i, child_proccess_array[i]);
}
}
}
//printf("my global_pid = %ld\n",global_pid);
sleep(60);
return 0;
}