Added 13,15

This commit is contained in:
Dawid Pietrykowski 2022-11-24 21:32:56 +01:00
parent f1abb7ec54
commit 2e5758eb98
5 changed files with 119 additions and 0 deletions

28
processes/.vscode/tasks.json vendored Normal file
View File

@ -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"
}

BIN
processes/13 Executable file

Binary file not shown.

50
processes/13.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
/* 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ć 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);
}
}
}
}

BIN
processes/15 Executable file

Binary file not shown.

41
processes/15.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
/* 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;
}