Hi,
I am new to Unix C programming. I am trying to write a simple multi-process server. The server get an numberic input from keyboard and start a new child process, the child process does nothing but sleep for a few seconds and then exit. The parent process return to accept more input without waiting for the child process to exit.
The problem that I have is that after I fork() for a couple of times, that is acceptting inputs from keyboard for two or three times, the parent process begins to create process automatically without user input.
Here is the code that I wrote.
#define MAX_PROCESS 5;
static int current_process_num=0;
void install_sig_handler();
void sig_handler(int sig);
int main() {
int seconds;
int i;
install_sig_handler();
printf("pid: %d--enter seconds ", getpid());
fflush(stdout);
for(; {
scanf("%d", &seconds);
if (current_process_num < MAX_PROCESS){
current_process_num++;
switch (fork()){
case -1:
fprintf(fp,"ERROR! Can not spawn child process\n"
exit(1);
case 0:
for (i=0;i<seconds;i++){
sleep (1);
}
_exit(0);
default:
break;
}
}
else {//has reach the limit
printf("has reached the limit, please try again\n"
fflush(stdout);
}
}
return 0;
}
void install_sig_handler(){
struct sigaction sig_struct;
sig_struct.sa_handler=sig_handler;
sigemptyset(&sig_struct.sa_mask);
sig_struct.sa_flags=0;
if(sigaction(SIGCHLD, &sig_struct, NULL)!=0){
perror("error"
exit(1);
}
}
void sig_handler(int sig){
pid_t pid;
int status;
while ((pid=waitpid(-1, &status, WNOHANG))>0){
current_process_num--;
// printf("one child has exited, and the current process num is %d\n",current_process_num);
}
}
Thanks for your kindly help and have a nice weekend
Fred
I am new to Unix C programming. I am trying to write a simple multi-process server. The server get an numberic input from keyboard and start a new child process, the child process does nothing but sleep for a few seconds and then exit. The parent process return to accept more input without waiting for the child process to exit.
The problem that I have is that after I fork() for a couple of times, that is acceptting inputs from keyboard for two or three times, the parent process begins to create process automatically without user input.
Here is the code that I wrote.
#define MAX_PROCESS 5;
static int current_process_num=0;
void install_sig_handler();
void sig_handler(int sig);
int main() {
int seconds;
int i;
install_sig_handler();
printf("pid: %d--enter seconds ", getpid());
fflush(stdout);
for(; {
scanf("%d", &seconds);
if (current_process_num < MAX_PROCESS){
current_process_num++;
switch (fork()){
case -1:
fprintf(fp,"ERROR! Can not spawn child process\n"
exit(1);
case 0:
for (i=0;i<seconds;i++){
sleep (1);
}
_exit(0);
default:
break;
}
}
else {//has reach the limit
printf("has reached the limit, please try again\n"
fflush(stdout);
}
}
return 0;
}
void install_sig_handler(){
struct sigaction sig_struct;
sig_struct.sa_handler=sig_handler;
sigemptyset(&sig_struct.sa_mask);
sig_struct.sa_flags=0;
if(sigaction(SIGCHLD, &sig_struct, NULL)!=0){
perror("error"
exit(1);
}
}
void sig_handler(int sig){
pid_t pid;
int status;
while ((pid=waitpid(-1, &status, WNOHANG))>0){
current_process_num--;
// printf("one child has exited, and the current process num is %d\n",current_process_num);
}
}
Thanks for your kindly help and have a nice weekend
Fred