Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Using the POSIX threads library, write a 2-thread program for Linux.Once the thread spawning is done, the first thread (T1) should wait for user input

Posted By: Kyle Evans     Category: C Programming     Views: 3867

Using the POSIX threads library, write a 2-thread program for Linux. Once the thread spawning is done, the first thread (T1) should wait for user input. T1 should produce no output, but should shut down the whole process once the user types a q or a Q. T2 should take no input, but should generate a list of prime numbers starting at 2 and going as high as it can in the time allowed. From the user point of view, this program will generate primes until told to quit.

Code for Using the POSIX threads library, write a 2-thread program for Linux.Once the thread spawning is done, the first thread (T1) should wait for user input in C Programming

# include <stdio.h>
# include <pthread.h>

void *t1(void *);

int main()
{
    pthread_t tid;
    int iCount,iNumber=2;
    int PrimeFlag=1;
    pthread_create(&tid,NULL,t1,NULL);
    for(;;)
    {
        for(iCount=2;iCount<iNumber;iCount++)
        {        
            if(iNumber % iCount==0)
            {
                PrimeFlag=0;
                break;
            }
        }        
        if(PrimeFlag==1)
        {
            printf("%d \t",iNumber);
        }
        PrimeFlag=1;
        iNumber++;
        if(iNumber % 10 ==0)
        {
            usleep(1);
        }

    }    
    return 0;
}

void *t1(void *p)
{
    char ch;
    while(1)
    {
        ch=getchar();
        if(ch=='q' || ch=='Q')
        {
            exit(0);

        }
    }
}

/* Output

[divyen@localhost pp-tw4]$ cc -o Prog03 -lpthread Prog03.c
[divyen@localhost pp-tw4]$ ./Prog03
2 3 5 7
11 13 17 19
23 29
31 37
41 43 47
53 59
61 67
71 73 79
83 89
97
101 103 107 109
113
127
131 137 139
149
151 157
q
*/
  
Share: 



Kyle Evans
Kyle Evans author of Using the POSIX threads library, write a 2-thread program for Linux.Once the thread spawning is done, the first thread (T1) should wait for user input is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!