F2020-CS4350-HW-7-C-MultiThread-Programs CS4350 -­‐ Unix Programming Fall 2020 – Husain Gholoom – Senior Lecturer in Computer Science Page 1 Programming Assignment 7 Optional - It Will Replace One of...

1 answer below »
Write a program in C that accepts a positive integer number X from the user and then uses 5 synchronized threads in order to do the following :


F2020-CS4350-HW-7-C-MultiThread-Programs CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  1     Programming Assignment 7 Optional - It Will Replace One of Your Lowest Programming Assignment Grades Due Date : Monday November 30th , 2020 No later that 5:15 pm   NAME    :      __________________________           Course  Number  /  Name    :  ___________________         Assignment  Number  :    ___________   Due  Date  :      ______________________     Write  a  program  in  C    that    accepts  a  positive  integer  number  X  from  the  user  and  then   uses     5      synchronized  threads  in  order  to  do  the  following  :   1. The  first  thread  prints    all  odd  numbers  between  1  and    X  inclusive.   2. The  second  thread  prints  all  the  even  numbers      between  1  and  X  inclusive.   3. The  third    thread  prints  the  sum      between  1  and  X  inclusive   4. The  fourth  thread  print  the  hailstone  sequence  for  the  number  X  .   5. The  last  thread  determines  whether  or  not  X  is  an  Armstrong  number.   CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  2     Note : • Thread  1  ,  2  ,  3  must  be  synchronized.    Threads  4  and  5  are  also  synchronized.   Must  use  lock  and  unlock  when  implementating  this  program.    Look  at  the   sample  run.     • Positive  integer  must  be  grater    than  0.     • Given  any  positive  integer  n,  the  hailstone  sequence  starting  at  n  is  obtained   as  follows.  You  write  a  sequence  of  numbers,  one  after  another.  Start  by   writing  n.  If  n  is  even,  then  the  next  number  is  n/2.  If  n  is  odd,  then  the  next   number  is  3n  +  1.  Continue  in  this  way  until  you  write  the  number  1.     • A  positive  integer  is  called  an  Armstrong  number  if  the  sum  of  cubes  of   individual  digit  is  equal  to  that  number  itself.  For  example:   153  =  1*1*1  +  5*5*5  +  3*3*3    //  153  is  an  Armstrong  number.       12  is  not  equal  to  1*1*1+2*2*2    //  12  is  not  an  Armstrong  number   CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  3     Sample Run Welcome to Husain Gholoom’s Thread App This program will do the following : Accepts an integer number X from the user and then uses 5 synchronized threads in order to do the following : 1. The first thread prints all odd numbers between 1 and X. 2. The second thread prints all the even numbers between 1 and X. 3. The third thread prints the sum between 1 and X. 4. The fourth thread print the hailstone sequence for the number X . 5. The last thread determines whether or not X is an Armstrong number. Enter an Integer > 0 --- > 7 CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  4     All odd / Even / Sum of numbers between 1 and 7 Function1-Odd : 1 Function2-Even : 2 Function3-Sum : 3 Function1-Odd : 3 Function2-Even : 4 Function3-Sum : 10 Function1-Odd : 5 Function2-Even : 6 Function3-Sum : 21 Function1-Odd : 7 All odd / Even / Sum Threads Exits Function4 - Hailstone Process Started Input Number 7 Hailstone sequence of 7 is : 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 Function5 - Armstrong Process Started 7 is not an Armstrong number Hailstone Thread Exits Armstrong Thread Exits End of Main program ---- > Sum = 28 Program by Husain Gholoom 11 – 30 - 2020 CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  5     Welcome to Husain Gholoom’s Thread App This program will do the following : Accepts an integer number X from the user and then uses 5 synchronized threads in order to do the following : 1. The first thread prints all odd numbers between 1 and X. 2. The second thread prints all the even numbers between 1 and X. 3. The third thread prints the sum between 1 and X. 4. The fourth thread print the hailstone sequence for the number X . 5. The last thread determines whether or not X is an Armstrong number. Enter an Integer > 0 --- > -7 **** Number must be grantor than 0 *** Program by Husain Gholoom 11 – 30 - 2020 CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  6     Welcome to Husain Gholoom’s Thread App This program will do the following : Accepts an integer number X from the user and then uses 5 synchronized threads in order to do the following : 1. The first thread prints all odd numbers between 1 and X. 2. The second thread prints all the even numbers between 1 and X. 3. The third thread prints the sum between 1 and X. 4. The fourth thread print the hailstone sequence for the number X . 5. The last thread determines whether or not X is an Armstrong number. Enter an Integer > 0 --- > a **** Number must be grantor than 0 *** Program by Husain Gholoom 11 – 30 - 2020     CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  7       Rules  :   1. The  programs  must  be  syntactically    and  logically  correct.  The   program  will  be  tested  using  the  Zeus  (  Unix  OS  )  at  the  university.   2. The  programs  must  be  documented  and  your  names  ,  course  and   section  number  ,  your  group  number,  assignment  number  ,  due  date   ,  and  instructor’s  name  should  be  included  as  part  of  your  program   documentation.  A  brief  description  of  the  program  must  also  be   included     3. Must  properly  format  the  output  as  shown  above  .   4. You  must  upload  your  solution  using  Canvas  –  Make  sure  you   include  the  above  as  comments  in  your  program.  Make  sure  that  you   name  your  homework  document  as  follows  :     F20_FirstName_LastName_PA7.c                             For  example           F20_Husain_Gholoom_PA7.c       No  late  assignment  will  be  accepted  and  no  extensions  will  be   given          The  following  compiling  command    will  be  used  when  compiling  the                Program   gcc -lpthread F20_Husain_Gholoom_PA7.c              Not  allowed  to  customize    the  command   CS4350  -­‐  Unix    Programming       Fall  2020    –    Husain  Gholoom  –  Senior  Lecturer  in  Computer  Science   Page  8           5. Must    place  the  following  information  on  top  left  of  the  first  page  of  your   assignment  as  comments  before  the  start  of  the  program.  Add  the  missing   information       NAME  :      ______________________               Course  Number  /  Name  :  _______________________     Assignment  Number:    ___________   Due  Date:    ________________   The following points will be deducted if : • Compilation Errors, missing electronic copy , not written in c programming language, submitting .zip file, submitting files other than .c … etc. ( - 10 points ) • The program will be complied as follows : gcc -lpthread
Answered Same DayNov 29, 2021

