solution:
1)
#include <stdio.h>
#include <stdlib.h> // Header file for rand()
#include <unistd.h> //Header file for sleep()
#include <pthread.h> // header for pthread library
// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *hello_world()
{
sleep(rand()); // sleep
for random amount of seconds
printf("Hello world\n");
// print hello world
}
void *goodbye(){
sleep(rand()); // sleep for random amount of
seconds
printf("Good bye \n");
// print goodbye
}
int main()
{
pthread_t thread1,
thread2; // create thread variable which stores thread id of the
thread
// create the thread
passing addresss to thread id and second argument is to attributes
of thread if
// NULL default is
used
// third is the function
to executed by thread
// arguments to the
function executed by thread
pthread_create(&thread1, NULL, hello_world, NULL);
pthread_create(&thread2, NULL, goodbye, NULL);
// exit when threads
are complete
pthread_exit(NULL);
}
2)
#include <stdio.h>
#include <stdlib.h> // Header file for rand()
#include <unistd.h> //Header file for sleep()
#include <pthread.h> // header for pthread library
// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *bellcurve_grad(void *argp){
sleep(1); //sleep for 1
second
float *grades = (float
*)argp; // cast the argument to float because pthread sends all
arguments to function as a void pointer
float bellcurve =
*grades * 1.5;
printf("%f\n",
bellcurve); // print the bellcurve
}
int main()
{
// get grades of five
students
float grades[5];
for(int
i=0;i<5;i++){
printf("Enter grade of student %d: ", i+1);
scanf("%f", &grades[i]);
}
pthread_t thread1,
thread2, thread3, thread4, thread5; // create thread variable which
stores thread id of the thread
// create the thread
passing addresss to thread id and second argument is to attributes
of thread if
// NULL default is
used
// third is the function
to executed by thread
// arguments to the
function executed by thread must be casted as void pointer
//
pthread_create(&thread1, NULL, bellcurve_grad, (void
*)&grades[0]);
pthread_create(&thread2, NULL, bellcurve_grad, (void
*)&grades[1]);
pthread_create(&thread3, NULL, bellcurve_grad, (void
*)&grades[2]);
pthread_create(&thread4, NULL, bellcurve_grad, (void
*)&grades[3]);
pthread_create(&thread5, NULL, bellcurve_grad, (void
*)&grades[4]);
// exit when threads are
complete
pthread_exit(NULL);
}
please give me thumb up
C - language please 1. Create a program that does the following, make sure you can...
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: 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. The first newly created thread invokes the following function void *print_string_in_reverse_order(void *str) { // this function accepts...
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...
C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...
Multi-threaded programming help!!! Can anyone solve this problem? It has to be written in C, runs on Linux. his time you need to rewrite a multithreaded Pthread program that works with sleep() or pthread_join() in order to print out Fibonacci sequences properly. Create a folder name, WK6 on your class Linux machine when you work. gcc assignment3.c-Wall -Werror -pthread You are required to develop your own program using C with the following information Need to declare a function that receives...
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...
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 <<...
Using C++ Please make sure you comment each section of the program so I can better understand it. Thank you! Assignment Content You are now working for a bank, and one of your first projects consists of developing an application to manage savings accounts. Create a C++ program that does the following: Creates a SavingsAccount class Uses a static data member, annualInterestRate, to store the annual interest rate for each of the savers Ensures each member of the class contains...
The goal of this assignment is to write a multithreaded program (Task3.c) that explores synchronization challenge 1. Assume that we have a shared variable CurrentiD. This is initialized to 1 at the beginning. 2. Now create 5 threads in your program and assign ID 1,2,3,4,5 to them respectively. You can pass the ID as a parameter when you create the threads. Each of the threads will try to access the variable "CurrentiD" Whenever a thread acquires the variable, it checks...
I want to create a program that first calculate the summation from 1 to R. This code will computes the summation from 1 to R with single thread. #include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #define NuMBer_THREADS 2 #define R 1e4 long long sum = 0; // matrix multiplication is c = a * b; void calculatesum() { int i=1; for(i=1;i<=N;i++) { sum+=i; asleep(100); } } // Thread function // Each thread calculates the summation over (R...