私は大学生で、ネットワークの割り当ての一環として、Stop-and-Wait Protocol の実装を行う必要があります。問題文では、2 つのスレッドを使用する必要があります。私はスレッド化の初心者ですが、pthreads API の man ページを読んだ後、基本的なコードを書きました。ただし、スレッドが正常に作成された後 (引数として pthread_create() に渡された関数の最初の行の実行時)、セグメンテーション違反が発生します。
typedef struct packet_generator_args
{
int max_pkts;
int pkt_len;
int pkt_gen_rate;
} pktgen_args;
/* generates and buffers packets at a mean rate given by the
pkt_gen_rate field of its argument; runs in a separate thread */
void *generate_packets(void *arg)
{
pktgen_args *opts = (pktgen_args *)arg; // error occurs here
buffer = (char **)calloc((size_t)opts->max_pkts, sizeof(char *));
if (buffer == NULL)
handle_error("Calloc Error");
//front = back = buffer;
........
return 0;
}
メイン スレッドは、このバッファからパケットを読み取り、停止して待機するアルゴリズムを実行します。
pktgen_args thread_args;
thread_args.pkt_len = DEF_PKT_LEN;
thread_args.pkt_gen_rate = DEF_PKT_GEN_RATE;
thread_args.max_pkts = DEF_MAX_PKTS;
/* initialize sockets and other data structures */
.....
pthread_t packet_generator;
pktgen_args *thread_args1 = (pktgen_args *)malloc(sizeof(pktgen_args));
memcpy((void *)thread_args1, (void *)&thread_args, sizeof(pktgen_args));
retval = pthread_create(&packet_generator, NULL, &generate_packets, (void *)thread_args1);
if (retval != 0)
handle_error_th(retval, "Thread Creation Error");
.....
/* send a fixed no of packets to the receiver wating for ack for each. If
the ack is not received till timeout occurs resend the pkt */
.....
gdb を使用してデバッグを試みましたが、関数の最初の行でセグメンテーション エラーが発生する理由を理解できませんgenerate_packets()
。うまくいけば、あなたの一人が助けることができます. 追加のコンテキストが必要な場合は、 http://pastebin.com/Z3QtEJpQでコード全体を入手できます。私はこれに何時間も費やして、ここで本当に渋滞しています。どんな助けでも大歓迎です。