2

I am trying to code for the following but in vain. can someone help?

Create a thread that continuously loops, prompting the user to input an integer.The second thread sleeps for ten seconds, and then displays: the maximum value input so far; . It then goes back to sleep and repeats.

I am highly confused and stuck. Can someone help

This is the code so far

pthread_create(&thread1, NULL, &inputfunction, NULL);
        pthread_create(&thread2,NULL, &inputfunction, NULL);
        pthread_join(thread1,NULL);
       // pthread_join(thread2,NULL);
//        pthread_mutex_init(&array_lock, NULL);
        return 0;
}

void *inputfunction()
{
        //int *values= ptr_value;
        int i;
        int arr[5];
        printf("Input values:");
        for (i=0;i<5;i++)
        scanf("%d",&arr[i]);
        sleep(10);

        int max=0;
        for (i=0;i<5;i++)
                {
                        if (arr[i]>max)
                                max=arr[i];
                }
        printf("max=",max);

        return NULL;


}
Mat
  • 202,337
  • 40
  • 393
  • 406
Richa Sachdev
  • 2,937
  • 3
  • 17
  • 12

2 Answers2

5

Since this seems to be homework I won't write you any code but a little analysis might help you understand the problem better here.

Breaking down requirement & How to analyze it?

You need two separate threads, One which does the job of receiving the variable from user and storing it, and another thread which wakes up after every 10 seconds and reads the current inputted value.
Apart from the obvious fact that you need 2 threads notice the emphasis on storing & reading, it implies the same variable needs to be accessed from two different threads which tells you that it should be a global variable.

What is wrong in your current code?
You have two threads alright, but you need two different thread functions as well because you need to do different activities in the two threads.So you cannot reuse the same function inputfunction() for both threads. You will need two distinct functions & we already have a breakdown of what each function should do in the first part of the Q here.
Hint: Check out the documentation of your environment(Windows/Linux etc) for timers and registering your own functions as callback functions that could actually simplify the need of a spawning the separate thread completely.

Also, You need to access the same global variable between two threads, this means that the two threads can race among themselves to access the shared variable so you need some kind of synchronization mechanism to avoid this.

The above analysis should get you started with programming, attempt to do so and if you face any problems, feel free to ask a question here with specific details of what you have tried and what exact problem you are facing.

All the best :)

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

You only wrote one function that blurs the line between thread one and thread two.

  • It only loops 5 times (not called for in the instructions!)
  • It reads input (part of thread one)
  • It sleeps (part of thread two)
  • It maintains an array of 5 integers (not called for in the instructions!)

If you simply follow the instructions, you will be MUCH further along:

Create a thread that continuously loops, prompting the user to input an integer.

/* global */  int g_i;

void Thread_One_Main(void)
{
    g_i = 0;
    while( g_i != -1)
    {
        scanf("%d", &g_i);
    }
}

The second thread sleeps for ten seconds, and then displays the maximum value input so far.
It then goes back to sleep and repeats.

void Thread_Two_Main(void)
{
    int max = 0;
    while(g_i != -1)
    {
        sleep(10);
        if (g_i > max)
        {
            max = g_i;
            printf("New max value: %d\n", max);
        }
    }
}

P.S. I decided to use -1 as a stop-value, since you didn't mention anything about when or how the program ends.

Now, can you write a main() to get both of these threads started?

Can you synchronize between the two threads so they don't read and write from g_i at the same time?

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Thanks for the input. I followed the advice and have changed my code. the program stops when a on integer is inputted. And I need to create mutex locks I believe to avoid race condition. – Richa Sachdev May 23 '12 at 06:25