i write code, should works follow: creating child process, send him n real time signals (e.g. sigrtmin) , after other real time signal (e.g. sigrtmax). child process should have suitable signal handler send sigrtmin signals, , after sigrtmax, should end. problem is, if send child 10 signals, 1 time receive in parent 10 signals , 1 time 0 , 1 time 1 signal. expect receive many signals send. can please advice? parent:
#define _bsd_source #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <time.h> #include <sys/types.h> #include <sys/time.h> #include <sys/resource.h> #include <sys/wait.h> #include <unistd.h> int requests, child_pid, income_signal_counter = 0; struct sigaction new; union sigval sigval_tmp; void generatechild(){ int pid = fork(); if (pid == 0){ if(execvp("./child", null) == -1){ printf("sth goes wrong\n"); exit(1); } exit(0); }else if(pid > 0){ usleep(100000); child_pid = pid; } } void sendsignalsbykillwithrtsignals(){ int signal_sent = 0; for(int =0; i<requests; i++){ if(kill(child_pid, sigrtmin)== 0){ signal_sent ++; } } printf("(parent)wyslanych sygnalow sigrtmin: %d\n", signal_sent); kill(child_pid, sigrtmax); } void rtminsignalhandler(){ income_signal_counter++; // printf("(parent)liczba otrzymanych sigrtmin: %d/%d\n", income_signal_counter, requests); } int main(int argc, char *argv[]){ requests = atoi(argv[1]); int func_type = atoi(argv[2]); generatechild(); struct sigaction new; new.sa_handler = &rtminsignalhandler; new.sa_flags = 0; sigaction(sigrtmin, &new, null); sendsignalsbykillwithrtsignals(); int wstatus; wait(&wstatus); if(wifexited(wstatus) && wexitstatus(wstatus) != 0){ printf("proces potomny zdechl\n"); exit(2); } printf("(parent)signal sigrtmin received: %d/%d\n", income_signal_counter, requests); return 0; }
child:
#define _bsd_source #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <unistd.h> volatile int parent_id; void signalhandler(int signal){ kill(parent_id, signal); } void exitsignalhandler(int signal){ _exit(0); } int main(int argc, char *argv[]){ parent_id = getppid(); struct sigaction new; new.sa_handler = &signalhandler; new.sa_flags = 0; sigaction(sigrtmin, &new, null); new.sa_handler = &exitsignalhandler; new.sa_flags = 0; sigaction(sigrtmax, &new, null); while(1){ } return 0; }
Comments
Post a Comment