Question

In: Computer Science

Modify the following source code so that five subsequent threads print the message “Hello World number,”...

Modify the following source code so that five subsequent threads print the message “Hello World number,” where number indicates the unique thread created; e.g. “Hello World 1”

// Your_name_goes_here
#include <pthread.h>
#include <semaphore.h>

sem_t semaphore; // also a global variable just like mutexes

void *thread_function( void *arg )
{
sem_wait( &semaphore );
// perform some task
pthread_exit( NULL );
}

int main()
{
int tmp; tmp = sem_init( &semaphore, 0, 0 ); // initialize

pthread_create( &thread[i], NULL, thread_function, NULL );
sem_post( &semaphore );
pthread_join( thread[i], NULL );
sem_destroy( &semaphore );

return 0;
}

Solutions

Expert Solution

Here is the code,

#include <pthread.h>
#include <semaphore.h>
#include <iostream>

using namespace std;

sem_t semaphore; // also a global variable just like mutexes

void *thread_function( void *arg ) {
    sem_wait( &semaphore );
    long number;
    number = (long) arg;
    cout<<"Hello World "<<number<<endl;
    pthread_exit( NULL );
}

int main() {
    int tmp; 
    tmp = sem_init( &semaphore, 0, 0 ); // initialize
    pthread_t threads[5];
    for(int i=1; i<=5; i++){
        pthread_create( &threads[i], NULL, thread_function, (void *)i);
        sem_post( &semaphore );
        pthread_join( threads[i], NULL );
        sem_destroy( &semaphore );
    }
    return 0;
}

OUTPUT:

If you have any doubts then put in the comments. Also, do upvote the solution.


Related Solutions

Print "Hello World" in the most complicated way (java)
Print "Hello World" in the most complicated way (java)
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
Use your webcam and record a movie, write a motion detection code and print the message...
Use your webcam and record a movie, write a motion detection code and print the message "Motion Detected" on the original frame. Try to make a better visualization by using different masking. ---using Numpy and opencv
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through...
Modify the following code to count the number of recursive calls made for the Manhattan-path problem...
Modify the following code to count the number of recursive calls made for the Manhattan-path problem we studied earlier. Next, modify to include stored values and see how that reduces the number of calls made. public class ManhattanWithCallCount { public static void main (String[] argv) { // Test case 1: go from (2,2) to (0,0) int r = 2; int c = 2; int n = countPaths (r, c); System.out.println ("r=" + r + " c=" + c + "...
Parse the file /usr/src/include/minix/ipc.h (Struct message) Modify the newly created system call to receive and print...
Parse the file /usr/src/include/minix/ipc.h (Struct message) Modify the newly created system call to receive and print a parameter of type long through the structure “message” (the System_Calls_in_Minix.pdf file may be helpful). From the test file, the parameter must be sent to the user library created in the previous exercise, which in turn sends the parameter to the syscall. Your library must use the M4 type message. The system call must print the arbitrary number sent by the user code through...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
Modify the code so that it provides a web form to collect the cookie values from...
Modify the code so that it provides a web form to collect the cookie values from the users. In other words, you will add two text boxes where users will enter the cookie name and the cookie value. You will also add a submit button, which lets users to send the cookie values to the server. The page should display the previous cookie values when it is loaded. <!DOCTYPE html> <?php     $cookie_name = "user";     $cookie_value = "John Doe";...
How to validate Javascript form data? Here is the code. Can someone modify it so that...
How to validate Javascript form data? Here is the code. Can someone modify it so that all the information is validated? Thanks. <!DOCTYPE html> <html lang="en"> <head>    <title>Music Survey</title>    <meta charset="utf-8"> </head> <style>    legend { font-weight:bold;    }    </style> <body> <h1>Music Survey</h1> <form method="post" action=""> <label for="myname"><b>Name:</b></label>        <input type="text" name="myname" id="myname">        <br><br> <label for="myemail"><b>Email:</b></label>        <input type="email" name="myemail" id="myemail">        <br><br>   <fieldset> <legend>Select Your Favorite Types of Music:</legend> <input type="checkbox"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT