C snipt.info - Index - Arhiva

download | new post | emilian_ex1 on April Mon 13th 1:33 PM - Never Expires

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4.  
  5. int main(int argc,char* argv[])
  6. {
  7.     int n,nr,aux,j,i,pid;
  8.     FILE *f1,*f2,*f3;
  9.     f1 = fopen("f1i.txt","w");
  10.     f2 = fopen("f2i.txt","w");
  11.     f3 = fopen("f3i.txt","w");
  12.     if(f1==NULL || f2==NULL || f3==NULL)
  13.     {
  14.         printf("EROARE LA SCRIEREA IN FISIER(E) !!!\n");
  15.         exit(1);
  16.     }
  17.    
  18.     aux=1;
  19.     do
  20.     {
  21.         printf("Cate numere introduceti ?\n");
  22.         scanf("%d",&n);
  23.         if(n<0 || n>30000)
  24.             printf("NU SE POT INTRODUCE ATATEA NUMERE!\n");
  25.     }while(n<0||n>30000);
  26.    
  27.     for(i=0;i<n;i++)
  28.     {
  29.         scanf("%d",&nr);
  30.         if(aux==1)
  31.             fprintf(f1,"%d ",nr);
  32.         if(aux==2)
  33.             fprintf(f2,"%d ",nr);
  34.         if(aux==3)
  35.             fprintf(f3,"%d ",nr);
  36.         aux++;
  37.         if(aux==4)
  38.             aux = 1;
  39.     }
  40.     fclose(f1);
  41.     fclose(f2);
  42.     fclose(f3);
  43.    
  44.     //am terminat de scris numerele in fisiere NOT
  45.     for(i=1;i<=3;i++)
  46.     {
  47.         if((pid=fork())==-1)
  48.         {
  49.             printf("EROARE LA FORK!!!\n");
  50.             exit(2);
  51.         }
  52.         if(0 == pid)
  53.         {
  54.             //fiu
  55.             FILE *f4;
  56.             if(i==1)
  57.             {   /*
  58.                 int s1=0,n1;
  59.                 f1 = fopen("f1i.txt","r");
  60.                 f4 = fopen("f1o.txt","w");
  61.                 while(!feof(f1))
  62.                 {
  63.                     fscanf(f1,"%d ",&n1);
  64.                     s1+=n1;
  65.                 }
  66.                 fprintf(f4,"%d",s1);
  67.                 fclose(f1);
  68.                 fclose(f4);*/
  69.                 execl("./worker","worker","f1i.txt","f1o.txt",NULL);
  70.             }
  71.             else if(i==2)
  72.             {/*
  73.                 int s2=0,n2;
  74.                 f2 = fopen("f2i.txt","r");
  75.                 f4 = fopen("f2o.txt","w");
  76.                 while(!feof(f2))
  77.                 {
  78.                     fscanf(f2,"%d ",&n2);
  79.                     //printf("!!%d!!",n2);
  80.                     s2+=n2;
  81.                 }
  82.                 fprintf(f4,"%d",s2);
  83.                 fclose(f2);
  84.                 fclose(f4);*/
  85.                 execl("./worker","worker","f2i.txt","f2o.txt",NULL);
  86.             }
  87.             else
  88.             {/*
  89.                 int s3=0,n3;
  90.                 f3 = fopen("f3i.txt","r");
  91.                 f4 = fopen("f3o.txt","w");
  92.                 while(!feof(f3))
  93.                 {
  94.                     fscanf(f3,"%d ",&n3);
  95.                     s3+=n3;
  96.                     //printf("!!%d!!",n3);
  97.                 }
  98.                 fprintf(f4,"%d",s3);
  99.                 fclose(f3);
  100.                 fclose(f4);
  101.             */
  102.             execl("./worker","worker","f3i.txt","f3o.txt",NULL);
  103.             }
  104.         }
  105.     }
  106.    
  107.     for(j=1;j<=3;j++)
  108.         wait(NULL);
  109.    
  110.    
  111.     return 0;
  112.    
  113. }

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
Syntax Highlighting:
To highlight particular lines, prefix each line with @@
Pressing TAB inserts 3 spaces