zoukankan      html  css  js  c++  java
  • 第十二周学习笔记

    exec1

    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    	char	*arglist[3];
    
    	arglist[0] = "ls";
    	arglist[1] = "-l";
    	arglist[2] = 0 ;//NULL
    	printf("* * * About to exec ls -l
    ");
    	execvp( "ls" , arglist );
    	printf("* * * ls is done. bye");//若执行成功,就无法返回,所以打印不出这句话
    
    	return 0;
    }
    

    execvp():会从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。
    返回值:
    如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。

    exec2

    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    	char	*arglist[3];
    
    	arglist[0] = "ls";
    	arglist[1] = "-l";
    	arglist[2] = 0 ;
    	printf("* * * About to exec ls -l
    ");
    	execvp( arglist[0] , arglist );//这里将arglist[0]中的值作为执行文件。
    	printf("* * * ls is done. bye
    ");
    }
    

    和exec1,结果差不多

    exec3

    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    	char	*arglist[3];
    	char*myenv[3];
    	myenv[0] = "PATH=:/bin:";
    	myenv[1] = NULL;
    
    	arglist[0] = "ls";
    	arglist[1] = "-l";
    	arglist[2] = 0 ;
    	printf("* * * About to exec ls -l
    ");
    //	execv( "/bin/ls" , arglist );
    //	execvp( "ls" , arglist );
    //  execvpe("ls" , arglist, myenv);
    
    	execlp("ls", "ls", "-l", NULL);
    	printf("* * * ls is done. bye
    ");
    }
    

    函数说明:
    execlp()会从PATH 环境变量所指的目录中查找符合参数file的文件名,找到后便执行该文件,然后将第二个以后的参数当做该文件的argv[0]、argv[1]……,最后一个参数必须用空指针(NULL)作结束。如果用常数0来表示一个空指针,则必须将它强制转换为一个字符指针,否则将它解释为整形参数,如果一个整形数的长度与char * 的长度不同,那么exec函数的实际参数就将出错。如果函数调用成功,进程自己的执行代码就会变成加载程序的代码,execlp()后边的代码也就不会执行了.
    返回值:
    如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno 中。

    forkdemo1

    #include	<stdio.h>
    #include<sys/types.h>
    #include<unistd.h>
    int main()
    {
    	int	ret_from_fork, mypid;
    	mypid = getpid();//取得进程识别码			   
    	printf("Before: my pid is %d
    ", mypid);//打印进程pid
    	ret_from_fork = fork();//产生子进程
    	sleep(1);//休眠1秒
    	printf("After: my pid is %d, fork() said %d
    ",//打印
    			getpid(), ret_from_fork);
    
    	return 0;
    }
    

    *Sleep()单位为毫秒,sleep()单位为秒(如果需要更精确可以用usleep单位为微秒)
    这里返回两次!

    forkdemo2

    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
    	printf("before:my pid is %d
    ", getpid() );
    	fork();
    	fork();
    	printf("aftre:my pid is %d
    ", getpid() );
    
    	return 0;
    }
    

    这里使用了两个fork,那么就会有2^2个返回。

    forkdemo3

    #include	<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    int main()
    {
    	int	fork_rv;
    
    	printf("Before: my pid is %d
    ", getpid());
    
    	fork_rv = fork();		/* create new process	*/
    
    	if ( fork_rv == -1 )		/* check for error	*/
    		perror("fork");
    	else if ( fork_rv == 0 ){ //利用pid来判断是否是子进程
    		printf("I am the child.  my pid=%d
    ", getpid());
    	
    		exit(0);
    	}
    	else{
    		printf("I am the parent. my child is %d
    ", fork_rv);
    		exit(0);
    	}
    
    	return 0;
    }
    

    forkdemo4

    #include	<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    int main()
    {
    	int	fork_rv;
    
    	printf("Before: my pid is %d
    ", getpid());
    
    	fork_rv = fork();		/* create new process	*/
    
    	if ( fork_rv == -1 )		/* check for error	*/
    		perror("fork");
    
    	else if ( fork_rv == 0 ){ 
    		printf("I am the child.  my pid=%d
    ", getpid());
    		printf("parent pid= %d, my pid=%d
    ", getppid(), getpid());
    		exit(0);
    	}
    
    	else{
    		printf("I am the parent. my child is %d
    ", fork_rv);
    		sleep(10);
    		exit(0);
    	}
    
    	return 0;
    }
    

    forkgdb

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int  gi=0;
    int main()
    {
    	int li=0;
    	static int si=0;
    	int i=0;
    
    	pid_t pid = fork();
    	if(pid == -1){
    		exit(-1);
    	}
    	else if(pid == 0){
    		for(i=0; i<5; i++){
    			printf("child li:%d
    ", li++);
    			sleep(1);
    			printf("child gi:%d
    ", gi++);
    			printf("child si:%d
    ", si++);
    		}
    		exit(0);
    		
    	}
    	else{
    		for(i=0; i<5; i++){
    			printf("parent li:%d
    ", li++);
    			printf("parent gi:%d
    ", gi++);
    			sleep(1);
    			printf("parent si:%d
    ", si++);
    		}
    	exit(0);	
    	
    	}
    	return 0;
    }
    

    这个效果是并发带来的。当parent打印两个之后休息1秒,这时候child开始打印,打印一个之后,开始休息,这时刻parent可以开始打印了,打印一轮(si第一次gi,li第二次)后,又遇到sleep,再次跑child……造成了这种现象。

    psh1

    #include	<stdio.h>
    #include	<stdlib.h>
    #include	<string.h>
    #include<unistd.h>
    
    #define	MAXARGS		20				
    #define	ARGLEN		100				
    
    int execute( char *arglist[] )
    {
    	execvp(arglist[0], arglist);		
    	perror("execvp failed");
    	exit(1);
    }
    
    char * makestring( char *buf )
    {
    	char	*cp;
    
    	buf[strlen(buf)-1] = '';		
    	cp = malloc( strlen(buf)+1 );		
    	if ( cp == NULL ){			
    		fprintf(stderr,"no memory
    ");
    		exit(1);
    	}
    	strcpy(cp, buf);		
    	return cp;			
    }
    
    int main()
    {
    	char	*arglist[MAXARGS+1];		
    	int		numargs;			
    	char	argbuf[ARGLEN];			
    
    	numargs = 0;
    	while ( numargs < MAXARGS )
    	{					
    		printf("Arg[%d]? ", numargs);
    		if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '
    ' )
    			arglist[numargs++] = makestring(argbuf);
    		else
    		{
    			if ( numargs > 0 ){		
    				arglist[numargs]=NULL;	
    				execute( arglist );	
    				numargs = 0;		
    			}
    		}
    	}
    	return 0;
    }
    

    执行输入的命令,空参数返回

    psh2

    #include	<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<unistd.h>
    #include	<signal.h>
    
    #define	MAXARGS		20				
    #define	ARGLEN		100				
    
    char *makestring( char *buf )
    {
    	char	*cp;
    
    	buf[strlen(buf)-1] = '';		
    	cp = malloc( strlen(buf)+1 );		
    	if ( cp == NULL ){			
    		fprintf(stderr,"no memory
    ");
    		exit(1);
    	}
    	strcpy(cp, buf);		
    	return cp;			
    }
    
    void execute( char *arglist[] )
    {
    	int	pid,exitstatus;				
    
    	pid = fork();					
    	switch( pid ){
    		case -1:	
    			perror("fork failed");
    			exit(1);
    		case 0:
    			execvp(arglist[0], arglist);		
    			perror("execvp failed");
    			exit(1);
    		default:
    			while( wait(&exitstatus) != pid )
    				;
    			printf("child exited with status %d,%d
    ",
    					exitstatus>>8, exitstatus&0377);
    	}
    }
    
    int main()
    {
    	char	*arglist[MAXARGS+1];		
    	int		numargs;			
    	char	argbuf[ARGLEN];			
    
    	numargs = 0;
    	while ( numargs < MAXARGS )
    	{					
    		printf("Arg[%d]? ", numargs);
    		if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '
    ' )
    			arglist[numargs++] = makestring(argbuf);
    		else
    		{
    			if ( numargs > 0 ){		
    				arglist[numargs]=NULL;	
    				execute( arglist );	
    				numargs = 0;		
    			}
    		}
    	}
    	return 0;
    }
    

    上一个函数的循环模式,并在成功执行一次后,并显示当前函数调用的子程序一个什么状态= =

    testbuf1

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	printf("hello");
    	fflush(stdout);
    	while(1);
    }
    

    打印一个hello,还不让我退出

    testbuf2

    #include <stdio.h>
    int main()
    {
    	printf("hello
    ");
    	while(1);
    }
    

    打印一个hello,还不让我退出

    testbuf3

    #include <stdio.h>
    
    int main()
    {
    	fprintf(stdout, "1234", 5);
    	fprintf(stderr, "abcd", 4);
    }
    

    打印一个1234abcd

    testpid

    #include <stdio.h>
    #include <unistd.h>
    
    #include <sys/types.h>
    
    int main()
    {
    	printf("my pid: %d 
    ", getpid());
    	printf("my parent's pid: %d 
    ", getppid());
    	return 0;
    }
    

    打印一个进程和他的父进程的pid

    testpp

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	char **pp;
    	pp[0] = malloc(20);//pp[0]就是*pp
    
    	return 0;
    }
    

    访问的内存超出了系统所给这个程序的内存空间

    testsystem

    #include	<stdlib.h>
    
    int main ( int argc, char *argv[] )
    {
    
    	system(argv[1]);
    	system(argv[2]);
    	return EXIT_SUCCESS;
    }				/* ----------  end of function main  ---------- */
    

    system()—执行shell命令也就是像dos发送一条指令

    函数说明:
    system()会调用fork()产生子进程, 由子进程来调用/bin/sh-c string 来执行参数string 字符串所代表的命令, 此命令执行完后随即返回原调用的进程. 在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT 和SIGQUIT 信号则会被忽略.

    返回值:
      1、如果 system()在调用/bin/sh 时失败则返回127, 其他失败原因返回-1.。
      2、若参数string 为空指针(NULL), 则返回非零值.
      3、如果system()调用成功则最后会返回执行shell 命令后的返回值, 但是此返回值也有可能为system()调用/bin/sh 失败所返回的127, 因此最好能再检查errno 来确认执行成功.

    这就是同时执行两个指令

    waitdemo1

    #include	<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<unistd.h>
    
    #define	DELAY	4
    
    void child_code(int delay)
    {
    	printf("child %d here. will sleep for %d seconds
    ", getpid(), delay);
    	sleep(delay);
    	printf("child done. about to exit
    ");
    	exit(17);
    }
    
    void parent_code(int childpid)
    {
    	int wait_rv=0;		/* return value from wait() */
    	wait_rv = wait(NULL);
    	printf("done waiting for %d. Wait returned: %d
    ", 
    			childpid, wait_rv);
    }
    int main()
    {
    	int  newpid;
    	printf("before: mypid is %d
    ", getpid());
    	if ( (newpid = fork()) == -1 )
    		perror("fork");
    	else if ( newpid == 0 )
    		child_code(DELAY);
    	else
    		parent_code(newpid);
    
    	return 0;
    }
    

    waitdemo2

    #include	<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<unistd.h>
    
    #define	DELAY	10
    
    void child_code(int delay)
    {
    	printf("child %d here. will sleep for %d seconds
    ", getpid(), delay);
    	sleep(delay);
    	printf("child done. about to exit
    ");
    	exit(27);
    }
    
    void parent_code(int childpid)
    {
    	int wait_rv;	
    	int child_status;
    	int high_8, low_7, bit_7;
    
    	wait_rv = wait(&child_status);
    	printf("done waiting for %d. Wait returned: %d
    ", childpid, wait_rv);
    
    	high_8 = child_status >> 8; /* 1111 1111 0000 0000 */
    	low_7  = child_status & 0x7F;   /* 0000 0000 0111 1111 */
    	bit_7  = child_status & 0x80;   /* 0000 0000 1000 0000 */
    	printf("status: exit=%d, sig=%d, core=%d
    ", high_8, low_7, bit_7);
    }
    
    int main()
    {
    	int  newpid;
    
    	printf("before: mypid is %d
    ", getpid());
    
    	if ( (newpid = fork()) == -1 )
    		perror("fork");
    	else if ( newpid == 0 )
    		child_code(DELAY);
    	else
    		parent_code(newpid);
    }
    

    参考资料:

    1、关于函数execvp

    http://baike.baidu.com/link?url=0rhuZXP0bq0UiDG23UnajLP5icf41REasjtkxu8s4dyDGAHHoZb975snEtYMdpGGjETLcz622p81gzev-oIs9a

    2.关于函数 execlp

    http://baike.baidu.com/link?url=DcuefHbGBzIGro-KJp-hfzutFbTbH7aKet1-9oMJTbmHlj2EMsqwTjdA0H4GeeS0hivVU_DWzh_VhowoFDlsga

    3.system

    http://baike.baidu.com/view/4053255.htm

  • 相关阅读:
    洛谷P1357 Solution
    洛谷P3469 Solution
    洛谷P2617 Solution
    CF818F Solution
    CF802K Solution
    CF519E Solution
    在代码中改变log的级别
    Java非对称加密解密
    mvn test 远程调试
    rsyn实现服务器源码同步
  • 原文地址:https://www.cnblogs.com/midori/p/5005395.html
Copyright © 2011-2022 走看看