#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;
}