process - Synchronization of parent and child with SIGUSR signal in C. Make parent and child read one after the other -
i have created 2 way communication between parent , child processes using 2 pipes. parent , child write data , able make them read data each other. parent writes numbers 1 5, , child writes numbers 6 10. want parent start reading data first, , reading continues in order switching parent child until data read: 6,1,7,2,8,3,9,4,10,5. have tried synchronize reading sigusr1 when parent reading second time program stops. have searched lot find problem can be, , tried tips , alike working examples, nothing seems help. here code:
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <signal.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> void paction(int dummy) { printf("p*************************************************\n"); } void caction(int dummy) { printf("c*************************************************\n"); } int main() { int pfd[2]; int pfd2[2]; pid_t cfork, pfork; if (pipe(pfd) == -1 || pipe(pfd2) == -1) { fprintf(stderr,"pipe failed"); exit(1); } cfork = fork(); signal(sigusr1, paction); if (cfork == -1) { printf("fork failed\n"); exit(1); } else if (cfork > 0) { /*parent process*/ int numbers[] = {1, 2,3, 4, 5}; int numbers2[] = { 6, 7,8, 9, 10 }; close(pfd[0]); /*close read end, write , close write end*/ /*write part*/ int limit = 5; int i; (i = 0; < limit; i++) { printf("parent sends: %d\n", numbers[i]); write(pfd[1], &numbers[i], sizeof(numbers[i])); printf("child sends: %d\n", numbers2[i]); write(pfd2[1], &numbers2[i], sizeof(numbers2[i])); } printf("***************************************************\n"); close(pfd[1]); close(pfd2[1]); /*read part/////////////////////////////////////////*/ int temp; int reads = 5; int j; (j = 0; j < reads; j++) { sleep(1); read(pfd2[0], &temp, sizeof(temp)); printf("parent gets: %d\n", temp); kill(cfork, sigusr1); pause(); } /*printf("***************************************************\n");*/ kill( cfork, sigusr1 ); close(pfd2[0]); } else { /*child process*/ signal(sigusr1, caction); close(pfd[1]); int temp; int reads = 5; int j; pfork = getppid(); (j = 0; j < reads; j++) { sleep(1); read(pfd[0], &temp, sizeof(temp)); printf("child gets: %d\n", temp); kill(getppid(), sigusr1); pause(); } /*printf("***************************************************\n");*/ close(pfd[0]); close(pfd2[0]); } return 0;
}
my output looks this:
> parent sends:1 > child sends:6 > parent sends:2 > child sends:7 > parent sends:3 > child sends:8 > parent sends:4 > child sends:9 > parent sends:5 > child sends:10 > ************************************************************** parent gets:6 > c************************************************************ > child gets:1 > p************************************************************* > parent gets:7
and here when stops. if can me appreciate because want know problem is, , since beginner in c programming , processes! thank in advance
printf()
not async-safe function. calling printf()
in both normal code , signal handler cause undefined behavior. in particular, printf()
may need take lock on output-stream, while taking locks in signal-handlers very inadvisable (risk of self-deadlock).
Comments
Post a Comment