Answer To: F2020-CS4350-HW-7-C-MultiThread-Programs CS4350 -­‐ Unix Programming Fall 2020 – Husain Gholoom –...

Ria answered on Nov 30 2021
150 Votes
Capture2.PNG
F20_FirstName_LastName_PA7.c
#include
#include
#include .h>
#include
#define NUM_OF_THREADS 10
pthread_mutex_t lock;
int X, i, j, sum=0, H, A;
void* thread1(void* arg)
{
    pthread_mutex_lock(&lock);
    
    if(i%2 != 0) {
        printf("Function1-Odd\t: %d\n", i);
        j=i;
    }
    
    pthread_mutex_unlock(&lock);
}
void* thread2(void* arg)
{
    pthread_mutex_lock(&lock);
    
    if(i%2 == 0) {
        printf("Function2-Even\t: %d\n", i);
    }
    pthread_mutex_unlock(&lock);
}
void* thread3(void* arg)
{
    pthread_mutex_lock(&lock);
    
    if (i%2 == 0) {
        sum += i+j;
        printf("Function3-Sum\t: %d\n", sum);
    }
    
    pthread_mutex_unlock(&lock);
}
int* thread4(void* arg)
{
    pthread_mutex_lock(&lock);
    int c=0;
    
    if (H == 1 && c == 0) {
        printf("%d, ", c);
    }
    else if (H == 1 && c != 0) {
        c++;
        printf("%d, ", c);
    }
    else if (H%2 == 0) {
        c++;
        H = H/2;
        printf("%d, ", H);
    }
    else if (X%2 != 0) {
        c++;
        H = 3*H+1;
        printf("%d, ",...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here