Can anyone help me improve my source code below? I tried fixing it but apparently, there's not much success.

#define NUM_THREADS1     1

void *go_to_website2(void *program);

void *go_to_website1(void *program)
{
     char* str[3];
     str[0] = (char *)program;   
     str[1] = "http://www.google.com"; //supposedly 2nd website to go to
     str[2] = '\0';

     char* new_prog = "firefox";
     pthread_t th;
     int rc2;
     rc2 = pthread_create(&th, NULL, go_to_website2, (void *)new_prog); //2nd thread created

     pthread_join(th, NULL); //wait for the second thread th to be finished
     execvp(str[0], str); //go to 2nd website (google.com), unsuccessfully opened
}

void *go_to_website2(void *program)
{
     char* str2[3];
     str2[0] = (char *)program;    
     str2[1] = "http://www.yahoo.com"; //first website, successfully opened but the 
     str2[2] = '\0';                   // threads and the program exits after this
     execvp(str2[0], str2);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS1];

   int rc1, rc2;
   long t;

   char* prog;
   prog = "firefox";

   for(t = 0; t < NUM_THREADS1; t++) {
      sleep(1);
      printf("In main: creating thread %ld\n", t); //used this for checking

      sleep(1);
      rc1 = pthread_create(&threads[t], NULL, go_to_website1, (void *)prog); //create 
   }                                                                    // first thread

  pthread_exit(NULL);
}

Now I have no idea how to open more than one website. If we try to run the program above, only www.yahoo.com will be opened. When yahoo is opened, the threads and the program itself terminate. Is there a way to fix the program to open www.yahoo.com and then www.google.com afterwards? Could anyone suggest to me some concepts/strategies in using pthreads to implement for this purpose? I'm using Linux. Any answers will be greatly appreciated.

please take a look at this website.

The concept you are using here is not quite right. You don't need to define 2 separate functions to take care of your task. So your general design should be like, create a function which gets the your link address(ex: www.yahoo.com) as a parameter, then design your threads and pass the link address through the thread to the function. The function calls the respective program and uses the passed parameter to run the program. There is one tiny trick in all thread programming though, you may write your threads, run the program, and then, ...... nothing happens. It's quite a headache when you can't figure it out.

Just keep in mind that invoked threads die as soon as the MAIN THREAD dies. What it means is that(You can find this in the link I left above), when you do threading, a master(main) thread is created and then, that main thread calls the rest of the threads, so as soon as the main thread finishes its tasks, it terminates, and along with it all its children(other threads) are terminated too. Therefore we use a function called join() which joins the baby threads to join their mother and as long as babies are still working, mother would wait for them!!! hehehe... I hope you got it.

So your general design should be like, create a function which gets the your link address(ex: www.yahoo.com) as a parameter, then design your threads and pass the link address through the thread to the function. The function calls the respective program and uses the passed parameter to run the program.

This part is quite confusing to me because you used the word 'function' several times. Were you talking about only 1 function here? Is the function that gets the link address also the function that the link address will be passed to? And is it also the function that will call the program? Thanks

Yeap, exactly, you only need one function. That's why we have functions right?? To avoid repeating our code over and over again.

Yeap, exactly, you only need one function. That's why we have functions right?? To avoid repeating our code over and over again.

So will this function be somewhat recursive in a way? The way I'm visualizing it right now is that this function will receive the link address, then it creates threads, the threads will pass the link address to the function itself, and upon receipt the function will then call the program and the browser will now be opened. Is my interpretation correct?

Yeap, exactly, you only need one function. That's why we have functions right?? To avoid repeating our code over and over again.

group256, do you know of any other way to open a website from a C code aside from using exec/system functions? My instructor didn't approve of the exec function because it needs firefox, he told me what if the computer doesn't have firefox in it? He suggested I use more generic functions from libraries and he also mentioned about the Socket APIs. He said I could use these generic functions in calling the url/link address only, and there's no need to call .exe functions like firefox. Do you know of any other method that implements some functions in this Socket APIs to go to a website? Thank you

hmm... Now we are moving on to some other parts!!!

Of course you can access a website without firefox or any other browsers. But this thread title and question was about "how to open a browser" not how to connect to a web server.

A browser is nothing except than a multithreaded program that uses sockets to retrieve data from web servers.

For that, you need to know about socket programming which is completely out of scope of this thread. It's not that hard to do socket programming in C but you need to have understanding of IP and port address and TCP and UDP protocols.

FYI, when a thread is done and you have no more questions to ask in that thread(in case your question is answered) please mark it as SOLVED.

By the way, if you are interested, pop a shell and type "w3m www.yahoo.com" and see what happens. I think it could be a good example!!

Edited 6 Years Ago by group256: n/a

hmm... Now we are moving on to some other parts!!!

Of course you can access a website without firefox or any other browsers. But this thread title and question was about "how to open a browser" not how to connect to a web server.

A browser is nothing except than a multithreaded program that uses sockets to retrieve data from web servers.

For that, you need to know about socket programming which is completely out of scope of this thread. It's not that hard to do socket programming in C but you need to have understanding of IP and port address and TCP and UDP protocols.

FYI, when a thread is done and you have no more questions to ask in that thread(in case your question is answered) please mark it as SOLVED.

By the way, if you are interested, pop a shell and type "w3m www.yahoo.com" and see what happens. I think it could be a good example!!

I didn't mark it as SOLVED yet because I thought I would still be asking something about multithreading, but it seems like my program is headed to another route (which is to use socket programming). I'm new to socket programming so I'm not really sure which protocol I'm going to use, either TCP or UDP. Is it better to have connection-oriented protocol for this? I also tried typing "w3m www.yahoo.com" in the terminal but it said something like "Can't load www.yahoo.com". I created a new thread about this new topic here: http://www.daniweb.com/forums/post1394042.html#post1394042 Hope you could answer my questions. Thanks

I didn't mark it as SOLVED yet because I thought I would still be asking something about multithreading, but it seems like my program is headed to another route (which is to use socket programming). I'm new to socket programming so I'm not really sure which protocol I'm going to use, either TCP or UDP. Is it better to have connection-oriented protocol for this? I also tried typing "w3m www.yahoo.com" in the terminal but it said something like "Can't load www.yahoo.com". I created a new thread about this new topic here: http://www.daniweb.com/forums/post1394042.html#post1394042 Hope you could answer my questions. Thanks

new to networking?
this could be a good place to start

This article has been dead for over six months. Start a new discussion instead.