【进程线程练习题】

1.使用有名管道,完成两个进程的相互通信

main.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char const *argv[])
{
    mkfifo("pipe",0664);
    int fd = open("pipe",O_RDONLY);
    char buf[128];
    while (1)
    {
        read(fd,buf,sizeof(buf));
        printf("%s\n",buf);
        if (strcmp("quit",buf)==0)
        {
            break;
        }
        
    }
    close(fd);
    system("rm pipe");
    return 0;
}

sed.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char const *argv[])
{
    int fd = open("pipe",O_WRONLY);
    char buf[128];
    while (1)
    {
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1] = '\0';
        write(fd,buf,sizeof(buf));
        if (strcmp("quit",buf)==0)
        {
            break;
        }
        
    }
    close(fd);
    return 0;
}

2.使用无名管道完成父子进程间的通信

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char const *argv[])
{
    int pipefd[2] = {0};
    if (pipe(pipefd) == -1)
    {
        perror("pipe");
        return -1;
    }
    char wbuf[128];
    char rbuf[128];
    pid_t pid = fork();

    if (pid > 0)
    {
        close(pipefd[1]); // 关闭写入端
        while (1)
        {
            read(pipefd[0], rbuf, sizeof(rbuf));
            printf("Received: %s", rbuf);
            if (strcmp(rbuf, "quit") == 0)
            {
                break;
            }
        }
        close(pipefd[0]); // 关闭读取端
    }
    else if (pid == 0)
    {
        close(pipefd[0]); // 关闭读取端
        while (1)
        {
            fgets(wbuf, sizeof(wbuf), stdin);
            write(pipefd[1], wbuf, strlen(wbuf) + 1); // 加上字符串结束符 '\0'
            if (strcmp(wbuf, "quit") == 0)
            {
                break;
            }
        }
        close(pipefd[1]); // 关闭写入端
    }

    return 0;
}

3.使用标准IO完成两个文件的拷贝

#include 
#include 

#define BUFFER_SIZE 128

int main(int argc, char const *argv[])
{
    FILE *sourceFile = NULL;
    FILE *destinationFile = NULL;
    char buf[BUFFER_SIZE];
    size_t bytesRead;

    // 打开源文件
    sourceFile = fopen("./a.txt", "rb");
    if (sourceFile == NULL)
    {
        perror("Error opening source file");
        return -1;
    }

    // 打开目标文件,如果已存在则覆盖
    destinationFile = fopen("./b.txt", "wb");
    if (destinationFile == NULL)
    {
        perror("Error opening destination file");
        fclose(sourceFile);
        return -1;
    }

    // 读取源文件并写入目标文件
    while ((bytesRead = fread(buf, 1, sizeof(buf), sourceFile)) > 0)
    {
        fwrite(buf, 1, bytesRead, destinationFile);
    }

    // 关闭文件
    fclose(sourceFile);
    fclose(destinationFile);

    printf("File copy successful.\n");

    return 0;
}

4.使用文件IO实现两个文件的拷贝

#include 
#include 

#define BUFFER_SIZE 1024

int main() {
    FILE *sourceFile, *destinationFile;
    char buffer[BUFFER_SIZE];
    size_t bytesRead;

    // 打开源文件
    sourceFile = fopen("source.txt", "rb");
    if (sourceFile == NULL) {
        perror("Error opening source file");
        return EXIT_FAILURE;
    }

    // 打开目标文件,如果已存在则覆盖
    destinationFile = fopen("destination.txt", "wb");
    if (destinationFile == NULL) {
        perror("Error opening destination file");
        fclose(sourceFile);
        return EXIT_FAILURE;
    }

    // 从源文件读取数据,并写入目标文件
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {
        fwrite(buffer, 1, bytesRead, destinationFile);
    }

    // 关闭文件
    fclose(sourceFile);
    fclose(destinationFile);

    printf("File copy successful.\n");

    return EXIT_SUCCESS;
}

5.使用多进程完成两个文件的拷贝

#include 
#include 
#include 
#include 
#include 

#define BUFFER_SIZE 1024

int main() {
    pid_t pid = fork();

    if (pid < 0) {
        perror("Fork failed");
        return EXIT_FAILURE;
    }

    if (pid == 0) {
        // Child process
        FILE *sourceFile = fopen("source.txt", "rb");
        if (sourceFile == NULL) {
            perror("Error opening source file");
            return EXIT_FAILURE;
        }

        FILE *destinationFile = fopen("destination.txt", "wb");
        if (destinationFile == NULL) {
            perror("Error opening destination file");
            fclose(sourceFile);
            return EXIT_FAILURE;
        }

        char buffer[BUFFER_SIZE];
        size_t bytesRead;

        // 从源文件读取数据,并写入目标文件
        while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {
            fwrite(buffer, 1, bytesRead, destinationFile);
        }

        // 关闭文件
        fclose(sourceFile);
        fclose(destinationFile);

        printf("Child process: File copy successful.\n");
    } else {
        // Parent process
        wait(NULL); // 等待子进程结束
        printf("Parent process: Child process completed.\n");
    }

    return EXIT_SUCCESS;
}

6.使用多线程完成两个文件的拷贝

#include 
#include 
#include 
#include 
#include 

#define BUFFER_SIZE 1024

int main() {
    pid_t pid = fork();

    if (pid < 0) {
        perror("Fork failed");
        return EXIT_FAILURE;
    }

    if (pid == 0) {
        // Child process
        FILE *sourceFile = fopen("source.txt", "rb");
        if (sourceFile == NULL) {
            perror("Error opening source file");
            return EXIT_FAILURE;
        }

        FILE *destinationFile = fopen("destination.txt", "wb");
        if (destinationFile == NULL) {
            perror("Error opening destination file");
            fclose(sourceFile);
            return EXIT_FAILURE;
        }

        char buffer[BUFFER_SIZE];
        size_t bytesRead;

        // 从源文件读取数据,并写入目标文件
        while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {
            fwrite(buffer, 1, bytesRead, destinationFile);
        }

        // 关闭文件
        fclose(sourceFile);
        fclose(destinationFile);

        printf("Child process: File copy successful.\n");
    } else {
        // Parent process
        wait(NULL); // 等待子进程结束
        printf("Parent process: Child process completed.\n");
    }

    return EXIT_SUCCESS;
}

你可能感兴趣的:(算法)