Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/sem.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/wait.h>
  9.  
  10.  
  11. int semafor;
  12.  
  13. static void usun_semafor(void);
  14. static void utworz_nowy_semafor();
  15.  
  16. int main(){
  17. pid_t id;
  18.  
  19.  
  20.  
  21. utworz_nowy_semafor();
  22.  
  23. id = fork();
  24. if(id == 0) execl("./plik1", "plik1", NULL);
  25.  
  26. id = fork();
  27. if(id == 0) execl("./plik2", "plik2", NULL);
  28.  
  29.  
  30. id = fork();
  31. if(id == 0) execl("./plik3", "plik3", NULL);
  32.  
  33.  
  34. for(int i = 0; i < 3; i++) {
  35. if(wait(NULL) == -1) perror("blad wait");
  36.  
  37. }
  38.  
  39.  
  40. usun_semafor();
  41. system("ipcs -s");
  42. printf("\nprogram napisal: Krystian Labaj.\n");
  43.  
  44. return 0;
  45. }
  46.  
  47.  
  48. static void utworz_nowy_semafor()
  49. {
  50. semafor=semget(31,5,0777|IPC_CREAT);
  51. if (semafor==-1)
  52. {
  53. printf("Nie moglem utworzyc nowego semafora.\n");
  54. exit(EXIT_FAILURE);
  55. }
  56. else
  57. {
  58. printf("Semafor zostal utworzony : %d\n",semafor);
  59. }
  60. }
  61.  
  62.  
  63. static void usun_semafor(void){
  64. int sem;
  65. sem = semctl(semafor,0,IPC_RMID);
  66. if(sem == -1)
  67. {
  68. printf("Nie mozna usunac semafora.\n");
  69. exit(EXIT_FAILURE);
  70. }
  71. else{
  72. printf("Semafor zostal usuniety: %d\n",sem);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement