ResourceCST-321 Activity 2 GuideContentsProcesses in Linux1Signals in Linux3Threads in Linux4Mutexes and Semaphores in Linux5Research Questions6Appendix A – Sample...

Can you help with writing C programs


Resource CST-321 Activity 2 Guide Contents Processes in Linux1 Signals in Linux3 Threads in Linux4 Mutexes and Semaphores in Linux5 Research Questions6 Appendix A – Sample Programs7 Processes in Linux Overview In this activity, students will learn about processes using the Linux fork() and posix_spawn() functions. Execution Execute this assignment according to the following guidelines: 1. Read the following article, tutorials, and videos located in the Topic Materials: a. Read the article "The fork() System Call." b. View the video "Linux fork() Introduction." c. View the video playlist "Learning C Programming Lesson 21: Calling Function and Recursive." d. Download the Intro to C Programming training deck. i. Study Modules 3 and 4. 2. Per guidance from your instructor, create a small C program with the following capabilities: a. From the main() function using the fork() function to create a parent and child process. b. The child process should print 10 messages to the console and sleep for 1 second between message then exit the process with a return code of 0. c. The parent process should print 10 messages to the console and sleep for 2 seconds between messages, then exit the process with a return code of 0. d. After reading the article "The fork() System Call,” write a theory of operation explaining how the code worked. 3. Per guidance from your instructor, create a small C program with the following capabilities. a. From the main() function accept a command line argument that will be used to spawn off the passed in application. b. Use the POSIX posix_spawn() function to spawn off the application. c. Print the process ID to the console. d. Wait for the process to end by calling the waitpid() function. e. Research the documentation on the posix_spawn() and waitpid() functions and write a theory of operation for how the code worked. Signals in Linux Overview In this activity, students will learn about signals using the Linux kill and signal functions. Execution Execute this assignment according to the following guidelines: 1. View the following video located in the Topic Materials: "Sending and Handling Signals in C (Kill, Signal, Sigaction)" 2. Per guidance from your instructor, create a small C program with the following capabilities: a. From the main() function using the fork() function to create a parent producer and child consumer process. b. The child consumer process should create a signal called WAKEUP, sleep until that signal is received, then once received run a loop for 20 iterations printing a message to the console and sleeping for 1 second for each iteration, then exit the process with a return code of 1. c. The parent producer process should run a loop for 30 iterations printing a message to the console and sleeping for 1 second for each iteration and once a count of 5 has been reached send the WAKEUP signal, then exit the process with a return code of 1. d. After reading the article "The fork() System Call,” and reading the documentation on signals, write a theory of operation explaining how the code worked. Threads in Linux Overview In this activity, students will learn about threads using the Linux POSIX thread functions. Execution Execute this assignment according to the following guidelines: 1. View the following videos located in the Topic Materials: a. "Introduction to Pointers in C/C++" b. "Pointers in C" c. "Pointers and Dynamic Memory – Stack vs Heap" d. "Pthreads #1: Introduction" e. Download the Intro to C Programming training deck. i. Study Modules 3 and 4. 2. Per guidance from your instructor, create a small C program with the following capabilities: a. From the main() function using the pthread_create() function to create thread 1 and thread 2. b. The thread 1 function should print 10 messages to the console and sleep for 1 second between messages, then exit the thread with a return code of null. c. The thread 2 function should print 10 messages to the console and sleep for 2 seconds between messages, then exit the thread with a return code of null. d. The main() function should use the pthread_join() function to wait for each thread to exit before exiting the main program. e. NOTE: make sure to use the -pthread flag as an option in the gcc compiler! f. Research the documentation on the POSIX thread functions and write a theory of operation for how the code worked. Mutexes and Semaphores in Linux Overview In this activity, students will learn about mutexes using the Linux POSIX mutex and semaphore functions. Execution Execute this assignment according to the following guidelines: 1. View the following videos located in the Topic Materials: a. "Mutex Synchronization in Linux wit Pthreads" b. "C Programming in Linux Tutorial #046 – Semaphore Pthread" c. "What Is Difference Between Semaphore and Mutex" 2. Per guidance from your instructor, create a small C program with the following capabilities: a. Write a bad bank program that simulates bank deposit transactions. b. From the main() function using the pthread_create() function to create 2 threads. c. Each thread will call the same function, which simulates a bank deposit transaction, and should sit in a loop for each least 1,000,000 transactions, adding 1 (simulating $1 deposit) to a global variable representing a bank balance that starts out with a value of 0. The thread should return null after all deposits have been made. d. Run the program. The expected bank balance should be $2,000,000 (2 threads each depositing $1,000,000). Write a theory of operation explaining why the program did not behave as expected and address the offending line or lines of code that are causing an issue. e. NOTE: make sure to use the -pthread flag as an option in the gcc compiler! 3. Per guidance from your instructor, create a small C program with the following capabilities: a. Fix the bad bank program by using POSIX mutexes. b. Run the program. The expected bank balance should be $2,000,000 (2 threads each depositing $1,000,000). Write a theory of operation explaining why the program now behaves properly with the mutexes. c. NOTE: make sure to use the -pthread flag as an option in the gcc compiler! 4. Per guidance from your instructor, create a small C program with the following capabilities: a. Fix the bad bank program by using POSIX semaphores. b. Run the program. The expected bank balance should be $2,000,000 (2 threads each depositing $1,000,000). Write a theory of operation explaining why the program now behaves properly with the semaphores. c. NOTE: make sure to use the -pthread flag as an option in the gcc compiler! Research Questions Research Questions: Online students will address these in the Discussion Forum and traditional on ground students will address them in this assignment.  0. Consider the following code for a process P0 (assume turn has been initialized to zero): --- Etc. --- while (turn != 0) {} Critical Section /* … */ turn = 0; --- Etc. --- For process P1, the code is: --- Etc. --- while (turn != 1) {} Critical Section /* … */ turn = 1; --- Etc. --- Does the above code meet all the required conditions for a correct mutual-exclusion solution? Explain and justify your answer. 0. Consider the following C code: void main() { fork(); fork(); exit(); } How many child processes are created upon execution of this program? Run the code on your computer and support your answers with relevant screenshots. Submission   1.              In a Microsoft Word document, complete the following for the Activity Report:  a.              Cover Sheet with the name of this assignment, date, and your name.  b.              Section with a title that contains all theory of operation write-ups, answers to questions asked in the activity, and any screenshots taken during the activity.  c.              Section with a title that contains the answers to the Research Questions (traditional ground students only).  Submit the Activity Report to the learning management system (LMS).  Appendix A – Sample Programs The following can be used as guidance to program the C programs in the activity. Linux Process Examples Linux Thread Example Linux Mutex and Semaphore Examples Linux Signal Example © 2021. Grand Canyon University. All Rights Reserved. © 2021. Grand Canyon University. All Rights Reserved.
Jan 10, 2023
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here