Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. typedef struct{
  6.     int* vector;
  7.     int numar_aparitii;
  8. }memorie_partajata;
  9. //memorie_partajata.numar_aparitii=0;
  10. int temp = 0;
  11. void split(int *stanga,int *dreapta,pid_t* fiu_stanga,pid_t* fiu_dreapta){
  12.     printf("--------------stanga si dreapta:%d, %d----------\n",*stanga,*dreapta);
  13.     int temp = 0;
  14.     if(*stanga<*dreapta){
  15.         if((*stanga)==(*dreapta))
  16.             return;
  17.         else if(*dreapta-*stanga==1){
  18.             temp=*dreapta;
  19.             *dreapta=*stanga;
  20.             if(*fiu_stanga=fork()){
  21.                 *stanga=temp;
  22.                 *dreapta=temp;
  23.  
  24.                     if(*fiu_dreapta=fork()){
  25.  
  26.                     }
  27.                     waitpid(*fiu_stanga,NULL,0);
  28.                     waitpid(*fiu_stanga,NULL,0);
  29.                 split(stanga,dreapta,fiu_stanga,fiu_dreapta);
  30.             }
  31.  
  32.         }else{
  33.             temp=*dreapta;
  34.             *dreapta=(*dreapta+(*stanga))/2;
  35.             if(*fiu_stanga=fork()){
  36.                 *stanga=(*dreapta)+1;
  37.                 *dreapta=temp;
  38.                     if(*fiu_dreapta=fork()){
  39.  
  40.                     }
  41.                 }
  42.             waitpid(*fiu_stanga,NULL,0);
  43.             waitpid(*fiu_stanga,NULL,0);
  44.             split(stanga,dreapta,fiu_stanga,fiu_dreapta);
  45.         }
  46.  
  47.     }
  48.  
  49. }
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53.     pid_t parinte=getpid();
  54.     int stanga=1;
  55.     int dreapta=10;
  56.     int temp=0;
  57.     pid_t fiu_stanga;
  58.     pid_t fiu_dreapta;
  59.     eticheta:
  60.     split(&stanga,&dreapta,&fiu_stanga,&fiu_dreapta);
  61.     waitpid(fiu_stanga,NULL,0);
  62.     waitpid(fiu_stanga,NULL,0);
  63.  
  64.  
  65.  
  66.  
  67.     if(fiu_dreapta!=0 && fiu_stanga!=0){
  68.  
  69.  
  70.  
  71.         printf("sunt in parinte, cu pidul: %d si %d si %d\n",parinte,fiu_stanga,fiu_dreapta);
  72.     }else
  73.         printf("sunt in forkul cu stanga: %d si dreapta: %d pentru procesul cu: %d\n",stanga,dreapta,getpid());
  74.  
  75.  
  76.     return 0;
  77. }
  78. /*
  79.  *  int valoare = 0;
  80.     pid_t parinte = getpid();
  81.     pid_t fiu = fork();
  82.  *   if(fiu!=0){
  83.         printf("sunt in parinte, cu pidul: %d\n",parinte);
  84.  
  85.     }else{
  86.         printf("sunt in fiu, cu pidul: %d\n",getpid());
  87.         printf("pidul parintelui: %d\n",parinte);
  88.  
  89.     }
  90.     */
  91. /*
  92.  * if(fiu_stanga=fork()){
  93.         if(fiu_dreapta=fork()){
  94.  
  95.         }
  96.     }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement