嵌入式学习代码总结-进程(一)

#include<stdio.h>
int main(int argc,char *argv[])
{
	FILE *fd=NULL;
	fd=fopen(argv[1],"r");
	char ch;
	if(NULL==fd)
	{
		perror("open error\n");
		return -1;
	}
	while(-1!=(ch=fgetc(fd)))
	{
		fputc(ch,stdout);
	}
	fclose(fd);
	return 0;
}
/*************************************************************************
    > File Name: wait.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 04:18:48 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t pid=-1;
	pid=fork();
	if(-1==pid)
	{
		return -1;
	}
	if(0==pid)
	{
		printf("child process pid=%d\r\n",getpid());
		sleep(5);
		exit(3);
	}
	else
	{
		printf("parent process,pid=%d\r\n",getpid());
		int status=-1;
	//	pid_t  pidTmp=wait(&status);
		pid_t pidTmp=-1;
		while(0==(pidTmp=waitpid(pid,&status,WNOHANG)))
		{
			printf("hello test------------\r\n");
			sleep(1);
		}
		printf("pidTmp=%d,status=%d\r\n",pidTmp,WEXITSTATUS(status));
		sleep(10);
	}
}
/*************************************************************************
    > File Name: wait.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 04:18:48 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t pid=-1;
	pid=fork();
	if(-1==pid)
	{
		return -1;
	}
	if(0==pid)
	{
		printf("child process pid=%d\r\n",getpid());
		sleep(5);
		exit(3);
	}
	else
	{
		printf("parent process,pid=%d\r\n",getpid());
		int status=-1;
		pid_t  pidTmp=wait(&status);
		printf("pidTmp=%d,status=%d\r\n",pidTmp,WEXITSTATUS(status));
		sleep(10);
	}
}
/*************************************************************************
    > File Name: test.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 10:41:00 AM CST
 ************************************************************************/

#include<stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[],char *env[])
{
	while(argc--)
	{
		printf("%s\r\n",*argv++);
	}
	printf("--------------------------\r\n");
	while(*env)
	{
		printf("%s\r\n",*env++);
	}
	printf("#######################\r\n");
	printf("%s\r\n",getenv("PWD"));
	return 0;
}
/*************************************************************************
    > File Name: fork.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 01:22:16 PM CST
 ************************************************************************/

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
	pid_t pid=-1;
	printf("hello 22101--------\r\n");
	pid=fork();
	if(-1==pid)
	{
		return -1;
	}
	if(0==pid)//child process
	{
	//	sleep(10);
		printf("child process,pid=%d,parent pid=%d\r\n",getpid(),getppid());
	}
	else//pid>0 parent process
	{
		sleep(10);
		printf("parent process,parent pid=%d,pid=%d\r\n",getpid(),pid);
	}
	printf("hello world!-----------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: exit.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 03:39:40 PM CST
 ************************************************************************/

#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
	printf("hello test by@22101----");
//	return 0;
	//exit(0);
	_exit(0);
}
/*************************************************************************
    > File Name: execve.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 03:20:54 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>

int main()
{
	char *argv[]={"aaa","bbb","ccc",NULL};
	char *env[]={"PWD=22101test","shixh",NULL};
	if(-1==execve("test",argv,env))
	{
		printf("execve error!-----------\r\n");
		return -1;
	}
	return 0;
}


/*************************************************************************
    > File Name: execlp.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 02:55:48 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>

int main()
{
	printf("hello 22101--------\r\n");
	if(-1==execlp("ps","ps","axj",NULL))
	{
		printf("execlp error!-----------\r\n");
		return -1;
	}
	printf("hello world!---------\r\n");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值