#include <myhead.h>
long se(FILE *fp)
{
fseek(fp, 0L, SEEK_END);
long i = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return i;
}
void ch1(FILE *a, FILE *b)
{
long i = se(a);
long j = i / 2;
char buff[1024];
size_t rd;
fseek(a, 0L, SEEK_SET);
while (ftell(a) < j)
{
rd = fread(buff, sizeof(char), sizeof(buff), a);
fwrite(buff, sizeof(char), rd, b);
}
}
void ch2(FILE *a, FILE *b)
{
long i = se(a);
long j = i / 2;
char buff[1024];
size_t rd;
fseek(a, j, SEEK_SET);
while (!feof(a))
{
rd = fread(buff, sizeof(char), sizeof(buff), a);
fwrite(buff, sizeof(char), rd, b);
}
}
int main(int argc, const char *argv[])
{
FILE *a = fopen("se.txt", "r");
FILE *b = fopen("des1.txt", "w");
FILE *c = fopen("des2.txt", "w");
pid_t pid1, pid2;
pid1 = fork();
if (pid1 == 0)
{
ch1(a, b);
exit(0);
}
pid2 = fork();
if (pid2 == 0)
{
ch2(a, c);
exit(0);
}
int status;
waitpid(pid1, &status, 0);
waitpid(pid2, &status, 0);
fclose(a);
fclose(b);
fclose(c);
return 0;
}
要求创建 3 个进程,其中子进程 1 拷贝文件的前一半,子进程 2 拷贝文件的后一半,父进程负责回收两个子进程的资源
最新推荐文章于 2024-09-04 11:56:14 发布