【进程线程】

#include 
#include 
#include 
#include 
#include 

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;
}

你可能感兴趣的:(数据结构,嵌入式硬件)