|
以下のプログラムは親プロセスと子プロセスの同期をとって、
親プロセス(int input_func())で,
入力して、
子プロセス(int process_func())で、
出力をするプログラムです。
しかし、子プロセスで出力したいのですが、何も出力されません。
どなたか、アドバイスお願いします。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <setjmp.h>
#define INPUTLEN 100
char input[INPUTLEN];
char temp_file[] = {"tmp"};
int pid , ppid;
int status;
FILE *fp;
struct sigaction parent_action, child_action;
int input_func();
int process_func();
void int_handler(int signal);
int main(int argc , char *argv[]){
if ( (fp = fopen(temp_file , "w")) == NULL){
fprintf(stderr , "Cannot create temp\n");
return EXIT_FAILURE;
}
parent_action.sa_handler = int_handler;
parent_action.sa_flags = SA_RESTART;
sigaction(SIGUSR1 , &parent_action , NULL);
switch(pid = fork()){
case -1:
perror("Cannot create fork()\n");
exit(1);
case 0:
process_func();
default:
input_func();
}
return EXIT_SUCCESS;
}
int input_func(){
while (1){
printf ("Enter Line: ");
if((fgets(input, INPUTLEN , stdin))==NULL)
exit(1);
fprintf(fp , "%s\n" , input);
if (strncmp(input , "quit" , 4) == 0){
exit(1);
}
kill(pid , SIGUSR1);
pause();
}
return EXIT_SUCCESS;
}
int process_func(){
child_action.sa_handler = int_handler;
sigaction(SIGUSR1, &child_action , NULL);
ppid = getppid();
while(1){
kill(pid , SIGUSR1);
pause();
sigaction(SIGCHLD , &child_action, NULL);
sigaction(SIGUSR1 , &child_action , NULL);
fgets(input , INPUTLEN , fp);
printf ("Line Processed: %s\n" , input);
}
return EXIT_SUCCESS;
}
void int_handler(int signal){
}
|