Question

Write a program hellomany.c that will create a number N of threads specified in the command...

Write a program hellomany.c that will create a number N of threads specified in the command line, each of which prints out a hello message and its own thread ID. To see how the execution of the threads interleaves, make the main thread sleep for 1 second for every 4 or 5 threads it creates. The output of your code should be similar to:

       I am thread 1. Created new thread (4) in iteration 0...
       Hello from thread 4 - I was created in iteration 0
       I am thread 1. Created new thread (6) in iteration 1...
       I am thread 1. Created new thread (7) in iteration 2...
       I am thread 1. Created new thread (8) in iteration 3...
       I am thread 1. Created new thread (9) in iteration 4...
       I am thread 1. Created new thread (10) in iteration 5...
       Hello from thread 6 - I was created in iteration 1
       Hello from thread 7 - I was created in iteration 2
       Hello from thread 8 - I was created in iteration 3
       Hello from thread 9 - I was created in iteration 4
       Hello from thread 10 - I was created in iteration 5
       I am thread 1. Created new thread (11) in iteration 6...
       I am thread 1. Created new thread (12) in iteration 7...
       Hello from thread 11 - I was created in iteration 6
       Hello from thread 12 - I was created in iteration 7

Homework Answers

Answer #1

The program for the given problem is given below -

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

//Let us Define maximum number of threads so that it doesn't run endlessly
#define NUMBER 50

pthread_t thread_id[NUMBER];

//Function to create thread.
void * createThread(void * threadNum){
printf("Hello from thread %u - I was created in iteration %d \n",(int)pthread_self(),(int)threadNum);
pthread_exit(NULL);
}

int main(){
int a, n;
printf(" Enter number of threads you want to make.");
scanf("%d",&n);
  
// If n is greater than our max threads then we put n = max number of threads.
if(n > NUMBER)
{
n = NUMBER;
}
for(int i = 0; i < n; i++)
{
// Creating thread
a = pthread_create(&thread_id[i], NULL, createThread, (void*)i);
  
//Error handling
if(a)
{
printf("\n ERROR: return code from pthread_create is %d \n", a);
exit(1);
}
  
  
printf("\n I am thread %u. Created new thread (%u) in iteration %d ...\n",
(int)pthread_self(), (int)thread_id[i], i);
  
// Making main thread sleep after every 5 seconds.
if(i % 5 == 0)
sleep(1);


}

  pthread_exit(NULL);

}

Code screenshot -

Code Output -

Note - In my output, thread id doesn't start from 1 because I was running my program in an online compiler. If you run this program in your system, it will start from 1.

Solution ends.

Please comment and let me know if you have any further doubts. Please upvote this answer if you like it.

Thank you.

Have a good day.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file called synch.txt. Then it will create two separate threads, Thread-A and Thread-B. Both threads will open synch.txt and write to it. Thread-A will write the numbers 1 - 26 twenty times in nested for loops then exit. In other words, print 1 - 26 over and over again on separate lines for at least 20 times. Thread-B will write the letters A - Z...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between...
Write a PHP code that: 1- Creates an array that holds 10 random integer numbers between 1 and 100. 2- Moves all multiple of 3-numbers in the array that created in part-a into a new array. 3- Moves all multiple of 5-numbers in the array that created in part-a into a new array. 4- Find the maximum and the minimum multiple of 3-numbers, if exist. 5- Find the maximum and the minimum multiple of 5-numbers, if exist. 6- Prints the...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D list in an N X N structure with integers 1-(N*N) Print the list created Reverse the list such that (1) each list in the 2D list is reversed and (2) the order of each list in the outer list is reversed (see example) Print the reversed list Example Execution What size 2D list would you like to create? N> 3 The original list is:...
Write Linux command lines to do the following: 1. Create a directory with name “yourName_ID” 2....
Write Linux command lines to do the following: 1. Create a directory with name “yourName_ID” 2. Create a file with name “yourLastName.txt” inside the directory created in step.1 3. Write inside the created file exactly the following content (you may use editors such as vim, pico, ….): Linux Lab Lab. ToDo#1 Summer Semester 2019/2020# 4. Display the first 2 lines of the file. 5. Change the permission of the file to read write and execute for owner, groups and others....
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how...
I need a breakdown to perform in excel for numbers 7,8,9. I am unsure of how I calculate the times. heres the data set and the questions. Calculate the probability that a flight will depart early or on time. Calculate the probability that a flight will arrive late. Calculate the probability that a flight departs late or arrives early. DEP_Delay ARR_Delay -4 0 -3 -3 0 -5 -7 -1 8 3 -1 -5 3 8 11 6 -6 0 -5...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT