26. Linux process and signal---system function and process state switch

Source: Internet
Author: User

26.1 system function 26.1.1 function description

System (execute shell command)
Correlation function Fork,execve,waitpid,popen

1 #include <stdlib.h>2int system (constcharstring);
    • function functions: Simplifying the EXEC function
    • Function description
      • System () calls fork () to produce a child process that calls/bin/sh-c string to execute the command represented by the argument string string, which then returns the process that was originally called.
      • The SIGCHLD signal is temporarily shelved while the system () is being called, and the SIGINT and sigquit signals are ignored.
      • Equivalent to/bin/bash-c "cmd" or exec ("bash", "-C", "cmd")
    • return value
      • If system () fails when calling/bin/sh, it returns 127, and other failure reasons return-1.
      • If the argument string is a null pointer (null), a value other than 0 is returned.
      • If the system () call succeeds, the return value after executing the shell command is returned, but this return value may also be 127 of the return of the system () call to/bin/sh failure, so it is better to check the errno again to confirm that the execution was successful.
    • Additional Instructions
      • Do not use System () when writing programs with Suid/sgid permissions, and System () inherits environment variables, which can cause system security issues through environment variables.
26.1.2 System Application
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4 5 Char*cmd ="Date";6 7 intMainvoid)8 {9System"Clear");Ten system (CMD); One  A     return 0; -}

Compile execution

  

26.1.3 Building the Mysystem command
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <sys/types.h>5#include <sys/wait.h>6 7 Char*CMD1 ="Date > S1.txt";8 Char*CMD2 ="Date > S2.txt";9 Ten voidMysystem (Char*cmd) One { A pid_t pid; -     if(PID = fork ()) <0) { -Perror ("Fork Error"); theExit1); -}Else if(PID = =0) { -         if(EXECLP ("/bin/bash","/bin/bash","- C", CMD, NULL) <0) { -Perror ("EXECLP Error"); +Exit1); -         } +     }  A  atWait0); - } -  - intMainvoid) - { -System"Clear"); in system (CMD1); -  to Mysystem (CMD2); +  -     return 0; the}

Compile and debug

  

26.6 Process State Switching

  

    • Runnable: Ready state
    • Running: Run status
    • Block/suspend: Blocking or suspending State
    • Dead: Terminating state. The running state calls Return/exit_exit into the dead state
    • OS Scheduler: System scheduling

26. Linux process and signal---system function and process state switch

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.