Question

In: Computer Science

Write a C program that creates 5 threads sends the thread index as an argument to...

Write a C program that creates 5 threads sends the thread index as an argument to the thread execution procedure/function. Also, the main process/thread joins the newly created threads sequentially one after the other. From the thread procedure print “I am a thread and my index is “ [print the correct index number]. From the main thread after the join print “I am the main thread and just completed joining thread index “ [print the correct index].

Solutions

Expert Solution

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

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void *print_message_function( void *ptr )
{
    pthread_mutex_lock( &mutex1 );
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
      pthread_mutex_unlock( &mutex1 );
     return 0;
}

int main()
{
    pthread_t thread1,thread2,thread3,thread4,thread5;
    char *message1 = "I am a thread and my index is 1";
    char *message2 = "I am a thread and my index is 2";
    char *message3 = "I am a thread and my index is 3";
    char *message4 = "I am a thread and my index is 4";
    char *message5 = "I am a thread and my index is 5";
  
     int  i1,i2,i3,i4,i5;

    /* Create independent threads each of which will execute function */

    i1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
    i2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
    i3 = pthread_create( &thread3, NULL, print_message_function, (void*) message3);
    i4 = pthread_create( &thread4, NULL, print_message_function, (void*) message4);
    i5 = pthread_create( &thread5, NULL, print_message_function, (void*) message5);

    pthread_join( thread1, NULL);
    printf("I am the main thread and just completed joining thread index : %lu\n",thread1);
   
    pthread_join( thread2, NULL); 
    printf("I am the main thread and just completed joining thread index : %lu\n",thread2);
    
    pthread_join( thread3, NULL); 
    printf("I am the main thread and just completed joining thread index : %lu\n",thread3); 
   
    pthread_join( thread4, NULL);
    printf("I am the main thread and just completed joining thread index : %lu\n",thread4);
   
    pthread_join( thread5, NULL);   
    printf("I am the main thread and just completed joining thread index : %lu\n",thread5);

    exit(0);
}


Related Solutions

Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program accepts inputs from the user from the console. The user can provide the following two types of input: add num1 num2 mult num1 num2 Here, num1 and num2 are two integer numbers. E.g., the user may input add 1 2. The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Write a C code to let the main thread create N child threads, where each created...
Write a C code to let the main thread create N child threads, where each created thread will randomly generate an integer between 0 to 10 and put it into a global array variable. After that, the main thread will calculate the sum of all the generated integers. N should be input as a command line argument. Complete the following C code by filling all “???”s in the code sketch. NOTE: when you compile the code, you need to add...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a file without the file becoming corrupted. To do this, your program will create three threads which write strings to the same file. Each thread will randomly write a selection of strings to the file at random intervals. When finished, the file will contain all the strings written correctly to the file. You may use mutexes, semaphores, or a monitor your write on your own....
Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT