Question

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


#include <stdio.h> #include <pthread.h> #include <assert.h> void proc(void *arg) printf(arg=ld\n, (long)arg); pthread exit(
#include <stdio.h> #include <pth read.h> #include <assert.h> void sproc(void sarg) { int i; for(i-0;i<5;i+) return (void *) (
#include <stdio.h> #include <pthread. h> #include cassert.h> #include <unistd. h> void proc(void arg) sleep(2); printf(arg=l
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 string both before and after conversion on two separate lines. Hint: it is dangerous to have printing code in your thread function (s). You may refer to testThread.s testThread2.c, pthread ex1.c, pthread ex2.c for examples. Note: if you do not use pthread to divide the conversion task among the threads, you may get zero points.
#include #include #include void proc(void *arg) printf("arg=ld\n", (long)arg); pthread exit((void )99);//if we have this function call, the main will print out "result-99" instead of "result=2016" return (void ) 2016 ; int main() int r pthread_t x; void *result; I/when pthread_creste and pthread_join successfully execute, they will return rapthread_create (&x, NULL, proc, Tvoid *)34); assert(r0); r=pthread join (x, &result); assert(r); printf("result=ld\n", (long) result ) ; return ;
#include #include #include void sproc(void sarg) { int i; for(i-0;i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1->

#include <iostream>
#include <pthread.h>
#include <string>
#include <cstdlib>
    using namespace std;
char a[5][12];
int i, j;
void *convert(void *temp1)
{
    a[i][j] = 'A' + (25 - (a[i][j] - 'A')) % 26;
    return (void *)NULL;
}
int main(int argc, char **argv)
{
    if (argv[1][0] > '6' || argv[1][0] < '2')
    {
        cout << "Incorrect Arguments";
    }
    for (i = 0; i < 5; i++)
        for (j = 0; j < 12; j++)
            a[i][j] = 'A' + rand() % 26;
    pthread_t Threads[5][12];
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 12; j++)
            cout << a[i][j] << " ";
    cout << endl;
    for (i = 0; i < 5; i++)
        for (j = 0; j < 12; j++)
        {
            pthread_create(&Threads[i][j], NULL, convert, (void *)NULL);
        }
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 12; j++)
        {
            pthread_join(Threads[i][j], NULL);
        }

    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 12; j++)
            cout << a[i][j] << " ";
}

OUTPUT

NWLRB B MQBH CDARZ0WK K Y HID D Q SCDXRJ MOWFR XS JY BLD BEFSARCBYNECDY GGX NDORYBNJYS X WARALD P P Y H RDWQ H X WCIQNL DUICS

I HAVE CREATED BETTER CODE IF YOU HAVE ANY ISSUE OR PROBLEM COMMENT IT DOWN

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...
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
  • Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusi...

    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 string both before...

  • Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By...

    Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By walking through this example you’ll learn: * - How to use pthread_create(). * - How to use pthread_exit(). * - How to use pthread_join(). * */ #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <sys/wait.h> void* ninja(void* arg){ printf("Who’s there?"); fflush(stdout); pthread_exit("ninja"); } // // Expected output: // // Knock knock. // Who's there? - from ninja // Knuc...kles. // int main(int argc, char* argv[]){...

  • 1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually...

    1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually differs. a) Why is the variable myglobal usually not 40? Be specific, be precise. b) In what special case would myglobal be 40? Be specific, be precise. #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* Purpose: Use 2 threads to increment myglobal exactly 40 times in total. Compile: using -pthread option */ int myglobal = 0; void *thread_function(void *arg) {   ...

  • i want to fix the solution and provide an explanation why the pthread_join is not working...

    i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...

  • Please explain your answers thoroughly for the following question. 1) Compile thread2.c (remember the -lpthread flag)....

    Please explain your answers thoroughly for the following question. 1) Compile thread2.c (remember the -lpthread flag). Run it numerous times, and notice the output usually differs. a) Why is the variable myglobal usually not 40? Be specific, be precise. b) In what special case would myglobal be 40? Be specific, be precise. #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* Purpose: Use 2 threads to increment myglobal exactly 40 times in total. Compile: using -pthread option */...

  • Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusiv...

    Write a C or C++ program A6pc(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 string both before...

  • Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be...

    Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c=0,x=0; void *UpdateC1(void *arg) {    int i;    for(i=0;i<2000000;i++)   {        sem_wait(&s1);        c++;   x++;        sem_post(&s1);    } } void *UpdateC2(void *arg) {    int i,x=0;    for(i=0;i<2999999;i++)   {        sem_wait(&s1);        c++;   x++;       ...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • 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,...

  • //In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 +...

    //In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 + 3*3 + 4*4 + ... + n*n // Note we should know from CSE2500 that this sum is // n*(n+1)*(2*n+1)/6 // We a n value, we will create 2*n threads to do the calculation so that // we can have a race condition. // Before you change the code, read the code and run the code and see what // happens. Do we already...

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