Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <string.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <time.h>
- #include <sys/time.h>
- int usr1AmountSend = 0;
- int usr2AmountSend = 0;
- int usr1AmountAccept = 0;
- int usr2AmountAccept = 0;
- int currProc;
- void termProc(int sig){
- printf("(%d) PID - %d (PPID - %d) finished work after %d signal SIGUSR1 and %d signal SIGUSR2\n",
- currProc, getpid(), getppid(), usr1AmountSend,
- usr2AmountSend);
- for (;;) {
- pid_t childPid = wait(NULL);
- if (childPid == -1) {exit(EXIT_SUCCESS);}
- }
- }
- void usr1Proc(int sig){usr1AmountAccept++;}
- void usr2Proc(int sig){usr2AmountAccept++;}
- int main(){
- signal(SIGTERM, termProc);
- signal(SIGUSR1, usr1Proc);
- signal(SIGUSR2, usr2Proc);
- pid_t pid_array[9];
- currProc = 0;
- pid_array[currProc] = getpid();
- pid_t pid = fork();// process #1
- if (pid == 0){
- currProc = 1; // process #1
- pid_array[currProc] = getpid();
- pid = fork(); // process #2
- if (pid == 0){
- currProc = 2; // process #2
- pid_array[currProc] = getpid();
- pid = fork(); // process #3
- if (pid == 0){
- currProc = 3; // process #3
- pid_array[currProc] = getpid();
- pid = fork(); // process #7
- if (pid == 0){currProc = 7; pid_array[currProc] = getpid();} // process #7
- } else {
- pid = fork(); // process #4
- if (pid == 0){
- currProc = 4; // process #4
- pid_array[currProc] = getpid();
- pid = fork(); // process #6
- if (pid == 0){currProc = 6; pid_array[currProc] = getpid();} // process #6
- } else {
- pid = fork(); // process #5
- if (pid == 0){
- currProc = 5; // process #5
- pid_array[currProc] = getpid();
- pid = fork(); // process #8
- if (pid == 0){currProc = 8; pid_array[currProc] = getpid();} // process #8
- }
- }
- }
- }
- for (int i = 0; i < 101; i++)
- {
- switch (currProc)
- {
- case 1:
- kill(pid_array[2], SIGUSR2);
- usr2AmountSend++;
- break;
- case 2:
- kill(0, SIGUSR1);
- usr1AmountSend++;
- break;
- case 3:
- kill(pid_array[7], SIGUSR1);
- usr1AmountSend++;
- break;
- case 4:
- kill(pid_array[6], SIGUSR1);
- usr1AmountSend++;
- break;
- case 5:
- kill(pid_array[8], SIGUSR1);
- usr1AmountSend++;
- break;
- case 8:
- kill(pid_array[1], SIGUSR2);
- usr2AmountSend++;
- break;
- }
- struct timeval currentTime;
- gettimeofday(¤tTime, NULL);
- char buffer[80];
- strftime(buffer, sizeof(buffer), "%H:%M:%S",
- localtime(¤tTime.tv_sec));
- sprintf(buffer, "%s.%06ld", buffer, currentTime.tv_usec);
- printf("[%s] %d process (PID - %d|PPID - %d) send/accept USR1 (%d / %d) and USR2 (%d / %d)\n\n",
- buffer, currProc, getpid(), getppid(), usr1AmountSend, usr1AmountAccept, usr2AmountSend, usr2AmountAccept);
- }
- kill(0, SIGTERM);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment