Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Предмет: ОСиСП
- Лабораторная №4
- Задание: Дерево процессов, обмен сигналами.
- Вариант:
- Таблица 1: - 7 === 1->(2,3,4,5,6) 6->(7,8)
- Таблица 2: - 6 === 1->2 SIGUSR1 2->(3,4) SIGUSR2 4->5 SIGUSR1 3->6 SIGUSR1 6->7 SIGUSR1 7->8 SIGUSR1 8->1 SIGUSR1
- Выполнил: Петрович Максим, гр. 251004
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- //#include <sys/wait.h>
- #include <signal.h>
- #include <sys/time.h>
- #include <string.h>
- #include <libgen.h>
- #include <errno.h>
- char* progname;
- static struct timeval tv;
- pid_t pid[8];
- /* _fork() - создание процесса + контроль ошибки */
- pid_t _fork();
- int main (int argc, char* argv[]) {
- progname = basename(argv[0]);
- int i,j;
- pid_t p;
- pid[0] = getpid(); // Родительский PID 0
- p = _fork(); // PID 1
- if (!p) {
- // PID 2-6
- for (i=2;i<=6;i++) {
- if (getppid() == pid[0]) {
- p = _fork();
- if (!p) {
- printf("I:%d,PID:%d\n", i, getpid());
- pid[i] = getpid();
- if (i == 6) { // PID 6
- // PID 7-8
- printf("6->\n");
- for (j=7;j<=8;j++) {
- if (getpid() == pid[6]) {
- p = _fork();
- if (!p) {
- printf("J:%d, PID:%d\n", j, getpid());
- pid[j] = getpid();
- }
- }
- }
- }
- }
- }
- }
- }
- while(1) {
- pause();
- }
- }
- pid_t _fork() {
- pid_t pid;
- pid = fork();
- if (pid != -1) {
- return pid;
- } else {
- fprintf(stderr, "%s: %s\n", progname, strerror(errno));
- exit(errno);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement