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. Thank you!

Any exec function replaces the calling image with its own..So that would explain why the calling process closes...

If I was doing this, I would use the fork/exec combination.

Any exec function replaces the calling image with its own..So that would explain why the calling process closes...

If I was doing this, I would use the fork/exec combination.

That was what I initially did. But what I really need is to open at least 4 websites one after the other, and fork() can't do that for me since it can only create 2 processes. So what I did was to use pthread, and yeah, I read in a tutorial that calling exec will terminate the threads but I think there's a way to do this successfully. Hopefully I figure that out in the next couple of days.

Any exec function replaces the calling image with its own..So that would explain why the calling process closes...

If I was doing this, I would use the fork/exec combination.

By the way, do you have any idea of how to implement this aside from fork/pthread? Or is there any other way to open a browser/website from the terminal aside from calling exec? Thanks

Calling fork creates a new process...Calling fork again will create another process and calling it again will create another...Just make sure you know who's calling fork...I would delegate that to the original process.

To answer your second question...You could use the system() function

int system(const char *command);

DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. During exe‐
cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
will be ignored.

Calling fork creates a new process...Calling fork again will create another process and calling it again will create another...Just make sure you know who's calling fork...I would delegate that to the original process.

To answer your second question...You could use the system() function

int system(const char *command);

DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed. During exe‐
cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
will be ignored.

I also tried calling fork() a couple of times in my original program, but then it didn't work. The program looks like this:

pid_t pid = fork();

if (pid == 0) {
    execvp(...); goes to another website
}

else {
    execvp(..); goes to 1 website
}

/*anything below this is not executed for some reason*/

pid_t pid2 = fork();

if (pid2 == 0) {
    execvp(...); goes to another website
}

else {
    execvp(..); goes to 1 website
}

Like my problem w/ pthread, exec terminated my program prematurely and I wasn't able to open the 3rd and 4th websites. What could have gone wrong?

That's because your calling execvp in both the parent and the child...Only call execvp in the child.

That's because your calling execvp in both the parent and the child...Only call execvp in the child.

So I'll just call fork again in the parent block?

So I'll just call fork again in the parent block?

Yep that's right..The parent calls fork and the child calls execvp.

Yep that's right..The parent calls fork and the child calls execvp.

Thanks man, the code is working now, although I don't know how to end the program. I mean if I run it in the terminal, after opening the websites, the program still doesn't end. Is there a command to make it stop after opening the websites? Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.