0
我是一名大学生,作为网络任务的一部分,我需要执行Stop-and-Wait协议。问题陈述需要使用2个线程。我是线程的新手,但在阅读pthreads API的手册页后,我编写了基本代码。但是,在线程创建成功后(执行传递给pthread_create()的函数的第一行作为参数)后,我得到了分段错误)。在Stop-and-Wait协议实现中使用pthreads时的SIGSEGV
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;
}
主线程从此bufffer读取数据包并运行stop-and等待算法。
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获得。我在这里花了几个小时在这里真正的果酱。任何帮助将不胜感激。
主要的教训是,原始的海报应该学会使用'gdb'并用'gcc -Wall -g'编译 – 2013-02-26 06:51:24