ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi Guys,
I'm trying to use threads and write soem code for TCP timers and seem to have got totlly stuck right at the start .It gives me a segmentation fault whenever I use pthread_create .Here's the code :-
Code:
#include <iostream>
#include <pthread.h>
using namespace std;
int store_ack_tid[20][20];
int i=0; int j=0;
void * dynamic_timer_calc(void *)
{
cout<<"INSIDE DYNAMIC TIMER CALC"<<endl;
}
void start_timer(int storeack)
{
int temp=0;
cout<<"Storeack is "<<storeack<<endl;
pthread_t tid; /*=new pthread_t*/
temp=pthread_create(&tid,NULL,dynamic_timer_calc,NULL);
cout<<"Error code is "<<temp<<endl;
}
void send_segment()
{
int seq=5; // IN THE REAL CODE THIS WILL NOT BE THERE AS THE SEQ WILL ALREADY BE DEFINED
cout<<"CODE FOR SENDING SEGMENT"<<endl;
start_timer(seq);
}
int main()
{
cout<<"inside main"<<endl;
for (i=0; i<19; i++)
for (j=0; j<19; j++)
store_ack_tid[i][j]=0;
send_segment();
return 0;
Whenever I comment that code related to pthread it runs okay . I've tried running it with and without arguments and the result is the same.
Here's my system type :-
[root@LABPC02 timers]#
uname -a 19,3 All
Linux LABPC02 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
[root@LABPC02 timers]#
Do try and see what's going wrong here. Any help/advice is appreciated.
Hey guys, never mind...I figured it out . I wasnt linking it up...so it should be
g++ -o timer -lpthread timer.cpp and it works properly . Iwasn't linking it previously.
Thnx for reading though.
Arvind
Yeah, I tried and it doesn't segfault. But here's one thing you might want to fix:
Code:
for (i=0; i<19; i++)
for (j=0; j<19; j++)
store_ack_tid[i][j]=0;
That block doesn't set all elements to zero, the condition must me < 20 for both i and j for that. And why on earth are i and j globals??
Anyway, if you want to set all elements to zero, simply do:
Code:
memset(store_ack_tid, 0, sizeof(store_ack_tid));
Include <cstring> for std::memset. And use it with care, if the array has decayed to a pointer after being passed to a function, sizeof() will return the size of an int*, not the array itself. But if it's global like in your case, it will work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.