Process Control Code (C)
This address: Http://blog.csdn.net/caroline_wendy
The output process ID,getpid ().
Code:
/*by C.l.wang * Eclipse CDT * Ubuntu 12.04 * 2014.10.5*/#include "apue.h" #include "error.h" int main (void) {printf ("Hello W Orld from Process ID%ld\n ", (long) getpid ()); exit (0);}
Output:
Hello World from Process ID 2260
Executes the command program, Fork () creates the process,EXECLP () executes the command, and the parent process waits for the child process to terminate waitpid ().
Code:
/*by C.l.wang * Eclipse CDT * Ubuntu 12.04 * 2014.10.5*/#include "apue.h" #include "error.h" #include <sys/wait.h>int Main (void) {char buf[maxline];p id_t pid;int status;printf ("percent"), while (Fgets (buf, MAXLINE, stdin)! = NULL) {if (Buf[strle N (BUF)-1] = = ' \ n ') {Buf[strlen (BUF)-1] = 0;} if (PID = fork ()) < 0) {Err_sys ("fork Error"),} else if (PID = = 0) {EXECLP (buf, buf, (char*) 0); Err_ret ("couldn ' t exec Ute:%s ", buf); exit (127);} if (PID = Waitpid (PID, &status, 0)) < 0) Err_sys ("Waitpid error");p rintf ("percent");} Exit (0);}
Output:
Linux-Process Control code (C)