#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
pid_t pid = fork();
if (pid == 0) {
// 子进程
int fd = open("./a.txt", O_RDONLY);
int fd2 = open("./b.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
char c[1024];
ssize_t bytesRead;
while ((bytesRead = read(fd, c, sizeof(c))) > 0) {
write(fd2, c, bytesRead);
}
close(fd);
close(fd2);
} else if (pid > 0) {
// 父进程
wait(NULL);
} else {
perror("fork");
}
return 0;
}
【进程线程】
最新推荐文章于 2024-07-18 16:41:02 发布