-----------------------------gre.c(生成十個文件)

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <string.h>


int foo(char *st)

{

   char ch[10]={0};

   int start=atoi(st);

   printf("****************%d\n",start);


   int fd,count=0;

   char s[5]={0};


   sprintf(ch,"%d.txt",start+1);

   fd=open(ch,O_WRONLY|O_CREAT,0644);


   if(fd<0){

       perror("open somefile");

       _exit(1);

   }

   start=start*10+1;

   while(count<10)

   {

       sprintf(s,"%3d",start);

       write(fd,s,strlen(s));

       count++;

       start++;

   }

}

int main(int argc,char *argv[])

{

   foo(argv[1]);

   

   return 0;

}

---------------------------merger.c(將生成的是個文件合併為一個)

#include <stdlib.h>

#include <sys/mman.h>

#include <fcntl.h>

#include <unistd.h>

#include <string.h>

#include <stdio.h>

#define N 10

int main(void) {

   int *p={0};

   char *q={0};

   int fd={0};

   int fw=open("data.txt",O_RDWR|O_CREAT|O_TRUNC,0644);

   int len=0,j;

   char ch[50];

   

   if(fw<0){

       perror("open hello2");

       exit(1);

   }


   for(j=0;j<N;j++)

   {

       sprintf(ch,"%d.txt",j+1);

       fd=open(ch,O_RDONLY);

       if(fd<0)

       {

           perror("open somefile");

           _exit(1);

       }


       len+=lseek(fd,0,SEEK_END)+1;

       

       close(fd);

   }


   lseek(fw,len-1,SEEK_END);

   write(fw," ",1);


   q=mmap(NULL,len,PROT_WRITE,MAP_SHARED,fw,0);

   if(q==MAP_FAILED){

       perror("mmap");

       exit(1);

   }


   for(j=0;j<N;j++)

   {

       sprintf(ch,"%d.txt",j+1);

       fd=open(ch,O_RDONLY);

       if(fd<0)

       {

           perror("open somefile");

           _exit(1);

       }


       len=lseek(fd,0,SEEK_END);


       p=mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);

       if(p==MAP_FAILED){

           perror("mmap");

           exit(1);

       }


       close(fd);

       

       memcpy(q,p,len);

       q=q+len;

       memcpy(q,"\n",1);

       q++;

       munmap(p,len+1);

   }


   close(fw);

   

   munmap(q,len);

   return 0;

}

--------------------oper.c(通過fork/execvp執行以上操作)

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <string.h>


int main(int argc,char *argv[])

{

   int count=0;

   char ch[20]={0};

   int stat_val;

   char *myargv[3]={0};

   char *meargv[2]={0};


   myargv[0]=malloc(10);

   strcpy(myargv[0],"./gre");

   

   meargv[0]=malloc(10);

   strcpy(meargv[0],"./merger");

   

   pid_t pid;

   while(count<10)

   {

       pid=fork();

       if(pid<0){

           perror("fork");

           exit(1);

       }


       printf("%d\n",pid);


       if(pid==0)

       {

           sprintf(ch,"%d",count);

           myargv[1]=malloc(5);

           strcpy(myargv[1],ch);

           execvp(myargv[0],myargv);

       }

       count++;

   }

   while(count>0)

   {

       wait(&stat_val);

       count--;

   }


   pid=fork();

   if(pid<0){

       perror("fork");

       exit(1);

   }


   printf("%d\n",pid);

   

   if(pid==0)

   execvp(meargv[0],meargv);

   

   wait(&stat_val);

   

   return 0;

}