English 中文(简体)
为什么这个多字 C 程序会给我分割错误? [关闭]
原标题:Why is this multithreaded C program giving me segmentation fault? [closed]

我打算用多线转换两个文件。 但下面的程序让我分解错误。

#include   <stdio.h>
#include  <stdlib.h>
#include <pthread.h>
#include  <string.h>


void *a_to_temp( void *filea);
void copyFile( FILE *in, FILE *out );
void *temp_to_b( void *fileb);
void *b_to_a(void *ab);

struct files{
    char a[80];
    char b[80];
} ab;

pthread_mutex_t temptob     = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t btoa        = PTHREAD_MUTEX_INITIALIZER;

main(int argc, char **argv)
{
    fprintf(stderr, "in main");

    pthread_t thread1, thread2, thread3;
    strcpy( ab.a, argv[1]);
    strcpy(ab.b, argv[2]);

    int  iret1, iret2, iret3;

    pthread_mutex_lock( &temptob );
    pthread_mutex_lock( &btoa );

    iret1 = pthread_create( &thread1, NULL, a_to_temp, (void*) &argv[1]);

    iret2 = pthread_create( &thread2, NULL, b_to_a, (void*) &ab);

    iret3 = pthread_create( &thread3, NULL, temp_to_b, (void*) &argv[2]);


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

    exit(0);
}


void *a_to_temp( void *filea) {
    FILE *a = fopen((char *)filea, "r");    
    FILE *f = fopen( "temp", "w");
    copyFile( a , f);
    fclose(f);
    fclose( a); 
    pthread_mutex_unlock( &temptob );
}

void *temp_to_b( void *fileb) {
    pthread_mutex_lock( &temptob );
    FILE *b = fopen((char *)fileb, "r");
    FILE *f = fopen( "temp", "r");
    copyFile( f, b);
    fclose(f);  
    pthread_mutex_unlock( &btoa );
}

void *b_to_a(void *ab) {
    pthread_mutex_lock( &btoa );
    FILE *a = fopen(((struct files *) ab)->a, "w"); //
    FILE *b = fopen(((struct files *) ab)->b, "r");//
    fprintf(stderr, "c files opened");
    copyFile( b, a);
    fclose(a);
    fclose(b);
}

void copyFile( FILE *in, FILE *out) {
    char ch;
    while(!feof(in)) {
        ch = getc(in);  
        if(!feof(in)) putc(ch, out);            
    }
}

我通过打印值测试了代码直到主函数结束。 我猜错误应该位于函数之一之内。

最佳回答

您通过 a_to_temp () 函数 a_to_temp () () charles \\ /code >, 然后再尝试使用它, 仿佛是 char \\ \ /code > 。 用于 a_temp_to_ b () 函数的 Ditto。 这不是幸福的配方; 核心倾弃是一个相当合理的响应 。

最简单的修正就是在 pthread_cape() 调用中放下 调用。 替代的办法是在调用函数中处理 char {/code> 。

注意 : 这仅仅是修正访问错误数据导致的核心倾弃点 。 代码也可能存在算法问题, 以确保正确同步发生 。 此处使用线索是否有性能收益是值得商榷的。 事实上, 可能没有。 但是, 这大概与练习点不相干, 也就是让线索代码发挥作用 。


你能告诉我如何将我的线条序列起来吗?

一个项目可以确保您检查每个系统呼叫的错误; 目前, 您将假设所有功能都有效 。 然而, 我认为 < a href=" https:// stackoverflow. com/ uss/2362222/ jay" > Jay < / a > 正在正确的轨道上 。 您可能想要一两个条件 ( < a href=> http://pubs. opengroup. org/ onlinepubs/96999999/ spuses/ pthread_cond_init.html/ rel=" nofolve noreferr"\\\code> pthread_cond_ init () et al) 来控制对文件的存取权限, 而不是缩写 。

问题回答

您的代码有一个问题, 就是您正在将哑巴锁在一个线条上, 并且将它解锁在另一个不允许的线条上。

如果试图解开一个不拥有它的线条中的哑巴,它可能导致未定义的行为。

例如,您的 pthread_mutex_lock( & amp; templeob) ; 在主线中, 而 pthread_mutex_unlock( & amp; tempob) ; 在 a_to_temp 中 。

恕我直言,但我的印象是,你真正想要实现的不是一定的相互排斥,而是操作的序列化(即: 命令 ) : 第一: oppl_ a_ a_to_ a; 第二: cop_ b_to_ a; 第三: cop_ b_ a_ a; 第三: cop_ op_ temp_ a_ b) 。 以上答案是对同一线索锁定和打开哑巴的正确。 然而, 命令( 或更确切地说, 迫使一条线索上到另一个任务完成之前) 并不是哑巴的工作, 而是显示显示器/ 设置变量的工作( 或以更复杂的方式, 因为一个 semapopherre可以执行监视器 。 要强制使用线索( 例如, cop_ b_to_ a) 必须 < em > a_ to con> a_ to coplement_ to phread_ a p63_ conmp_ d_st_ was_ lax_ a prow_ lask_ a hrestrop_ lement_ lex_ 86_ a hreff)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...