0

I went thorough different pthread tutorials on the web. here, here and here among others. But there is a questions that is still left unanswered, and was wondering if anyone could clarify it.

Question:

  • Suppose I want to print a b a b a b a b a. And suppose thread1 is printing a and thread2 is printing b. This means that thread1 executes then hands over the control to thread2. Then thread2 prints b and control is handed over back to thread1, so on and so forth. In such a scenario, is it possible to create two threads and call each one at a time inside a loop that executes a specific number of times(using thread ID or some builtin function?)? Or do i have to create two threads each time using a loop?

e.g: should i do something like:

  create_thread1()
  create_thread2()
  for loop
      call thread1()
      call thread2

or should i do something like:

  for loop
      create_thread1() to do something
      create_thread2() to do something

EDIT: I removed part of the details from questions, cause users thought that was the question.

EDIT: code

void *func(void *arg){
    int i;
    while(i<30){

        printf("%s\n",(char *)arg);
        i++;
    }
    return 0;
}


int main(int argc, char *argv[]){

    pthread_t thread1, thread2;
    int rt1, rt2;
    rt1 = pthread_create(&thread1, NULL, &func, "a");
    rt2 = pthread_create(&thread2, NULL, &func, "b");

    sleep(1);
    return;
}
Neo
  • 349
  • 5
  • 18
  • There is no `for` loop in the main function. You just create `thread1` and `thread2` and let them run. `thread1` has a `for` loop that prints `a` five times. `thread2` has a `for` loop that prints `b` four times. Peterson's algorithm is used in each thread to wait for the other thread, before executing the next iteration of the `for` loop. – user3386109 Nov 13 '14 at 22:25
  • @user3386109 well i don't want to print `aaaaabbbbb` i want each thread to run once and handover the control to the second thread – Neo Nov 13 '14 at 22:30
  • That's not the point of Peterson's algorithm. The point of Peterson is to allow two threads to alternate using a single resource. `thread1` has a loop that repeats five times. Each time through the loop, `thread1` prints a single `a` and then waits for `thread2` to run. – user3386109 Nov 13 '14 at 22:33
  • @user3386109 I know. Once this is done, I will add a counter, as critical code between the two threads. each will update it if they have a lock over it. But this is the first phase. Think of it as assignment restriction. i want to have a loop with two threads,each printing a single character a or b. The loop should print for example print 20 a b's. – Neo Nov 13 '14 at 22:37
  • Funny thing is, when I implemented the two threads without any arbitration, the output was `abababababababababab`. It took a little experimenting with small delays in `thread1` to get `ababbbbababbbbaaaaaa` – user3386109 Nov 13 '14 at 22:59
  • @user3386109 all i get is b's. don't know why. if i code pthread_create(thread1) and pthread_create(thread2), will they run parallel or serial? – Neo Nov 13 '14 at 23:04
  • Add your actual code to the question and I'll take a look. – user3386109 Nov 13 '14 at 23:05
  • 1
    `i` is uninitialized in the thread function. Use `for (i=0;i<30;i++)` instead. Also, you should use `pthread_join` at the end of `main` instead of `sleep`. But the uninitialized variable is the bigger problem. – user3386109 Nov 13 '14 at 23:14
  • @user3386109 is there a way to count the number of characters that is giong to be printed? let's say I want to print 10 characters (mix of a,b), how can I count it them so that I stop at after that. – Neo Nov 13 '14 at 23:22
  • For this homework, I would just have two thread functions, one that prints five `a`s and one that prints four `b`s. However, to answer your question, if you want to pass multiple parameters to the thread function, you need to define a `struct` to hold all of the parameters, and pass a pointer to the `struct` as the argument of the thread function. – user3386109 Nov 13 '14 at 23:29

2 Answers2

0

Have a global variable sum and in Thread1 add 10 to sum 1000 times and in Thread2 subtract 5 from sum 1000 times. Run both the Threads and see the output. Race condition will occur. Peterson's algorithm is to avoid race condition. Here sum += 10 and sum -= 5 are the critical sections. Now implement Peterson's algorithm and see the output.

This is the best example to demonstrate the working of Peterson's algorithm.

Venkatesh
  • 1,537
  • 15
  • 28
  • I will do peterson's algorithm right after i get this done. Think of the questions as an assignment restrictions. – Neo Nov 13 '14 at 22:39
0

Ok. So i found out that both my assumptions were wrong. Cause I assumed that threads that are created will be executed serially. But when creating threads, the OS will execut the threads randomly and in parallel. so I implemented it as below:

create_thread1(call function1)
create_thread2(call function2)

function1(){
  loop
}

function1(){
  loop
}

A partial answer

Community
  • 1
  • 1
Neo
  • 349
  • 5
  • 18