Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- typedef struct{
- int* vector;
- int numar_aparitii;
- }memorie_partajata;
- //memorie_partajata.numar_aparitii=0;
- int temp = 0;
- void split(int *stanga,int *dreapta,pid_t* fiu_stanga,pid_t* fiu_dreapta){
- printf("--------------stanga si dreapta:%d, %d----------\n",*stanga,*dreapta);
- int temp = 0;
- if(*stanga<*dreapta){
- if((*stanga)==(*dreapta))
- return;
- else if(*dreapta-*stanga==1){
- temp=*dreapta;
- *dreapta=*stanga;
- if(*fiu_stanga=fork()){
- *stanga=temp;
- *dreapta=temp;
- if(*fiu_dreapta=fork()){
- }
- waitpid(*fiu_stanga,NULL,0);
- waitpid(*fiu_stanga,NULL,0);
- split(stanga,dreapta,fiu_stanga,fiu_dreapta);
- }
- }else{
- temp=*dreapta;
- *dreapta=(*dreapta+(*stanga))/2;
- if(*fiu_stanga=fork()){
- *stanga=(*dreapta)+1;
- *dreapta=temp;
- if(*fiu_dreapta=fork()){
- }
- }
- waitpid(*fiu_stanga,NULL,0);
- waitpid(*fiu_stanga,NULL,0);
- split(stanga,dreapta,fiu_stanga,fiu_dreapta);
- }
- }
- }
- int main(int argc, char *argv[])
- {
- pid_t parinte=getpid();
- int stanga=1;
- int dreapta=10;
- int temp=0;
- pid_t fiu_stanga;
- pid_t fiu_dreapta;
- eticheta:
- split(&stanga,&dreapta,&fiu_stanga,&fiu_dreapta);
- waitpid(fiu_stanga,NULL,0);
- waitpid(fiu_stanga,NULL,0);
- if(fiu_dreapta!=0 && fiu_stanga!=0){
- printf("sunt in parinte, cu pidul: %d si %d si %d\n",parinte,fiu_stanga,fiu_dreapta);
- }else
- printf("sunt in forkul cu stanga: %d si dreapta: %d pentru procesul cu: %d\n",stanga,dreapta,getpid());
- return 0;
- }
- /*
- * int valoare = 0;
- pid_t parinte = getpid();
- pid_t fiu = fork();
- * if(fiu!=0){
- printf("sunt in parinte, cu pidul: %d\n",parinte);
- }else{
- printf("sunt in fiu, cu pidul: %d\n",getpid());
- printf("pidul parintelui: %d\n",parinte);
- }
- */
- /*
- * if(fiu_stanga=fork()){
- if(fiu_dreapta=fork()){
- }
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement