Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main(){
- printf("Mój PID = %d\n",getpid()); //indetyfikator procesu
- //errno=-1;
- printf("Error in fork = %d %s\n", errno,strerror(errno));
- pid_t pid;
- int i, iloscpowt = 10000, nieskon = 0;
- for(i = 0; i < iloscpowt; i++)
- switch (pid = fork())
- {
- case -1: // błąd
- printf("Błąd funkcji fork() = %d, %s\n", errno, strerror(errno));
- case 0: // proces potomny
- printf("proces potomny, mój PID = %d\n", getpid());
- printf("proces potomny, PID rodzica = %d\n", getppid());
- printf("proces potomny, wartość wywołania fork() = %d\n", pid);
- while(1)
- {
- printf("Licze do nieskonczonosci %d\n",nieskon);
- nieskon++;
- puts("p");
- sleep(1);
- }
- return 0;
- default: // proces macierzysty
- printf("proces macierzysty, mój PID = %d\n", getpid());
- printf("proces macierzysty, wartość wywołania fork() = %d\n", pid);
- while(1)
- {
- puts("m");
- sleep(1);
- }
- //sleep(2);
- }
- for(i = 0; i < iloscpowt; i++)
- if(wait(0)==-1)
- printf("Błąd funkcji fork() = %d, %s\n", errno, strerror(errno));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment