void shell_server() { int pidp; int pid; int *fifo; init_transit(0); init_data(0); if (!(fifo = (int *)malloc(2*sizeof(int)))) error("malloc error\n"); pidp = getpid(); if ((pid = fork())) { ft_pidsave(pid); return ; } pipe(fifo); recup_pipe(fifo); init_pipe_redir(); if (!(pid = fork())) { ft_pidsave(pidp); wait_sig(); } ft_pidsave(pid); signal(SIGINT, killslave); servershell(pid); }
void child(int child_id){ signal(SIGINT, sig_int); wait_sig(); lockf(STDOUT_FILENO, F_LOCK, 0); printf("child %d is terminating\n", child_id); lockf(STDOUT_FILENO, F_ULOCK, 0); }
int main(){ pid_t p1, p2; int fd_stdout = STDOUT_FILENO; signal(SIGINT, sig_int); while ((p1=fork()) == -1); if (!p1){ child(1); return 0; } while ((p2=fork()) == -1); if (!p2) { child(2); return 0; } puts("waiting SIGINT..."); wait_sig(); kill(p1, SIGINT); kill(p2, SIGINT); wait(0); wait(0); puts("parent is terminating"); return 0; }
int pipe_loop() { unsigned long check; pid_t pid; /* Hope the sleep will help the top process * finish the forkes, at least most of them*/ sleep (1); if (pipe (p1) || pipe (p2)) { perror ("pipe created failed"); exit (ERR_PIP); } if ((pid = fork()) < 0) { #ifdef DEBUG perror ("fork failed\n"); #endif exit (ERR_FRK); } if (pid > 0) { /* parent process */ #if 0 /*************************************************** * Set up the signal * only after receiving the SIGUSR1 signal * transfer is started **************************************************/ wait_sig (SIGUSR1, resume); pause (); #endif /* set up the timer */ wake_me (duration, report); /* master, write p1 & read p2 */ close(p1[0]); close(p2[1]); while (1) { if (write(p1[1], (char *)&iter, sizeof(iter)) != sizeof(iter)) { #ifdef NDEBUG if ((errno != 0) && (errno != EINTR)) perror("master write failed"); #endif exit(ERR_WRI); } if (read(p2[0], (char *)&check, sizeof(check)) != sizeof(check)) { #ifdef NDDEBUG if ((errno != 0) && (errno != EINTR)) perror("master read failed"); #endif exit(ERR_RED); } if (check != iter) { #ifdef DEBUG printf("Master sync error: expect %lu, got %lu\n", iter, check); #endif exit(ERR_SYN); } iter++; } } /* child process */ #if 0 /************************************************** * Child processes must ignore the signal SIGUSR1 * otherwise will be corrupted. Also this would * prevent overhead casued by signal handling **************************************************/ signal (SIGUSR1, SIG_IGN); #endif unsigned long iter1; iter1 = 0; /* slave, read p1 & write p2 */ close(p1[1]); close(p2[0]); while (1) { if (read(p1[0], (char *)&check, sizeof(check)) != sizeof(check)) { #ifdef NDEBUG if ((errno != 0) && (errno != EINTR)) perror("slave read failed"); #endif exit(ERR_RED); } if (check != iter1) { #ifdef DEBUG printf("Slave sync error: expect %lu, got %lu\n", iter1, check); #endif exit(ERR_SYN); } if (write(p2[1], (char *)&iter1, sizeof(iter1)) != sizeof(iter1)) { #ifdef NDEBUG if ((errno != 0) && (errno != EINTR)) perror("slave write failed"); #endif exit(ERR_WRI); } iter1++; } return 0; }