9月2日
1> 使用多进程完成两个文件的拷贝:父进程拷贝前一半内容,子进程拷贝后一半内容
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <src_file> <dest_file>\n", argv[0]);
exit(1);
}
int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("open src");
exit(1);
}
int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dest_fd < 0) {
perror("open dest");
close(src_fd);
exit(1);
}
// 用 lseek 获取文件大小
off_t filesize = lseek(src_fd, 0, SEEK_END);
if (filesize == -1) {
perror("lseek");
close(src_fd);
close(dest_fd);
exit(1);
}
off_t halfsize = filesize / 2;
// 把源文件读指针重置到开头
lseek(src_fd, 0, SEEK_SET);
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
else if (pid > 0) {
// 父进程:拷贝前一半
char buf[BUF_SIZE];
off_t to_copy = halfsize;
ssize_t n;
while (to_copy > 0 && (n = read(src_fd, buf, (to_copy > BUF_SIZE ? BUF_SIZE : to_copy))) > 0) {
write(dest_fd, buf, n);
to_copy -= n;
}
wait(NULL); // 等待子进程完成
}
else {
// 子进程:拷贝后一半
char buf[BUF_SIZE];
ssize_t n;
// 将文件指针移动到后一半
lseek(src_fd, halfsize, SEEK_SET);
lseek(dest_fd, halfsize, SEEK_SET);
while ((n = read(src_fd, buf, BUF_SIZE)) > 0) {
write(dest_fd, buf, n);
}
exit(0);
}
close(src_fd);
close(dest_fd);
printf("File copy finished.\n");
return 0;
}
2> 父进程创建两个子进程,子进程1负责拷贝文件前一半内容,子进程2负责拷贝文件后一半内容,父进程阻塞回收两个子进程资源
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <src_file> <dest_file>\n", argv[0]);
exit(1);
}
int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("open src");
exit(1);
}
int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dest_fd < 0) {
perror("open dest");
close(src_fd);
exit(1);
}
// 用 lseek 获取文件大小
off_t filesize = lseek(src_fd, 0, SEEK_END);
if (filesize == -1) {
perror("lseek");
close(src_fd);
close(dest_fd);
exit(1);
}
off_t halfsize = filesize / 2;
// 回到文件开头
lseek(src_fd, 0, SEEK_SET);
pid_t pid1 = fork();
if (pid1 == 0) {
// 子进程1:拷贝前半部分
char buf[BUF_SIZE];
off_t to_copy = halfsize;
ssize_t n;
int sfd = open(argv[1], O_RDONLY);
int dfd = open(argv[2], O_WRONLY);
while (to_copy > 0 && (n = read(sfd, buf, (to_copy > BUF_SIZE ? BUF_SIZE : to_copy))) > 0) {
write(dfd, buf, n);
to_copy -= n;
}
close(sfd);
close(dfd);
exit(0);
}
pid_t pid2 = fork();
if (pid2 == 0) {
// 子进程2:拷贝后半部分
char buf[BUF_SIZE];
ssize_t n;
// 子进程自己打开文件
int sfd = open(argv[1], O_RDONLY);
int dfd = open(argv[2], O_WRONLY);
// 定位到后一半
lseek(sfd, halfsize, SEEK_SET);
lseek(dfd, halfsize, SEEK_SET);
while ((n = read(sfd, buf, BUF_SIZE)) > 0) {
write(dfd, buf, n);
}
close(sfd);
close(dfd);
exit(0);
}
// 父进程:等待两个子进程完成
wait(NULL);
wait(NULL);
close(src_fd);
close(dest_fd);
printf("File copy finished.\n");
return 0;
}