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...
Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number...
Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number of comparisons it makes during a sort and displays the totals. To count comparisons, you will need to break up the double condition in the inner while loop. Use this program to measure the number of copies and comparisons for different amounts of inversely sorted data. Do the results verify O(N2 ) efficiency? Do the same for almost-sorted data (only a few items out...
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...
WILL UPVOTE ANSWER! Modify the given code to accept two integers from the user and print...
WILL UPVOTE ANSWER! Modify the given code to accept two integers from the user and print one of the following messages - the 1st number is bigger - the 2nd number is bigger - two numbers are equal ###################################### .data str1 : .asciiz "enter the 1st integer " str2 : .asciiz "enter the 2nd integer " str3 : .asciiz "The 1st num is bigger" str4 : .asciiz "The 2nd num is bigger" str5 : .asciiz "Two numbers are equal" .text...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT