diff --git a/processes/.vscode/tasks.json b/processes/.vscode/tasks.json new file mode 100644 index 0000000..08d9005 --- /dev/null +++ b/processes/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/processes/13 b/processes/13 new file mode 100755 index 0000000..f4fa27a Binary files /dev/null and b/processes/13 differ diff --git a/processes/13.c b/processes/13.c new file mode 100644 index 0000000..9bbc6a7 --- /dev/null +++ b/processes/13.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +/* Program tworzy proces potomny, potem tworzy proces potomny procesu potomnego. Proces +macierzysty zmiania standardową funkcję obsługi sygnału wywoływanego przy zakończeniu +procesu potomnego. Proszę zwrócić uwagę na fakt, że niestandardowa funkcja obsługi sygnału +jest ważna tylko do pierwszego jej użycia, potem jest używana ponownie standardowa funkcja +obsługi sygnału. Aby ciągle była używana niestandardowa funcja obsługi sygnału, należy w +niestandardowej funkcji obsługi sygnału rejestrować ją jako funkcję obsługi sygnału dla tego +konkretnego sygnału. +*/ +void dziecko(int i) +{ + printf("Dostalem sygnal, numer procesu %d. i:%d\n", getpid(), i); + wait(NULL); + // signal(SIGCHLD, dziecko); +} +int main(int argc, char *argv[]) +{ + int pid = 0, ppid; + ppid = getpid(); + printf("Przed wywolaniem funkcji fork. Rodzic ma numer %d.\n", ppid); + signal(SIGCHLD, dziecko); + if ((pid = fork()) == -1) + { + perror("Pomyslne zakonczenie funkcji fork nie jest mozliwe.\n"); + exit(1); + } + else + { + if (pid != 0) + { + sleep(10); + } + else + { + printf("Dziecko ma numer %d.\n", getpid()); + sleep(2); + pid = fork(); + if (pid != 0) + { + printf("Dziecko dziecka ma numer %d.\n", pid); + sleep(2); + } + } + } +} \ No newline at end of file diff --git a/processes/15 b/processes/15 new file mode 100755 index 0000000..fa2aa8b Binary files /dev/null and b/processes/15 differ diff --git a/processes/15.c b/processes/15.c new file mode 100644 index 0000000..4c48a0e --- /dev/null +++ b/processes/15.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +/* Program tworzy proces potomny i uruchamia zadane polecenie w ramach procesu potomnego. +Przykladowe wywolanie programu: a.out cal 10 2024. */ +int main(int argc, char **argv) +{ + char **polecenie; + long i = 0; + int pid; + polecenie = (char **)malloc(sizeof(char *) * argc); + while (i < argc - 1) + { + polecenie[i] = argv[i + 1]; + i++; + } + polecenie[i] = NULL; + if ((pid = fork()) == -1) + { + perror("Pomyslne zakonczenie funkcji fork nie jest mozliwe.\n"); + exit(1); + } + else + { + if (pid == 0) + { + execvp(polecenie[0], polecenie); + } + else + { + waitpid((pid_t)-1, NULL, 0); + printf("\nPolecenie wraz z argumentami zostalo wykonane.\n"); + } + } + printf("polecenie addr: %d\n", getppid()); + free(polecenie); + return 0; +} \ No newline at end of file