1. When your program starts, it shall do the following: 1. Create a file, SHARED
ID: 3557954 • Letter: 1
Question
1. When your program starts, it shall do the following:
1. Create a file, SHARED.txt, in the current directory (cwd).
2. Write it's pid (Process ID) followed by a Carriage Return and Newline in the file.
3. Close the file SHARED.txt
4. Create a semaphore named SEM which the threads will use to manage access to the file
SHARED.txt.
5. Create 6 threads. Use the POSIX version of threads (i.e., pthread_create())
6. Block/wait for all six threads to complete their work.
7. Destroy the semaphore, then exit gracefully, printing a friendly message to the console
2. Each thread shall perform the following (note, each thread is running concurrently):
1. Periodically (even numbered threads - once every two seconds, odd numbered threads
Explanation / Answer
/***************** Run This File on g++ compiler( LINUX) name this file as thread.cpp compile by g++ thread.cpp -o thread -lpthread run as ./thread if any other method you use make sure you have linked pthread library *********************/ #include #include #include #include #include #include #include #define NUM_THREADS 6 using namespace std; sem_t SEM; ofstream of; void *BusyWork(void *t) { int i; long tid; tid = (long)t; for (i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.