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
I HAVE CREATED BETTER CODE IF YOU HAVE ANY ISSUE OR PROBLEM COMMENT IT DOWN
PLEASE GIVE A THUMBS UP
1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...
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 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 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 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). 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
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 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. 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 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 + 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...