Linux processes and Signals

Source: Internet
Author: User
Tags terminates

Like Linux and UNIX, there is a virtual memory system that can place program code and data in the form of a memory page in a hard disk area.

Linux can manage more processes than physical memory.

Progress table

View Processes

PS-ef

The tty column shows the process started from that terminal. The Time column is the CPU time occupied by the process so far.

The cmd column displays the commands used to start the process. The stat column is used to indicate the current code of the process.

Stat code explanation:

S  Sleep is usually waiting for an event, such as a signal or input.

R  Run, strictly speaking, it is runable, that is, it is in the running or about to be executed status in the running queue.

D  Uninterrupted sleep (waiting ). It is usually waiting for input and output to complete.

T  Stop.

Z  Botnets

N  Low-priority tasks

W  Paging (not applicable to versions earlier than 2.6)

S  Process First Process

+   The process belongs to the foreground process group.

L  Multithreading Process

<  High-priority process

Start a new process

# Include <stdlib. h>

INT system (const char * string );

The role of the system function is to run the command passed to it as a string parameter and wait for the completion of the command. Command Execution is like

Run the following commands in shell:

$ Sh-C string

1. Replace the process image

#include<unistd.h>int execl(const char *path, const char *arg0 , ... , (char *)0);int execlp(const char *file , const char *arg0, ... , (char *)0);int execle(const char *path , const char *arg0 , ... , (char *)0 , char *const envp[] );int execv(const char *path, const char *arg0 , ... , (char *)0);int execvp(const char *file , const char *arg0, ... , (char *)0);int execve(const char *path , const char *arg0 , ... , (char *)0 , char *const envp[] );

These functions can be divided into two types. The number of parameters for execl, execlp, and execle is variable, and the parameters end with a null pointer structure.

The function ending with the letter P searches for the PATH environment variable to find the path of the executable file of the program.

2. Copy the process image

#include<sys/types.h>#include<unistd.h>pid_t fork(void);

The fork call in the child process returns 0. The Parent and Child processes can use this to determine who is the parent process and who is the child process.

After fork is called, the parent process returns a new process PID and the child process returns 0.

#include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(){    pid_t pid;    char *message;    int n;    printf("fork program starting\n");    pid = fork();    switch(pid)     {    case -1:        perror("fork failed");        exit(1);    case 0:        message = "This is the child";        n = 5;        break;    default:        message = "This is the parent";        n = 3;        break;    }    for(; n > 0; n--) {        puts(message);        sleep(1);    }    exit(0);}

Wait for a process

#include<sys/types.h>#include<sys/wait.h>pid_t wait(int *stat_loc);

The wait system calls the parent process to know that its child process has ended. This call returns the PID of the child process, which is usually the PID of the child process that has ended running.

We can use the macro defined in sys/Wait. H to explain the status information. See Table 11-2.

Macro description

Wifexited (stat_val) takes a non-zero value if the child process ends normally.

Wexitstatuc (stat_val) If wifexited is not zero, it returns the exit code of the sub-process.

Wifsignaled (stat_val) If the child process terminates due to an uncaptured signal, it takes a non-zero value.

Wtermsig (stat_val) If wifsignaled is not zero, it returns a signal code.

Wifstopped (stat_val) takes a non-zero value if the child process unexpectedly terminates.

Wstopsig (stat_val) If wifstopped is non-zero, it returns a signal code

#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(){    pid_t pid;    char *message;    int n;    int exit_code;    printf("fork program starting\n");    pid = fork();    switch(pid)     {    case -1:        exit(1);    case 0:        message = "This is the child";        n = 5;        exit_code = 37;        break;    default:        message = "This is the parent";        n = 3;        exit_code = 0;        break;    }    for(; n > 0; n--) {        puts(message);        sleep(1);    }/*  This section of the program waits for the child process to finish.  */    if(pid) {        int stat_val;        pid_t child_pid;        child_pid = wait(&stat_val);        printf("Child has finished: PID = %d\n", child_pid);        if(WIFEXITED(stat_val))            printf("Child exited with code %d\n", WEXITSTATUS(stat_val));        else            printf("Child terminated abnormally\n");    }    exit (exit_code);}

Botnets

It is useful to use fork to create a process, but you must be clear about the running status of the sub-process. When a child process is terminated, its association with the parent process remains.

It will not end until the parent process is terminated normally or the parent process calls wait. Therefore, table items in the progress table that represent child processes are not immediately released. Although the Sub-Process

It is no longer running, but it still exists in the system, because its exit code needs to be saved for future wait calls by the parent process. Then it

Become a dead or zombie process.

Another system call can be used to wait for the completion of the sub-process, which is the waitpid function. You can use it to wait for a specific process to end.

# Include <sys/types. h>

# Include <sys/Wait. H>

Pid_t waitpid (pid_t PID, int * stat_loc, int options );

Input/Output redirection

#include <stdio.h>#include <ctype.h>#include <stdlib.h>int main(){    int ch;    while((ch = getchar()) != EOF) {        putchar(toupper(ch));    }    exit(0);}

/*  This code, useupper.c, accepts a file name as an argument    and will respond with an error if called incorrectly.  */#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    char *filename;    if(argc != 2) {        fprintf(stderr, "usage: useupper file\n");        exit(1);    }    filename = argv[1];/*  That done, we reopen the standard input, again checking for any errors as we do so,    and then use execl to call upper.  */    if(!freopen(filename, "r", stdin)) {        fprintf(stderr, "could not redirect stdin to file %s\n", filename);        exit(2);    }    execl("./upper", "upper", 0);/*  Don't forget that execl replaces the current process;    provided there is no error, the remaining lines are not executed.  */    perror("could not exec ./upper");    exit(3);}

The freopen () function redirects a stream to another stream.

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.