hi all
is fork can be used as thread?
if yes how can it be?
thank you in advace
hi all
is fork can be used as thread?
if yes how can it be?
thank you in advace
fork() creates processes, not threads.
Use a thread function if you want to create a thread.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i treid this
i used this thread in seperate function where i need it.(not used in main())Code:pthread_t thread2; int iret2; iret2 = pthread_create( &thread2, NULL, write_data, NULL); pthread_join( thread2, NULL);
i am using pthread_join, so that main() application gets hanging.
sometimes it causes segmentationfualt
van you please help me
thank you in advance
my magic 8-Ball says "Without a doubt."
Post a small complete example which shows the problem.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
please forgive me,
it has big code...(more than 1000lines)
i got problem with
it is stoping the code untill completion of thread2.Code:pthread_join( thread2, NULL);
means my gui based application getting hanged.
and also onething am not using threads in main() function, using it in some other function.
with out these some functions are not working in my code.
do you have any idea about this
thank you in advance
Not a bloody clue without some code.
Segfault simply means that you tried to access memory you don't own.
There are many many ways which this can happen, and I'm not going to list them all just for you to pick them off one at a time saying "not it"
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
hello
here is some code
Code:#include <gtk/gtk.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <linux/sockios.h> #include <string.h> #include <math.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <syslog.h> GtkWidget *dialog; GtkWidget *parent_window; gint result; gchar *file; gchar *ind; gchar *post=NULL; void server1(); void server2(); int n,i=2010,j=2030; gchar *item=NULL,*item2=NULL,*item1=NULL, *text=NULL; gchar *text1=NULL,*post1=NULL; /* Get the selected filename and print it to the console */ static void file_ok_sel() { g_print ("%s\n",file ); item1= g_strconcat (file,NULL); post=g_strconcat (file,NULL); pthread_t thread1; int iret1; iret1=pthread_create( &thread1, NULL, server2, NULL); pthread_join( thread1, 0); } int main( int argc, char *argv[] ) { gtk_init (&argc, &argv); dialog = gtk_file_chooser_dialog_new ("File Selection", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); result = gtk_dialog_run (GTK_DIALOG (dialog)); switch (result) { case (GTK_RESPONSE_OK): file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); g_print("file=%s\n",file); file_ok_sel(); /* Use the filename here. */ default: gtk_widget_destroy (dialog); } gtk_main (); return 0; } void error(char *msg) { perror(msg); exit(0); } void server2() { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; // int i=2030; char buffer[256]; portno = i; printf("i=%d\n",i); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname("XXX.XXX.XXX.XXX"); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) { } else { post=g_strconcat(post,NULL); g_print ("post=%s\n",post ); n = write(sockfd,post,strlen(post)); if (n < 0) error("ERROR writing to socket"); g_print("post======%s \n",post); } i++; server1(); } void server1() { int sockfd, newsockfd, portno, clilen; char buffer[256],*item; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno =j; printf("j=%d\n",j); bzero(item1,strlen(item1)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); printf("j=%d\n",j); j++; if (newsockfd < 0) error("ERROR on accept"); n = read(newsockfd,item1,1024); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",item1); if (n < 0) error("ERROR writing to socket"); item1[strlen(item1)]='\0'; int m; g_print("%s \n",item1); }common give me the solution of thread here "Mr.god of c"Not a bloody clue without some code.
i know "as a god of c" you can solve this.......
thank you in advance
First, lets have some code which doesn't look like it was indented by using your feet.
If it's as messy in your IDE as it is posted here, you're just making it much harder for yourself than it needs to be.
First issue, who allocates this memory, and how much do you actually get?Code:#include <gtk/gtk.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <linux/sockios.h> #include <string.h> #include <math.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <syslog.h> GtkWidget *dialog; GtkWidget *parent_window; gint result; gchar *file; gchar *ind; gchar *post = NULL; void server1(); void server2(); int n, i = 2010, j = 2030; gchar *item = NULL, *item2 = NULL, *item1 = NULL, *text = NULL; gchar *text1 = NULL, *post1 = NULL; /* Get the selected filename and print it to the console */ static void file_ok_sel() { g_print("%s\n", file); item1 = g_strconcat(file, NULL); post = g_strconcat(file, NULL); pthread_t thread1; int iret1; iret1 = pthread_create(&thread1, NULL, server2, NULL); pthread_join(thread1, 0); } int main(int argc, char *argv[]) { gtk_init(&argc, &argv); dialog = gtk_file_chooser_dialog_new("File Selection", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); result = gtk_dialog_run(GTK_DIALOG(dialog)); switch (result) { case (GTK_RESPONSE_OK): file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); g_print("file=%s\n", file); file_ok_sel(); /* Use the filename here. */ default: gtk_widget_destroy(dialog); } gtk_main(); return 0; } void error(char *msg) { perror(msg); exit(0); } void server2() { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; // int i=2030; char buffer[256]; portno = i; printf("i=%d\n", i); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname("XXX.XXX.XXX.XXX"); if (server == NULL) { fprintf(stderr, "ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0) { } else { post = g_strconcat(post, NULL); g_print("post=%s\n", post); n = write(sockfd, post, strlen(post)); if (n < 0) error("ERROR writing to socket"); g_print("post======%s \n", post); } i++; server1(); } void server1() { int sockfd, newsockfd, portno, clilen; char buffer[256], *item; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = j; printf("j=%d\n", j); bzero(item1, strlen(item1)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd, 5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); printf("j=%d\n", j); j++; if (newsockfd < 0) error("ERROR on accept"); n = read(newsockfd, item1, 1024); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n", item1); if (n < 0) error("ERROR writing to socket"); item1[strlen(item1)] = '\0'; int m; g_print("%s \n", item1); }
$ grep item1 foo.c
gchar *item = NULL, *item2 = NULL, *item1 = NULL, *text = NULL;
item1 = g_strconcat(file, NULL);
bzero(item1, strlen(item1));
n = read(newsockfd, item1, 1024);
printf("Here is the message: %s\n", item1);
item1[strlen(item1)] = '\0';
g_print("%s \n", item1);
> bzero(item1, strlen(item1));
If the rest of your code is correct, then this is just a waste of time. If on the other hand the memory is uninitialised at this point, then you're just going to trash an unknown amount of memory.
> n = read(newsockfd, item1, 1024);
Where did 1024 come from, and wasn't this being used for storing a filename?
> printf("Here is the message: %s\n", item1);
read() doesn't append a \0 (which is why you fill it with zeros I guess).
It's still wrong, because if read() fills the entire buffer, there's no room for a \0 anyway.
Something like this is what you should have...
char buff[1024];
n = read( newsockfd, buff, sizeof(buff) - 1 );
if ( n > 0 ) buff[n] = '\0';
Without an obvious \0, trying to print that as a string will result in garbage
> item1[strlen(item1)] = '\0';
strlen gives you the index to the \0 character, so this line just overwrites the \0 which is already there with another \0 (nothing happens).
I'm guessing you've chopped out a whole bunch of code showing how some of those variables are in fact initialised.
Maybe try this
gcc prog.c -lefence && gdb a.out
From inside the debugger, just run the program and it will trap at the first point you do something dodgy with allocated memory.
Or maybe
gcc prog.c && valgrind a.out
Then look at all the references to uninitialised and deallocated memory.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.