Working With Processes

Audio version created with Paper2Audio.

Listen on Paper2Audio

Working With Processes

In this activity, you work in teams of 2 similar 3 students to learn new programming concepts.
unlabeled summary: The table is a header for a section titled "Forming Group" and includes a field for "Start time:". It indicates the beginning of an activity where students form teams to learn programming concepts.

Content Learning Objectives

1. Understanding fork and execve system calls 2. Understanding fork behavior 3. Exploring how Shell works
unlabeled summary: The table outlines the roles and responsibilities of team members in an activity focused on learning programming concepts. The Project Manager, Wony Nguyen, is responsible for reading questions, managing time, and ensuring participation, including making and sharing document copies. The Recorder, Han Dang, is tasked with documenting answers and confirming team consensus. The Hacker, Kevin Shan, is responsible for coding and execution.
(30 min) Activity 1. fork(), execve(), and waitpid()
Start time: Q1. For each of the following system calls, fork() and execve(), use the manual pages to identify the errors returned. List the names and reasons for three different errors for each of these system calls. Please use the manual pages in the edlab environment.
Answer: <Forkgreater than 1. ENOMEM fork() failed to allocate the necessary kernel structures because memory is tight. 2. EAGAIN A system-imposed limit on the number of threads was encountered due to a. the RLIMIT NPROC soft resource limit (set via setrlimit(2)), which limits the number of processes and threads for a real user ID, was reached; b. the kernel's system-wide limit on the number of processes and threads, /proc/sys/kernel/threads-max, was reached (see proc(5)); c. the maximum number of PIDs, /proc/sys/kernel/pid max, was reached (see proc(5)); or d. the PID limit (pids.max) imposed by the cgroup "process number" (PIDs) controller was reached. 3. ENOSYS fork() is not supported on this platform (for example, hardware without a Memory-Management Unit). <Execvegreater than 1. ENOEXEC An executable is not in a recognized format, is for the wrong architecture, or has some other format error that means it cannot be executed. 2. E2BIG The total number of bytes in the environment (envp) and argument list (argv) is too large. 3. EFAULT pathname or one of the pointers in the vectors argv or envp points outside your accessible address space.
Q2. In class we discussed process creation and the fork() system call. We also illustrated the output of particular programs using fork() and their output.
See the example below. Draw a diagram, such as the one to the below, for the code in the right: Answer: Copy/Paste these components to build your plot Q3. Given the following process diagram, please list all the possible output in different process schedules. Hint: one of the possible outputs is "L0 Bye L1 Bye L2 Bye".
Answer: 1. "50 Bye 51 Bye 52 Bye" 2. "L0 Bye 51 52 Bye Bye" 3. "50 Bye 1 Bye Bye 2" 4. "50 Bye Bye Bye L1 L2" 5. "50 Bye Bye L1 L2 Bye" 6. "50 Bye Bye L1 Bye L2" 7. "L0 L1 Bye Bye Bye L2" 8. "L0 L1 Bye Bye L2 Bye" 9. "L0 L1 Bye L2 Bye Bye" 10. "L0 L1 L2 Bye Bye Bye" Q4. Review the code below and identify the processes (P, A, B, C) that will enter the zombie state if the program is not manually interrupted. Provide concise explanations, using one or two sentences, to describe the reasons for their potential entry into the zombie state.
Hint: For each process P, A, B, C identify two things - whether it finishes execution and whether it becomes a zombie if it does.
Answer: P: never finishes and does not become a zombie because it will be stuck on line 9 waiting for process C to finish, which never happens because it is in an infinite while loop.
A: finishes almost immediately but becomes a zombie because the parent process P is stuck on line 9 and never reaches line 11 to reap process A B: the same happens to process B as process A. It finishes execution but is never reaped.
C: never finishes because it is in an infinite while loop, but doesn't become a zombie.
Image summary: The image is a diagram illustrating the output of a program using the fork() system call. The diagram shows a sequence of processes being created, with each process printing a specific string. The program begins by printing "L0", then enters a loop where it forks a new process. Each process prints either "L1" or "Bye" depending on whether it is the parent or child process. The diagram illustrates the order in which the strings are printed by the different processes. The initial process prints L0, then the process forks, with the parent printing L1 and the child printing Bye. The forking and printing process repeats, with each new child process printing Bye. The diagram shows the branching structure of the processes and their respective outputs.
Algorithm 1 summary: The code represents the start of a main function in a C program. It serves as the entry point where program execution begins.
Algorithm summary: This code prints "L0" to standard output. It is a relatively simple operation.
Algorithm 3 forces the standard output buffer to be written to the terminal or console. It takes no input and produces no specific output, but ensures that any previously printed content is immediately displayed.
Code summary: The code iterates through a loop where the loop variable i goes from one to two. This loop is a for loop.
Algorithm 5 summary: This code segment checks if a new process is created using the fork function. The fork function duplicates the current process. If fork returns a non-zero value (the process ID of the child process) in the parent process, the conditional statement is true.
Code summary: This code prints a string with a number. The number is determined by the loop variable i.
Code summary: This code flushes the standard output stream. It ensures that any buffered output is immediately written to the console or output device.
Code summary: This code snippet is part of a program that uses the fork system call within a loop. The loop iterates twice, and in each iteration, it attempts to create a new process using fork. If the fork is successful (creating a child process), the child process prints a label and flushes the output.
Code summary: This code contains a loop that iterates twice. Inside the loop, a fork system call is made. If the fork is successful, the code prints a letter and the loop index. After the loop, the code prints another word.
Code summary: This line of code prints "Bye" followed by a newline character to standard output.
Code summary: This code forces the standard output buffer to be written. It does not take any input and does not return anything.
Code summary: The code returns a zero value. It is the last line of a C program.
Code summary: This is the end of a C code block, specifically the closing curly brace of the main function. It does not take any input and does not produce any output on its own. It signifies the completion of the program's execution.
Image summary: The image shows process diagrams. The diagrams illustrate possible outputs in different process schedules. The diagrams show different sequences of "Bye", "L0", "L1", and "L2", demonstrating various execution paths.
Image summary: The image is a process diagram. The diagram illustrates a process flow starting with L0, then branching to L1 and L2, with each branch eventually leading to "Bye". The diagram shows different possible paths and sequences in which the process can execute, ultimately resulting in the "Bye" output at various stages.
Image summary: The image is a process diagram. The diagram illustrates three processes, L0, L1, and L2, each ending with a "Bye" output. The processes are structured sequentially, with L1 starting after L0 and L2 starting after L1. The diagram depicts the order in which these processes are executed and terminated.
Algorithm summary: The code creates a parent process and three child processes. The parent waits for the children to complete, but one child enters an infinite loop. This causes the parent to wait indefinitely, and the other children become zombie processes because the parent never reaps them.
Algorithm summary: The code returns zero. It is a simple block of code that likely represents the end of a function or program.
(20 min) Activity 2. Shell
Start time: Download the `starter code`, complete the `fork exec` function and test it on the edlab machine. In this question, you will need to write a function to fork a process and use a child process to handle user commands. Hint: please do not use exit in the parent process since Shall is designed to continually run user commands.
Reference: execvp example Q5. Complete the fork exec function Good job! You have worked with Processes.
Create a pdf of this worksheet file and upload it. **Remember each member of the group needs to do this individually!
You have reached the end of the document.