3. Procesy

3.1. Tworzenie procesu

Listing 3.1: Przykład działania funkcji fork() (str. 16)

#include <stdio.h>

main(){
   printf("Poczatek\n");
   fork();
   printf("Koniec\n");
}

3.2. Uruchamianie programu

Listing 3.2: Przykład działania funkcji exec() (str. 17)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   printf("Poczatek\n");
   execlp("ls", "ls", "-a", NULL);
   printf("Koniec\n");
}

3.3. Synchronizacja przodka z potomkiem

Listing 3.3: Przykład uruchamiania programów bez synchronizacji przodka z potomkiem (str. 17)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   printf("Poczatek\n");
   if (fork() == 0){
      execlp("ls", "ls", "-a", NULL);
      perror("Blad uruchmienia programu");
      exit(1);
   }
   printf("Koniec\n");
}

Listing 3.4: Przykład uruchamiania programów z synchronizacja przodka z potomkiem (str. 18)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   printf("Poczatek\n");
   if (fork() == 0){
      execlp("ls", "ls", "-a", NULL);
      perror("Blad uruchmienia programu");
      exit(1);
   }
   wait(NULL);
   printf("Koniec\n");
}

Listing 3.5: Przykład uruchamiania programów z kontrolą poprawności (str. 18)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   printf("Poczatek\n");
   switch (fork()){
      case -1: 
         perror("Blad utworzenia procesu potomnego");
         break;
      case 0: /* proces potomny */
         execlp("ls", "ls", "-a", NULL);
         perror("Blad uruchmienia programu");
         exit(1);
      default: /* proces macierzysty */
         if (wait(NULL) == -1)
            perror("Blad w oczekiwaniu na zakonczenie potomka");
   }
   printf("Koniec\n");
}

Listing 3.6: Przykład działania funkcji wait() w przypadku naturalnego zakończenia procesu (str. 19)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   int pid1, pid2, status;

   pid1 = fork();
   if (pid1 == 0) /* proces potomny */
      exit(7);
   /* proces macierzysty */
   printf("Mam potomka o identyfikatorze %d\n", pid1);
   pid2 = wait(&status);
   printf("Status zakonczenia procesu %d: %x\n", pid2, status);
}

Listing 3.7: Przykład działania funkcji wait() w przypadku zabicia procesu (str. 19)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   int pid1, pid2, status;

   pid1 = fork();
   if (pid1 == 0){ /* proces potomny */
      sleep(10);
      exit(7);
   }
   /* proces macierzysty */
   printf("Mam potomka o identyfikatorze %d\n", pid1);
   kill(pid1, 9);
   pid2 = wait(&status);
   printf("Status zakonczenia procesu %d: %x\n", pid2, status);
}

3.4. Przeadresowanie standardowego wyjścia

Listing 3.8: Przykład przeadresowania standardowego wyjścia (str. 20)

#include <stdio.h>
#include <unistd.h>

main(int argc, char* argv[]){
   close(1);
   creat("ls.txt", 0600);
   execvp("ls", argv);
}

3.5. Sierota i zombi

Listing 3.9: Utworzenie sieroty (str. 20)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main(){
   if (fork() == 0){
      sleep(30);
      exit(0);
   }
   exit(0);
}

Listing 3.10: Utworzenie zombi (str. 21)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){
   if (fork() == 0)
      exit(0); 
   sleep(30);   
   wait(NULL);
}

3.6. Zadania

  1. Jaki będzie wynik wykonania programu z listingu 3.1, jeśli zamiast jednego wywołania funkcji fork() byłyby dwa kolejne wywołania tej funkcji.

  2. Jaki będzie wynik wykonania poniższego programu (co zostanie wypisane na standardowym wyjściu), gdy:

    1. CZAS_POTOMKA jest istotnie większy od CZAS_RODZICA
    2. CZAS_POTOMKA jest istotnie mniejszy od CZAS_RODZICA?
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define CZAS_POTOMKA (?)
    #define CZAS_RODZICA (?)
    
    int main() {
       int pid;
       pid = fork();
       if ( pid == 0 ){
          sleep( CZAS_POTOMKA );
          exit(7);
       }
       else {
          int status;
          sleep( CZAS_RODZICA );
          kill( pid, 9 );
          wait( &status );
          printf( "Status = %x\n", status );
       }
    }
    

Spis treści

Poprzedni temat

2. Pliki

Następny temat

4. Łącza

Ta strona