Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/param.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <sys/wait.h>
- #include <sys/signal.h>
- #include <errno.h>
- pid_t child_proc_1,child_proc_2, child_proc_3;
- int *pids_array;
- void add_processes()
- {
- child_proc_1 = fork();
- if (child_proc_1 == 0)
- return;
- child_proc_2 = fork();
- if (child_proc_2 == 0)
- return;
- child_proc_3 = fork();
- if (child_proc_3 == 0)
- return;
- }
- int is_initial_process()
- {
- if ((child_proc_1 > 0) &&
- (child_proc_2 > 0) &&
- (child_proc_3 > 0) )
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void create_n_processes(int n)
- {
- pids_array = (int *) calloc(n, sizeof(int));
- pid_t current_proc;
- for(int i=0; i<n; i++)
- {
- current_proc = fork();
- if(current_proc == 0)
- {
- printf("I am %d process with PID %d and PPID %d \n", i, getpid(), getppid());
- return;
- }
- else if(current_proc > 0)
- pids_array[i] = current_proc;
- }
- }
- void child_proc_1_handler(int signum, siginfo_t *info, void *f) {
- if (info->si_code == SI_QUEUE)
- {
- union sigval value;
- value.sival_int = strlen(info->si_value.sival_ptr);
- printf("The length of string is %d \n", value.sival_int);
- sigqueue(getppid(),SIGCHLD,value);
- }
- return;
- }
- void child_proc_2_handler(int signum, siginfo_t *info, void *f) {
- if (info->si_code == SI_QUEUE)
- {
- int n = abs((info->si_value.sival_int)-20);
- printf("I am going to create %d processes \n", n);
- create_n_processes(n);
- }
- return;
- }
- void received_proc_1_handler(int signum, siginfo_t *info, void *f) {
- if (info->si_code == SI_QUEUE)
- {
- union sigval value;
- value.sival_int = info->si_value.sival_int;
- printf("I am PARENT AND I AGREE THAT the length of string is %d \n", value.sival_int);
- sigqueue(child_proc_2,SIGUSR2,value);
- sleep(10);
- }
- return;
- }
- int main(int argc, const char **argv)
- {
- if (argc != 2)
- {
- printf("Not enough arguments... \n");
- exit(EXIT_FAILURE);
- }
- char *str = argv[1];
- struct sigaction act1;
- memset(&act1, 0, sizeof(act1));
- act1.sa_sigaction = child_proc_1_handler;
- sigemptyset(&act1.sa_mask);
- sigaddset(&act1.sa_mask, SIGUSR1);
- act1.sa_flags = SA_SIGINFO;
- sigaction(SIGUSR1,&act1,NULL);
- struct sigaction receivedact1;
- memset(&receivedact1, 0, sizeof(receivedact1));
- receivedact1.sa_sigaction = received_proc_1_handler;
- sigemptyset(&receivedact1.sa_mask);
- sigaddset(&receivedact1.sa_mask, SIGCHLD);
- receivedact1.sa_flags = SA_SIGINFO;
- sigaction(SIGCHLD,&receivedact1,NULL);
- struct sigaction act2;
- memset(&act2, 0, sizeof(act2));
- act2.sa_sigaction = child_proc_2_handler;
- sigemptyset(&act2.sa_mask);
- sigaddset(&act2.sa_mask, SIGUSR2);
- act2.sa_flags = SA_SIGINFO;
- sigaction(SIGUSR2,&act2,NULL);
- add_processes();
- if (is_initial_process() != 0)
- {
- union sigval value;
- value.sival_ptr = str;
- printf("1: %d ; 2:%d ; 3:%d ; \n", child_proc_1, child_proc_2, child_proc_3);
- sigqueue(child_proc_1,SIGUSR1,value);
- sleep(10);
- int k = killpg(0,SIGTERM);
- }
- else if (child_proc_1 == 0)
- {
- while(1)
- {
- }
- }
- else if (child_proc_2 == 0)
- {
- while(1)
- {
- }
- }
- else if (child_proc_3 == 0)
- {
- signal(SIGTERM,SIG_IGN);
- while(1)
- {
- }
- }
- else
- {
- printf("Loshara");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement