Write a program that accepts an integer N as a command line input and then proce
ID: 3906597 • Letter: W
Question
Write a program that accepts an integer N as a command line input and then proceeds to launch N threads within that process. Each process should consist of an infinite loop that prints out “Thread k running” (once every second) where k is a process index (from 0 to N-1). After 5 seconds of running, your main program should kill process 0, then after another 5 seconds, should kill process 1, and so on until all processes have been terminated. At that time, your main program should itself terminate.solution to these problems should be written in C or C++ for Linux.
Explanation / Answer
CODE
LINUX VERSION
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
void *print_thread_info(void *i)
{
long thread_num;
thread_num = (long)i;
// Timer for calculating 5 seconds
int timer = 0;
while (true)
{
timer++;
// Printing the message
cout << "Thread " << thread_num << " running." << endl;
if (timer == 5)
{
// Stopping the execution after 5 seconds
pthread_exit(NULL);
}
// Sleeping the thread for 1 second, so that it prints the message every 1 second
sleep(1);
}
}
int main()
{
int num_of_threads;
// Taking input from user
cout << "Enter the number of threads: ";
cin >> num_of_threads;
// Creating user passed number of threads
pthread_t threads[num_of_threads];
for (int i = 0; i < num_of_threads; i++)
{
// Calling the print_thread_info using each thread created
pthread_create(&threads[i], NULL, print_thread_info, (void *)i);
// Joining the threads so that main will wait until all the threads get executed and then only stops it's execution
(void) pthread_join(threads[i], NULL);
}
pthread_exit(NULL);
}
WINDOWS VERSION
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using std::chrono::system_clock;
void print_thread_info(int thread_num)
{
// Timer for calculating 5 seconds
int timer = 0;
while (true)
{
timer++;
// Printing the message
cout << "Thread " << thread_num << " running." << endl;
if (timer == 5)
{
// Stopping the execution after 5 seconds
break;
}
// Sleeping the thread for 1 second, so that it prints the message every 1 second
this_thread::sleep_for(chrono::seconds(1));
}
}
int main()
{
int num_of_threads;
// Taking input from user
cout << "Enter the number of threads: ";
cin >> num_of_threads;
// Creating user passed number of threads
thread* threads = new thread[num_of_threads];
for (int i = 0; i < num_of_threads; i++)
{
// Calling the print_thread_info using each thread created
threads[i] = thread(print_thread_info, i);
// Joining the threads so that main will wait until all the threads get executed and then only stops it's execution
threads[i].join();
}
}
OUPUT
Enter the number of threads: 4
Thread 0 running.
Thread 0 running.
Thread 0 running.
Thread 0 running.
Thread 0 running.
Thread 1 running.
Thread 1 running.
Thread 1 running.
Thread 1 running.
Thread 1 running.
Thread 2 running.
Thread 2 running.
Thread 2 running.
Thread 2 running.
Thread 2 running.
Thread 3 running.
Thread 3 running.
Thread 3 running.
Thread 3 running.
Thread 3 running.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.