hw6:1. Process can be in different states to allocate the resources better. List
ID: 3551560 • Letter: H
Question
hw6:1. Process can be in different states to allocate the resources better. List the symbol and meaning for each of these states. (10 points)
2. What does it mean to spawn a process? (10 points)
3. How can you determine what processes are owned by the current login user? (10 points)
4. Compare and contrast a job number with a process identification number.(10 points)
5. For this questions, refer to the following listing: (each 10 points)
[2] Stopped sort /etc/passwd
[3] - Stopped vi .cshrc
[4] + Stopped more .login
[5] Running find / -name foo
(1) What command would you use to bring the vi editor to the foreground?
(2) What is the command you would use to kill the more?
(3) What is the command you would use to get this listing?
6. Compare and contrast background and foreground jobs. Include the syntax you use to place a command in foreground and in background.(10 points)
7. What is the function of jobs command? (10 points)
8. How do you bring a job from foreground to background? Write down the steps (10 points)
hw7:Q1. (Duplicate Elimination) Use a single-subscripted array to solve the following problem.
Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it
Explanation / Answer
2.
1. the kernel has to take an existing process and then copy all of it into another part of memory.
2. Next it creates kernel entries to support the new process.
3. Finally it gives control of the cpu to user space in the new process
4. (optional) load an executable image into the process overwriting the old process memory. exec() does this, fork() does not do this.
Steps 1-3 is what fork() does, it is commonly called spawning a new process. Step 4 is performed by exec(). They are very different. So when you type
3. ps -u "$(echo $(w -h | cut -d ' ' -f1 | sort -u))" o user= | sort | uniq -c | sort -rn
4.
The job id is mostly just a shell construct. There is support in the kernel in the form of the signals that are involved in job control, and the way in which the kernel knows exactly which processes to send the job control signals to.
Strictly speaking, the answer to your first question is that the job id is purely a shell creation. It exists because a pipeline (or, rarely, another shell grouped construct) may consist of multiple processes that should be controlled as a unit.
To answer your last question, the shell starts all processes by first doing a fork(2) and then doing an execve(2). The only difference with & is that the shell does not do a wait(2) (or a related variant) and so the program can continue "in the background". There is actually little distinction in Unix between foreground and background.
The process group is an association defined by shells so that the kernel knows about a single "foreground" process that handles a set of various "background" processes. This is mainly important so that the background processes will generate a signal should they decide to suddenly read from a terminal. (Such terminal probably being connected to standard input.) This will cause the "job" to generate a signal and the shell will prompt the user to do something.
Try (sleep 5; read x)& and after 6 seconds type a return or something so that the shell wakes up. That's when you see something like...
[1]+ Stopped ( sleep 5; read x )
...and you then type fg to pull it into the foreground.
Originally, Unix had pipelines, and it had &, but there was no way to move a command or pipeline between foreground and background and no way to help a background process that suddenly decided to read standard input.
Job control and the kernel support for it were added by Bill Joy and others in early versions of BSD and csh(1). These were picked up line-for-line by commercial Unix and in cloned for the work-alike Linux kernel.
Regarding the questions about process groups and ps(1)... In order to support job control in shells, the kernel process state includes a process group ID and a session ID. A process group and a job are the same thing, but a job number is just a handle the shell makes up. A process is a session leader if the session ID is the same as the pid, and a process is a process group leader if the pgid is the same as the pid. I believe something a bit more subtle is happening with the + that ps(1) prints. Each terminal knows what its foreground process group is, so I believe a process gets a + if pid == pgid && (pgid is the foreground pg for its controlling terminal).
In summary, the kernel keeps several items of state: pid, pgid, sid, and a process may have a controlling terminal and a terminal may have a foreground pgid. These credentials are mostly intended to support job control but are also used to revoke access to a terminal when a user logs out.
5.
1. fg 3
2. kill -9 4
3. ps
6. background processes are those which run in background while some other process is working in foreground.
To make a process come to foreground use fg <job_id> for background use bg<job_id>
7.
The jobs command displays the status of jobs started in the current shell environment. If no specific job is specified with the JobID parameter, status information for all active jobs is displayed. If a job termination is reported, the shell removes that job's process ID from the list of those known by the current shell environment.
The /usr/bin/jobs command does not work when operating in its own command execution environment, because that environment does not have applicable jobs to manipulate. For this reason, the jobs command is implemented as a Korn shell or POSIX shell regular built-in command.
8.
To bring a specific background job into the foreground:
1. Enter the fg (foreground) command followed by the job number.
If there is only one job running in the background just enter the fg command.
For example:
This starts a process to print a file as a background job. The user then checks the current active jobs and brings the print job - job number three (3) - into the foreground.
9.
10.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.