Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. //creare 2 procese tata fiu
  2. //procesul fiu sa afiseze mesaje continuu pana cand tatal primeste de la tastatura un mesaj
  3. //secventa din fiu se executa de catre un fir executat de fir
  4.  
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <pthread.h>
  12.  
  13.  
  14. int main(int argc, char *argv[], char **env)
  15. {
  16.  
  17.  
  18. pid_t fiu;
  19. int pipe2[2];
  20.  
  21. char* msgFiu = "Salut tata!";
  22.  
  23.  
  24. pipe(pipe2); //pipe fiu-tata
  25.  
  26.  
  27. fiu = fork();
  28.  
  29. if(fiu == -1)
  30. {
  31. printf("Eroare fork! Nu s-a putut crea procesul fiu! \n");
  32. return -1;
  33. }//fiu eroare
  34.  
  35. if (fiu == 0)//fiu
  36. {
  37.  
  38. pthread_t thr1;
  39.  
  40.  
  41.  
  42. //pthread_create(&thr1, NULL, (void *)&afis, NULL); //creez fir 1
  43. //sleep(1);
  44.  
  45. while(1)
  46. {
  47. printf("FIUL a trimis mesajul ( %s ) catre tata!\n", msgFiu);
  48. sleep(2);
  49. printf("\n");
  50. }
  51.  
  52. //trimit mesaj fiu catre tata
  53. write(pipe2[1], msgFiu, strlen(msgFiu)*sizeof(char));
  54.  
  55.  
  56. printf("\n");
  57. printf("Inchidere program...\n");
  58. exit(0);
  59.  
  60.  
  61. }
  62.  
  63.  
  64. //tata
  65. else
  66. {
  67. char *buffer;
  68. int ch;
  69. //alocare spatiu pt mesaj fiu
  70. buffer = malloc(strlen(msgFiu)*50);
  71.  
  72. //citire mesaj fiu
  73. read(pipe2[0], buffer, strlen(msgFiu)*50);
  74.  
  75.  
  76. sleep(3);
  77. printf("TATAL a primit de la fiu mesajul: %s\n", buffer);
  78. printf("\n");
  79.  
  80.  
  81. sleep(5);
  82.  
  83. }
  84. return 0;
  85. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement