Hi,
Following code is doing the fork of 200 process, and run some command on it. It's doing the fork for all 200 processes in once. I need 50 Processes to be forked, and then is any finish start new one. How I can do it?
#include <unistd.h>
pid_t pid;
long forking_counter;
int i_seq_no;
int i_arr_idx;
int i_exec_idx;
int process_count = 0;
typedef struct st_pro {
int p_no;
pid_t p_id;
int status;
};
struct st_pro pro_array[200];
void call_func(pid_t p)
{
system("./mysh");
}
int main()
{
i_arr_idx = 0;
i_exec_idx = i_arr_idx;
forking_counter = 2;
pid = getpid();
// printf("Amar: forking_counter = [%d], pid = [%d], i_seq_no = [%d]\n", forking_counter, pid, i_seq_no);
while((pid!=0) && (forking_counter <=200))
{
i_arr_idx++;
sleep(0);
pid = fork();
if (pid == -1)
{
printf("Amar: Unable to fork the new process\n");
exit(0);
}
if(pid != 0)
{
pro_array[i_arr_idx].p_no = forking_counter;
pro_array[i_arr_idx].p_id = pid;
forking_counter++;
// printf("Amar: forking_counter = [%d], pid = [%d]\n", forking_counter, pid);
for(int i = 0; i < forking_counter; i++) {
printf("Amar: [%d][%d]\n", i, pro_array[i_arr_idx].p_id);
}
}
else
i_exec_idx = i_arr_idx;
}
call_func(pid);
return 0;
}
Following code is doing the fork of 200 process, and run some command on it. It's doing the fork for all 200 processes in once. I need 50 Processes to be forked, and then is any finish start new one. How I can do it?
#include <unistd.h>
pid_t pid;
long forking_counter;
int i_seq_no;
int i_arr_idx;
int i_exec_idx;
int process_count = 0;
typedef struct st_pro {
int p_no;
pid_t p_id;
int status;
};
struct st_pro pro_array[200];
void call_func(pid_t p)
{
system("./mysh");
}
int main()
{
i_arr_idx = 0;
i_exec_idx = i_arr_idx;
forking_counter = 2;
pid = getpid();
// printf("Amar: forking_counter = [%d], pid = [%d], i_seq_no = [%d]\n", forking_counter, pid, i_seq_no);
while((pid!=0) && (forking_counter <=200))
{
i_arr_idx++;
sleep(0);
pid = fork();
if (pid == -1)
{
printf("Amar: Unable to fork the new process\n");
exit(0);
}
if(pid != 0)
{
pro_array[i_arr_idx].p_no = forking_counter;
pro_array[i_arr_idx].p_id = pid;
forking_counter++;
// printf("Amar: forking_counter = [%d], pid = [%d]\n", forking_counter, pid);
for(int i = 0; i < forking_counter; i++) {
printf("Amar: [%d][%d]\n", i, pro_array[i_arr_idx].p_id);
}
}
else
i_exec_idx = i_arr_idx;
}
call_func(pid);
return 0;
}