Unix/Linux Programming Practice tutorial notes 8 processes and Programs: write command interpreter sh

Source: Internet
Author: User
PS-
-Option A lists all processes, including those running by other users on other terminals. Program But the output with option-a Does not include shell.
PS-l

Administrator @ Ubuntu :~ $ PS-l
F s uid PID ppid C pri Ni addr sz wchan tty time cmd
0 r 1000 7600 6949 1 80 0-1585-pts/0 00:00:00 bash
0 r 1000 7617 7600 0 80 0-628-pts/0 00:00:00 PS

A column named s indicates the status of each process. If the value of column S is R, the process corresponding to PS is in progress. If the values in the S column of other processes are "S", they are all in sleep state. Each process belongs to the user ID specified by the UID column. Each process has a process ID and a parent process ID.
Pri: Process Priority
Ni: niceness level
SZ: memory size occupied
Wchan: displays the cause of sleep. Read_c do_sel indicates the kernel address

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
Int main ()
{
Char * Arglist [3];
Arglist [0] = "ls ";
Arglist [1] = "-l ";
Arglist [2] = 0;
Printf ("*** about to Exec LS-L \ n ");
Execvp ("ls", Arglist );
Printf ("have done \ n ");
Return 0;
}

Administrator @ Ubuntu :~ /Test $./exec1
* ** About to Exec LS-l
Total usage 36
-Rwxr-XR-x 1 administrator 9083 exec1
-RW-r -- 1 administrator 237 exec1.c
-RW-r -- 1 administrator 992 exec1.o
-Rwxr-XR-x 1 administrator 9118 execlp
-RW-r -- 1 administrator 251 execlp. c

Execvp has two parameters: the program to be run and the command line parameter array of the program.

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define maxargs 20
# Define arglen 100
Int main ()
{
Char * Arglist [maxargs + 1];
Int numargs;
Char argbuf [arglen];
Char * makestring ();
Numargs = 0;
While (numargs <maxargs)
{
Printf ("Arg [% d] =", numargs );
If (fgets (argbuf, arglen, stdin) & * argbuf! = '\ N ')
Arglist [numargs ++] = makestring (argbuf );
Else
{
If (numargs> 0)
{
Arglist [numargs] = NULL;
Execute (Arglist );
Numargs = 0;
}
}
}
Return 0;
}

Int execute (char * Arglist [])
{
Execvp (Arglist [0], Arglist );
Perror ("execvp failed ");
Exit (1 );
}

Char * makestring (char * BUF)
{
Char * CP;
Buf [strlen (BUF)-1] = '\ 0 ';
CP = malloc (strlen (BUF) + 1 );
If (Cp = NULL)
{
Fprintf (stderr, "no memory \ n ");
Exit (1 );
}
Strcpy (CP, Buf );
Return CP;
}

A process ends in one of three ways (success, failure, or death.
1. A process may successfully complete its tasks.
2 process may fail exit (non-0 value)
3. A process is killed by a signal. The signal may come from the keyboard, interval timer, kernel, or other processes.

Wait () returns the PID of the ending child process to the parent process. How does the parent process know how the child process exits?
The answer is in the parameter passed to wait. When the parent process calls wait, an integer variable address is sent to the function. If the child process calls exit to exit, then the kernel stores the exit return value in this integer variable. if the process is killed, the kernel stores the signal number in this variable. This integer is composed of three parts-eight bits are record exit values, seven bits are record signal sequences, and the other bits are used to indicate errors and generate core dump)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.