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)
How can i modify my c code so that each number stored in the array is...
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main(). Below is the code: #include <stdio.h> #include <stdlib.h> #include <math.h> float Deg2Rad (float degrees) { // Calculate & return value float Result; Result = ((M_PI*degrees)/180.0); return (Result); } int main(void) { // Declare variables int Array[90]; int i; //...
hello please correct this code print("The Miles Per Gallon program") print() Trips = [] trip =...
hello please correct this code print("The Miles Per Gallon program") print() Trips = [] trip = 0 while 1: print("Do you want to add a trip from a csv file or Enter it manually? 1 for csv 2 for entering it manually") method = int(input()) if method == 1: print("Enter the filename") fileName = input() try: with open(fileName, 'r') as myFile1: reader = csv.reader(myFile1) Trips = list(reader) print("Miles Driven Gallons Used \tMPG") for i in Trips: for j in i:...
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...
Hi, I have this code so far and need to modify it so that the output...
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 = For example if I input...
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...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT