C语言 错误:段错误,要复制多个文件

ukdjmx9f  于 2023-04-05  发布在  其他
关注(0)|答案(2)|浏览(159)

我想使用多线程复制多个文件。这在错误中结束:* 分段错误 *,无法判断是哪里。
我尝试了:print("test");在不同的行,但给我同样的错误。我认为主要的功能是正确的,在start_routine()块的东西。
验证码:

typedef struct filePair
{
    char srcName[100];
    char dstName[100];
} filePair;

void * start_routine(void *arg) //file handling using system calls
{
    char tmp;

    printf("Copying %s to %s.\n", ((filePair *)arg)->srcName, ((filePair *)arg)->dstName);

    int src = open(((filePair *)arg)->srcName, O_RDWR); //open source file
    if (!src)
    {
        printf("Cannot open source file.\n"); //error handling
        exit(0);
    }

    //open dst file
    int dst = open(((filePair *)arg)->dstName, O_WRONLY | O_CREAT, 0641);
    if (!dst)
    {
        printf("Error in destination file.\n"); //error handling
        exit(0);
    }

    while (read(src, &tmp, 1)) //while loop to copy contents
        write(dst, &tmp, 1);

    close(src); //close src and dst files
    close(dst);

    return NULL;
}

//main function...
tf7tbtn2

tf7tbtn21#

该类型的名称是struct filePair。使该类型的用法更短的常见方法是合并typedef

typedef struct
{
    char src[100];
    char dst[100];
} filePair;

然后,您可以参考filePair类型。

wfveoks0

wfveoks02#

1.代码中分段错误的原因是指针使用不当,创建filePair类型的指针,如果没有为这些指针分配足够的内存,就会导致分段错误,如果我们为这些指针分配足够的内存,就不会有问题了。

  1. pthread_create()的原型是
    int pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*start_routine)(void *),void *arg);
    在pthread_create()中传递 * file 1之前,我们应该将file 1指针类型转换为(void *)。
    这里我提供了一个简单的版本,不使用filePair的指针,而是使用filePair示例。
#include <stdio.h>     /* printf, stderr */
#include <sys/types.h> /* pid_t */
#include <unistd.h>    /* fork */
#include <stdlib.h>    /* atoi */
#include <errno.h>     /* errno */
#include <pthread.h>   /* pthread */
#include <sys/stat.h>
#include <fcntl.h> /* file handling */
#include <time.h>  /* CLOCK */
#include <string.h>

typedef struct
{
    char srcName[100];
    char dstName[100];
} filePair;

void * start_routine(void *arg) //file handling using system calls
{
    char tmp;

    printf("Copying %s to %s.\n", ((filePair *)arg)->srcName, ((filePair *)arg)->dstName);

    int src = open(((filePair *)arg)->srcName, O_RDWR); //open source file
    if (!src)
    {
        printf("Cannot open source file.\n"); //error handling
        exit(0);
    }

    //open dst file
    int dst = open(((filePair *)arg)->dstName, O_WRONLY | O_CREAT, 0641);
    if (!dst)
    {
        printf("Error in destination file.\n"); //error handling
        exit(0);
    }

    while (read(src, &tmp, 1)) //while loop to copy contents
        write(dst, &tmp, 1);

    close(src); //close src and dst files
    close(dst);

    return NULL;
}

/* main function */
int main(int argc, char *argv[])
{
    pthread_t thread1; //Threads init
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;
    pthread_t thread5;

    filePair  file1;
    strcpy(file1.srcName, "file1.dat");
    strcpy(file1.dstName, "copy1.dat");

    filePair  file2;
    strcpy(file2.srcName, "file2.dat");
    strcpy(file2.dstName, "copy2.dat");

    filePair  file3;
    strcpy(file3.srcName, "file3.dat");
    strcpy(file3.dstName, "copy3.dat");

    filePair  file4;
    strcpy(file4.srcName, "file4.dat");
    strcpy(file4.dstName, "copy4.dat");

    filePair  file5;
    strcpy(file5.srcName, "file5.dat");
    strcpy(file5.dstName, "copy5.dat");

    printf("\n Before threading.\n\n");

    //Creating threads
    pthread_create(&thread1, NULL, start_routine, (void *)&file1);
    pthread_create(&thread2, NULL, start_routine, (void *)&file2);
    pthread_create(&thread3, NULL, start_routine, (void *)&file3);
    pthread_create(&thread4, NULL, start_routine, (void *)&file4);
    pthread_create(&thread5, NULL, start_routine, (void *)&file5);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);
    pthread_join(thread5, NULL);

    printf("\n After threading.\n\n");

    return 0;
}

相关问题