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 - once
every 3 seconds) get the semaphore SEM; once the thread has SEM, it will proceed to do the
following:
1. Open the file SHARED.txt and write the tid (thread id) of that thread (followed by a
Carriage Return and Newline)
2. Write to the console (print to stdout) "Thread is running" followed by a
newline
3. Close the file SHARED.txt
4. Release the semaphore SEM
2. Repeat 9 more times (each thread writes to the file and console a total of 10 times)
3. exit
You will need to use the following POSIX system calls for creating and managing the semaphores with:
sem_init(), sem_wait(), sem_post(), and sem_destroy().