1、父进程杀死子进程
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>int main()
{pid_t pid;pid = fork();if(pid < 0){perror("fork failed");}else if(pid == 0){int i =0;for(i = 0; i < 5; i++){printf("in child process\n");sleep(1);}}else{printf("in father process\n");sleep(2);printf("kill child process now\n");int nRet = kill(pid, SIGINT);printf("nRet = %d\n", nRet);}return 0;
}
2、定时自杀
#include <stdio.h>
#include <unistd.h>int main()
{int sec = 0;sec = alarm(5);printf("sec = %d\n", sec);sleep(1);sec = alarm(5);printf("sec = %d\n", sec);while(1);return 0;
}
3、自己实现文件的拷贝cp
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>int main(int argc, char *argv[])
{int fd_src, fd_dest;char rdBuf[1024] = { 0 };int nRet;if( argc < 3 ){printf("parameter num error");return 0;}fd_src = open(argv[1], O_RDONLY);if( fd_src < 0 ){perror("open src");return 0;}fd_dest = open(argv[2], O_WRONLY|O_CREAT, 0666);if( fd_dest < 0 ){perror("open dest");return 0;}while(1){nRet = read(fd_src, rdBuf, 1024);if( nRet <= 0){break;}write(fd_dest, rdBuf, nRet);}close(fd_src);close(fd_dest);return 0;
}