Skip to content

Instantly share code, notes, and snippets.

@Hajto
Created November 29, 2017 10:49
Show Gist options
  • Save Hajto/a9624e4335bfc5496676f36e6e4755ab to your computer and use it in GitHub Desktop.
Save Hajto/a9624e4335bfc5496676f36e6e4755ab to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main() {
int pm = getpid();
char polecenie_1[20];
int i;
int id, e, w;
sprintf(polecenie_1, "pstree -Ap %d", pm);
printf("\n");
printf("Oto proces macierzysty: PID: %d, PPID: %d, UID: %d, GID: %d \n", getpid(), getppid(), getuid(), getgid());
for (i = 0; i < 3; i++) {
id = fork();
switch (id) {
case -1:
perror("FORK ERROR");
exit(1);
case 0:
e = execl("/bin/echo", "echo", "Działa", NULL);
if (e == -1) {
perror("EXECL ERROR");
exit(2);
}
break;
}
}
for (i = 0; i < 3; i++) {
w = wait(&pm);
if (w == -1) {
perror("WAIT ERROR");
exit(3);
}
printf("Potomek: %d Kod powrotu: %d\n", w, pm);
}
if (getpid() == pm) {
system(polecenie_1);
} else
sleep(3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment