• Linux


    进程控制 代码(C)


    本文地址:http://blog.csdn.net/caroline_wendy


    输出进程IDgetpid().

    代码:

    /*By C.L.Wang
     * Eclipse CDT
     * Ubuntu 12.04
     * 2014.10.5*/
    
    #include "apue.h"
    #include "error.h"
    
    int main(void) {
    	printf("hello world from process ID %ld
    ", (long)getpid());
    	exit(0);
    }
    

    输出:

    hello world from process ID 2260
    


    运行命令程序。 fork()创建进程,execlp()运行命令,父进程等待子进程终止waitpid()

    代码:

    /*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];
    	pid_t pid;
    	int status;
    
    	printf("%% ");
    	while (fgets(buf, MAXLINE, stdin) != NULL) {
    		if (buf[strlen(buf) - 1] == '
    ') {
    			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 execute: %s", buf);
    			exit(127);
    		}
    
    		if ((pid = waitpid(pid, &status, 0)) < 0)
    			err_sys("waitpid error");
    		printf("%% ");
    	}
    
    	exit(0);
    }
    


    输出:





  • 相关阅读:
    获取配置文件
    windows下多tomcat部署
    tomcat是否有必要配置环境变量(摘)
    js删除map中元素
    HDU-3440 House Man
    HDU-1534 Schedule Problem
    POJ-1364/HDU 1531 King
    POJ-1275/HDU-1529 Cashier Employment
    POJ-1201/HDU-1384 Intervals
    HDU-5780 gcd
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6761693.html
Copyright © 2020-2023  润新知