This program assignment is provided to let the students know how to handle threa
ID: 3741330 • Letter: T
Question
This program assignment is provided to let the students know how to handle threads and enhance system availability on a multiprocessor or multicore environment. A single process is supposed to create multiple threads with runner functions which include individual threads roles. Threads can be created by using Pthreads API, Win32 API, or Java API on Unix/Linux or Windows platform environment. Environment: Unix/Linux environment (VM Linux or Triton Server), Windows platform Language: C, C++ Java Requirements L You have wide range of choices for this assignment. First, design your program to explain the basic concept of the process management in Unix Kernel. This main idea will be evolved to show your understanding on inter-process communication, file processing, etc. ii. Refer to the following thread API: PosixThreads.html -CreateThreadd, CloseHandle), ResumeThread0,... Thread handling with Java AP *http://www.javabe ili. The program should create at least two different threads and assign corresponding runner functions with the threads v. The output should contain the screen capture of the execution procedure of the program. v. Runner function may contain any logic you've designed, for example login procedure by opening session, collection of bank account information, catalog information display, audio/image data handling, etc vi. Result should be organized as a document which explains the overview of your program, code, execution results, and the conclusion including justification of your program, lessons youve learned, comments, etc.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// Using one global variable
int var = 0;
// This function is called whenever a new thread starts execution
void *runThread(void *vargp)
{
int *myid = (int *)vargp;
// Using one local variable along with global variable
static int varLocal = 0;
// Print the argument, static and global variables. Notice the variations in the values being printed by these threads and the order in which it is printed
printf("Thread ID: %d, Value of local variable: %d, Value of global variable: %d ", *myid, ++varLocal, ++var);
++varLocal;
++var;
}
int main()
{
int i;
pthread_t tid;
printf("Begin Multithreading ");
// Create thread using pthread_create, all the threads start executing function runThread as soon as they are created
pthread_create(&tid, NULL, runThread, (void *)1);
pthread_create(&tid, NULL, runThread, (void *)2);
pthread_create(&tid, NULL, runThread, (void *)3);
pthread_create(&tid, NULL, runThread, (void *)4);
// pthread_join() is used as a barrier where all the threads have to wait for others to finish
pthread_join(tid, NULL);
printf("All the threads have finished ");
pthread_exit(NULL);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.