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.
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.
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...
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
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