- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void sighndlr_1(int signo) { //obrabot4ik dlya 1go processa
printf("I'm child process #1. I've got SIGINT! :) So, I won't do anything...\n"); //prosto vyvod na ekran soobscheniya o polu4enii signala
}
void sighndlr_2(int signo) { //dlhya 2go
printf("I'm child process #2. I've got SIGINT :)\n");
}
int main(int argc, char **argv) {
pid_t pid1,pid2,pid3,pid4; //4 do4ernih processa
static struct sigaction act1,act2,act3; //3 struktury dlya 3h processov
act1.sa_handler = sighndlr_1; //ustanovka obrabot4ika dlya 1 struktory
act2.sa_handler = sighndlr_2; //dlya 2i
act3.sa_handler = SIG_IGN; //ignoriruem signal v 3 strukture
switch(pid1 = fork()) { //sozdaem 1 do4erniy process
case -1: //vihodim pri oshibke sozdaniya
printf("Error fork\n");
exit(1);
break;
case 0:
printf("Child process #1 started (pid = %d)\n", getpid()); //soobshenie ob uspeshnom sozdanii i vyvod pid
sigaction(SIGINT,&act1,NULL); //ustanovka struktury obrabot4ika dlya SIGINT
for(;;) pause(); //beskone4nyi cikl
break;
default:
switch(pid2 = fork()) { //sozdaem 2 process
case -1:
printf("Error fork\n");
exit(1);
break;
case 0:
printf("Child process #2 started (pid = %d)\n", getpid());
sigaction(SIGINT,&act2,NULL); //dlya SIGINT
sigaction(SIGQUIT,&act3,NULL); //ignorim SIGQUIT
for(;;) pause();
break;
default:
switch(pid3 = fork()) { //3 process
case -1:
printf("Error fork\n");
exit(1);
break;
case 0:
printf("Child process #3 started (pid = %d)\n", getpid());
sigaction(SIGINT,&act3,NULL); //ignorim SIGINT
for(;;) pause();
break;
default:
switch(pid4 = fork()) { //4 process
case -1:
printf("Error fork\n");
exit(1);
break;
case 0:
printf("Child process #4 started (pid = %d)\n", getpid());
setsid(); //menyaem identifikator seansa dlya processa
printf("Process #4 changed sid\n");
for(;;) pause();
break;
default:
printf("Finishing parent process... (pid = %d)\n", getpid());
exit(0); //zavershaetsya roditelsky process
break;
}
break;
}
break;
}
break;
}
}
Лаба по курсу операционных систем. Нужно было создать 4 дочерних процесса, и для каждого процесса создать свои обработчики для сигнала SIGINT или SIGQUIT. Полученный говнокод полон повторяющихся конструкций, и слишком сильно запутан операторами switch-case.
Boten 30.04.2011 17:37 # 0
Lure Of Chaos 30.04.2011 17:56 # +2
Boten 30.04.2011 18:12 # 0
guest 30.04.2011 18:02 # +4
istem 30.04.2011 18:35 # 0
guest 30.04.2011 20:14 # 0