Question

Need help with this C program based on threads. If someone could help as soon as...

Need help with this C program based on threads. If someone could help as soon as possible thanks in advance.

Write a multi-threaded C program using pthread_create() according to the following instructions:

  1. Your program should create exactly 2 new threads in the main() function, each of which invokes a different function. Print the thread IDs of the two new threads after they are created.

  1. The first newly created thread invokes the following function

void *print_string_in_reverse_order(void *str)  {

      // this function accepts a string as an argument

      // it prints the string in reverse order

      // your code goes here

}

When this thread is created in the main() function, the argument passed to the function print_string_in_reverse_order() is the following string:

char * str = “Hello World!”;

  1. The second newly created thread invokes the following function

void *print_in_uppercase_letters(void *str)  {

      // this function accepts a string as an argument

      // it prints the string in uppercase letters

      // your code goes here

}

When this thread is created in the main() function, the string argument passed to the function print_in_uppercase_letters() is a line of text typed by the user from keyboard.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The source code is followed by the output snapshot.

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

void *print_string_in_reverse_order(void *str) {
    // this function accepts a string as an argument
    // it prints the string in reverse order
    // your code goes here
    char *s = str;
    while(*s != '\0') s++; // go to end of string
    while(s != (char*)str)
        printf("%c", *--s);
    return NULL;
}

void *print_in_uppercase_letters(void *str) {
    // this function accepts a string as an argument
    // it prints the string in uppercase letters
    // your code goes here
    char *s = str;
    for(; *s != '\0'; s++)
    {
        if(*s >= 'a' && *s <= 'z')
            printf("%c", *s + 'A' - 'a');
        else
            printf("%c", *s);
    }
}

int main()
{
    pthread_t t1, t2;
    int ret;
    char * str = "Hello World!";
    char usrstr[200];

    scanf("%[^\n]s", usrstr);

    pthread_create(&t1, NULL, print_string_in_reverse_order, str);
    printf("Thread id 1: %lu\n", t1);

    pthread_create(&t2, NULL, print_in_uppercase_letters, usrstr);
    printf("Thread id 2: %lu\n", t2);

    //wait for threads to end
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    printf("\n");
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Need help with this C program based on threads. If someone could help as soon as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • I need help writing this program in C++. Thanks so much for your help in advance...

    I need help writing this program in C++. Thanks so much for your help in advance Function 1: Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, ... 50. Use recursion to calculate the sum. Demonstrate the function in a program. A sample...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

  • Need some help on the following JAVA problem: Write a program in which the main thread...

    Need some help on the following JAVA problem: Write a program in which the main thread creates two threads named “Display” and “Daemon”. The first thread (Display) executes in an infinite loop and prints out its name and system time every 1 second until it is interrupted by the main thread. The second thread is a daemon thread that executes in an infinite loop and prints out its name. The main thread waits for a specific number of seconds n...

  • I need to creatre a C++ program that can modify the producer and consumer. My main...

    I need to creatre a C++ program that can modify the producer and consumer. My main programmig language is C++. Modify "Producer and Consumer Problem" from lecture note so that it can use all buffer space, not "buffersize – 1" as in the lecture note. This program should work as follows: 1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The main...

  • Debugging and testing multithreaded programs is made more difficult compared to dealing with single-threaded programs by...

    Debugging and testing multithreaded programs is made more difficult compared to dealing with single-threaded programs by: 1. the necessity to divide activities into separate and concurrent tasks 2. the existence of many more different execution paths made possible by parallel thread execution 3. multithreading library APIs with confusing semantics -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Consider the following code that creates N threads using the POSIX threading library. The thread function threadFun receives as parameter a thread index (from 0 to N-1). #define NTHREADS 4...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT