Ana səhifə

A script of the Shell Commands for Project4

Yüklə 25.43 Kb.
ölçüsü25.43 Kb.
A Script of the Shell Commands for Project4

First to compile your project, use the command listed below. It is also listed in the project text outline:

gcc -o myshell myshell.c lex.yy.c -lfl

jobs - lists the jobs

wait - hold until all jobs are done
notify - wait until job is done
stop - cancels the job
fg - turns background job into foreground job
suspend - turns active background job into a suspended background job
bg - turns suspended background job into active background job
timex - reports time usage of process

1) Jobs: (use the command man jobs at the UNIX prompt)

Depending on the shell used, generally it lists the PID (process ID) of the jobs running (could be in background).

The Unix command jobs allows you to see a list of all the jobs you have invoked from the current shell. The shell will list the job ID of each job, along with the status (running, stopped, or otherwise), and the command that started the job. The shell considers the most recently-created (or most recently-manipulated) job to be the current job, marked with a plus sign. Other jobs are referred to as previous jobs, and are marked with a minus sign. The commands related to job control will apply to the current job, unless directed to do otherwise. You may refer to jobs by job ID by using the percent sign. Thus, job 1 is referred to as %1, job 2 is %2, and so forth.
The C Shell associates a job with each pipeline. It keeps a table of current jobs, printed by its jobs command, and assigns them small integer numbers. When a job is started asynchronously with &', csh displays a line that looks like:

[1] 1234

showing that the job that was started asynchronously was job number 1 and had one (top-level) process, whose process id was 1234. A job being run in the background stops if it tries to read from the terminal.

The C-shell has a facility that controls the execution of programs and scripts. The C-shell command, jobs lists the job number, the status of the job, and the command that generated them. You can execute a program in background mode in one of two ways.

1. Enter the program name followed by an ampersand (&).


     program &
   Then enter jobs. 

   The following message is displayed.

     [1]     + Running program
2. To suspend a running program or script, enter CTRL-Z. 

   The following message is displayed.

   To verify that the job is suspended, enter jobs, which 

   displays the following message.

     [1]     + Stopped program
   Enter the C-shell command, bg to place the suspended job 

   in background mode.

   To resume the job, bring it back into the foreground with the

   C-shell command, fg. Again, you can verify the results with 

   the jobs command, which displays
     [1]    + Running program

You can also use C-shell job control to terminate jobs using the kill command with the job number displayed by the jobs command.

        Syntax: kill -9 %jobnumber
2) Wait: (use man wait at the UNIX prompt) await process completion.


The unsigned decimal integer process ID of a command, for which the utility is to wait for the termination.


A job control job ID that identifies a background process group to be waited for. The job control job ID notation is applicable only for invocations of wait in the current shell execution environment, and only on systems supporting the job control option.

EXAMPLE: wait 2017 - Wait on pid 2017 until termination

3) notify: (see man notify)

notify [%job]

notify user immediately on change of status of job (specified by %job)

Reports immediately when a background job completes.

Script follows:
% jobs

% echo $status


% prog1 &

[1] 5913

% prog2

% -z


% bg

[2] 5924

% jobs

[1] + 5913 Suspended (tty output) prog1

[2] 5924 Running prog2

% fg


bin fertig

% jobs

[2] + 5924 Running prog2

% %2


  • A job is an executed command line

  • The jobs command will show you the list of all of your shell's jobs that are either suspended or executing in the background

The following are various ways to reference a job:


Where n is the job number reported by jobs


Your current job


Your current job


Your previous job


Job whose command line begins with string


Job whose command line contains string

- Job control allows you to do the following:

  • To background a process, type an & at the end of the command line

  • You can suspend the process running on the foreground by pressing ctrl+z

  • To restart a process execute: fg %jobnumber

  • Typing fg with no job number foregrounds the last suspended process

  • To kill a job running in the foreground type ctrl+c

  • To kill a suspended or background process, type: kill %jobnumber

  • Alternatively, processes can be killed by finding their process numbers (PIDs) and using kill PID_number

  • The wait command built into most shells will wait for completion of all background processes or a specific background process

  • The command wait will wait for all background jobs

  • The command wait pid will wait for a particular PID

  • The command notify will tell csh to give asynchronous notification of job completion

  • The command notify jobid will tell csh to give asynchronous notification for a particular job

  • The time command is used to time a simple command.

  • A job can terminate after a hard kill.

  • A job terminates when executed until complete (normal termination).

  • Your shell program has to deal with the two situations.

  • Use the kill() method with the SIGKILL signal to terminate a job instantaneously (static removal).

  • Use interrupts and signals to remove (dynamic removal) a job that terminates normally from the background queue. With the help of the waitpid() function, passing WNOHANG as an option, try to remove every job that terminates normally. (I will discuss this in details in today’s recitation).

  • Use the kill() method with the SIGSTOP signal to suspend a job.

  • The wait() and waitpid() functions allow the calling process to obtain status information pertaining to one of its child processes.

  • The wait() function will suspend execution of the calling process until status information of one of its terminated child processes is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. If status information is available prior to the call to wait(), return will be immediate.

  • The waitpid() function will behave identically to the wait() function, if pid argument has a value of -1 and the options argument has a value of zero. If pid is equal to zero, status is requested for any child process whose process group ID is equal to that of the calling process.

  • The option argument WNOHANG if passed won’t let the waitpid() function suspend the execution of the calling process if status is not immediately available for one of the child processes specified by pid.

  • If the wait() or waitpid() functions return because the status of a child process is available, these functions will return a value equal to the process ID of the child process for which status is reported. If the wait() or waitpid() functions return due to the delivery of a signal to the calling process, -1 will be returned and errno will be set to EINTR.

  • Use kill() method with SIGCONT signal to turn suspended background job into active background job.

  • Use kill() method with SIGCONT signal to turn background job into foreground job. Use also the waitpid() function to disallow the prompt from being available to the user.

  • To report time usage of the process use the time() function from time.h library.

  • The time() function returns the value of the time in seconds since the epoch.

Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur © 2016
rəhbərliyinə müraciət