ns3 create thread

37 views
Skip to first unread message

Yolanda Xu

unread,
May 30, 2017, 11:29:37 PM5/30/17
to ns-3-users
Can ns3 create thread as C?  I use (pthread_create(&t_id[num_threads], NULL, AddBot, &nodeid)  to create thread, the function AddBot is executed, but when I use pstree to see thread in terminal, I found the thread didn't start.
Here is part of my code:

void *AddBot(void * args)
{
cout << pthread_self()<<endl;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(num_threads%8, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
{
cout<<"set thread affinity failed\n"<<endl;
}
uint32_t nodeid = *(uint32_t *)args;
Ptr<Node> node = nodes.Get(nodeid);
ObjectFactory m_factory;
m_factory.SetTypeId(bot.GetTypeId());
Ptr<Scanner> client_app = m_factory.Create<Scanner>();
node->AddApplication(client_app);
return 0;
}
void AddThread(uint32_t nodeid)
{
if(pthread_create(&t_id[num_threads], NULL, AddBot, &nodeid))
{
cout<<"could not create thread"<<endl;
}
num_threads++;
sleep(5);
pthread_join(t_id[num_threads],NULL);
}

Yolanda Xu

unread,
May 31, 2017, 2:24:56 AM5/31/17
to ns-3-users
Here is part of my code:

void *AddBot(void * args)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(num_threads%8, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0)
{
cout<<"set thread affinity failed\n"<<endl;
}
uint32_t nodeid = *(uint32_t *)args;
Ptr<Node> node = nodes.Get(nodeid);
ObjectFactory m_factory;
m_factory.SetTypeId(bot.GetTypeId());
Ptr<Scanner> client_app = m_factory.Create<Scanner>();
//client_app->m_infected = 1;
node->AddApplication(client_app);
return 0;
}
void AddThread(uint32_t nodeid)
{

if(pthread_create(&t_id[num_threads], NULL, AddBot, &nodeid))
{
cout<<"could not create thread"<<endl;
}
num_threads++;
//cout<<"add thread"<<endl;
sleep(5);
pthread_join(t_id[num_threads],NULL);

}

void ListenHandleRecv(Ptr<Socket> socket) {
if (socket->GetTxAvailable() > 0) {
uint8_t buf[64];
socket->Recv(buf, 64, 0);
uint8_t ip[4] = { buf[1], buf[2], buf[3], buf[4] };
Ipv4Address address;
uint32_t nodeid;
uint8_t passwd = buf[9];
if (buf[0] == 0) {
address = address.Deserialize(ip);
nodeid = 0;
nodeid |= buf[5];
nodeid <<= 8;
nodeid |= buf[6];
nodeid <<= 8;
nodeid |= buf[7];
nodeid <<= 8;
nodeid |= buf[8];
cout << "[ScanListen] nodeid is: " << nodeid << " address is " << address << " passwd is: " << uint32_t(passwd) << endl;
mylist.push_back(nodeid);
AddThread(nodeid);
}
}
//socket->Close();
}
void ListenHandleConnectionCreated(Ptr<Socket> socket,const Address & address) {
socket->SetRecvCallback(MakeCallback(&ListenHandleRecv));
}
void ListenHandleConnection() {
for(uint32_t i = 8; i< 16; i++)
{
Ptr<Socket> server = Socket::CreateSocket(fanoutNodes[level - 1][i].Get(0),TcpSocketFactory::GetTypeId());
InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(),scan_cb_port);
server->Bind(local);
server->Listen();
server->ShutdownSend();
server->SetAcceptCallback(MakeNullCallback<bool, Ptr<Socket>, const Address &>(), MakeCallback(&ListenHandleConnectionCreated));
}

}

在 2017年5月31日星期三 UTC+8上午11:29:37,Yolanda Xu写道:
在 2017年5月31日星期三 UTC+8上午11:29:37,Yolanda Xu写道:
在 2017年5月31日星期三 UTC+8上午11:29:37,Yolanda Xu写道:

pdbarnes

unread,
Jun 3, 2017, 1:45:14 PM6/3/17
to ns-3-users
Reply all
Reply to author
Forward
0 new messages