zoukankan      html  css  js  c++  java
  • 实验六 进程基础

    项目 内容
    这个作业属于哪个课程 2020春季Linux系统与应用(南昌航空大学 - 信息工程学院)
    这个作业的要求在哪里 作业链接,点这里啦!
    学号-姓名 17041517-漆京
    作业学习目标 1、掌握Linux系统环境C语言编程概念

    2、学习Linux系统进程概念

    1,请举例说明静态链接库的创建与使用。

    ar:静态函数库创建的命令
    -c :create
    -r :replace
    表示当前插入的模块名已经在库中存在,则替换同名的模块;
    如果若干模块中有一个模块在库中不存在,ar显示一个错误信息,并不替换其他同名的模块。
    
    //文件名:add.c,加法
    int add(int a,int b){
    return a+b;
    }
    
    //文件名:sub.c,减法
    int sub(int a,int b){
    return a-b;
    }
    
    //文件名:main.c
    #include <stdio.h>
    int add(int a,int b);
    int sub(int a,int b);
    int main(){
    printf("3 + 1 = %d
    ",add(3,1));
    printf("3 - 1 = %d
    ",sub(3,1));
    return 0;
    }
    

    2、请举例说明共享库的创建与使用。

    #include <iostream>
    using namespace std;
    //文件名:common.h
    #ifndef _COMMON_
    #define _COMMON_
    int add(int a,int b)
    int sub(int a,int b)
    #endif
    
    开始的目录结构:
    

    观察下面的目录结构,与开始的目录结构对比:
    
    (1)创建共享库
    

    (2)使用自己的共享库
    方式一:指定相对路径
    方式二:只给链接器动态库名称(若要正常实现,后面必须添加一个环境变量)
    

    3、 编程实现一个简单文件复制命令。(文件I/O)

    man命令简介:
    man是manual(手册)的缩写,使用man这个命令可以调阅其中的帮助信息
    man的使用方法:
    使用命令"man [N] passwd"其中N(1-9,n)参数指定手册页的类型
    -a:在所有的man帮助手册中搜索 
    -f:显示给定关键字的简短描述信息
    -P:指定内容时使用分页程序
    -M:指定man手册搜索的路径
    
    #include <unistd.h> 
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <fcntl.h>
    #include <stdio.h> 
    #define BUFFERSIZE 4096 
    int main(int argc, char* argv[]) { 
    	if (argc != 3) {
    		printf("usage:
     mycp src dst
    "); 
    		return 1;
    	}
    	int srcfd = open(argv[1], O_RDONLY); 
    	if (srcfd == -1) { 
    		perror("open"); 
    		return 1; 
    	}
    	int dstfd = open(argv[2], O_CREAT | O_WRONLY, 0666);
    	if (dstfd == -1) { 
    		close(srcfd);
    		perror("open"); 
    		return 1; 
    	}
    	int len = 0; 
    	char buffer[BUFFERSIZE] = {0};
    	while((len = read(srcfd, buffer, BUFFERSIZE)) > 0) { 
    		if (write(dstfd, buffer, len) != len) { 
    			perror("write error");
            	return 1;
    		} 
    	}
    	if (len < 0) { 
    		perror("read error"); 
    		return 1;
    		}
    	close(srcfd); // 关闭文件 
    	close(dstfd); 
    	return 0; 
    }
    

    比较复制前后文件的异同:
    

    4、使用 fork 创建一个子进程,进程创建成功后父子进程分别输出不同的内容。

    fork函数简介
    (1)依赖的头文件 #include <unistd.h>
    (2)fork的原理和概念:
    fork子进程就是从父进程拷贝一个新的进程出来,子进程和父进程的进程ID不同,但用户数据一样。
    (3)父进程和子进程
    执行fork函数后有2种返回值:
    对于父进程,返回的是子进程的PID(即返回一个大于0的数字);
    对于子进程,则返回0,所以我们可以通过pid这个返回值来判断当前进程是父进程还是子进程。
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main(){
    	pid_t pid;
    	printf("[%d]:Begin! 
    ",getpid());
    	fflush(NULL);
    	pid = fork();
    	if(pid<0)
    	{
    		perror("fork()");
    		exit(1);
    	}
    	else if(pid > 0)
    	{
    		printf("[%d]:Parent process if working!
    ",getpid());
    	}
    	else
    	{
    		printf("[%d]:Child process if working!
    ",getpid());
    	}
    	printf("[%d]:Finish!
    ",getpid());
    	return 0;
    }
    

    全缓冲:
    全缓冲指的是系统在填满标准IO缓冲区之后才进行实际的IO操作;
    注意,对于驻留在磁盘上的文件来说通常是由标准IO库实施全缓冲。
    行缓冲:
    在这种情况下,标准IO在输入和输出中遇到换行符时执行IO操作;
    注意,当流涉及终端的时候,通常使用的是行缓冲。
    
    删除fflush(NULL);//查看运行结果区别
    ./fork1 > /tmp/out 输出结果删除后子程序工作前多个 Begin!
    

    进一步删除15行 
     ,查看运行结果区别
    删除后 Begin! 与 父进程、子进程间不换行
    

    5、 使用fork创建多个子进程。

    int i; 
    pid_t pid; 
    for (i = 0; i < 3; i++) 
    pid = fork();
    

    上面代码段会产生多少子进程?

    2^3-1=7
    产生7个子进程
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main(){
    	int i;
    	pid_t pid;
    	printf("[%d] Begin! 
    ",getpid());
    	for (i = 0;i < 3; i++)
    	{
    		if((pid = fork()) ==0 )
    			break;
    	}
    	if(pid<0)
    	{
    		perror("fork()");
    		exit(1);
    	}
    	else if(pid > 0)
    	{
    		printf("[%d] Parent process is working!
    ",getpid());
    	}
    	else
    	{
    		printf("[%d] Child process %d is working!
    ",getpid(),i);
    	}
    	return 0;
    }
    

    使用sleep函数简单控制进程输出顺序
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main(){
    	int i;
    	pid_t pid;
    	printf("[%d] Begin! 
    ",getpid());
    	for (i = 0;i < 3; i++)
    	{
    		if((pid = fork()) ==0 )
    			break;
    	}
    	if(pid<0)
    	{
    		perror("fork()");
    		exit(1);
    	}
    	else if(pid > 0)
    	{
    		sleep(3);
    		printf("[%d] Parent process is working!
    ",getpid());
    	}
    	else
    	{
    		sleep(i);
    		printf("[%d] Child process %d is working!
    ",getpid(),i+1);
    	}
    	return 0;
    }
    

    6、在 fork 之前以写的方式创建了一个文件 test.txt。然后 fork 出的子进程立即向文件中写入

    “world”,然后睡眠5秒。而父进程在 fork 后睡眠3秒后向 test.txt 写入 "hello",并关闭描述符。子

    进程恢复后,又向 test.txt 文件中写入 "lalala"后关闭描述符,结束。

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    int main() {
    	int fd = open("test.txt",O_WRONLY | O_CREAT,0664);
    	if (fd == -1){
    		perror("open");
    		return 1;
    	}
    	printf("I'm father
    ");
    	printf("before fork
    ");
    	pid_t pid = fork();
    	if (pid > 0){
    	sleep(3);
    	printf("I'm father; I'm writing test.txt...
    ");
    	write(fd, "hello", 5);
    	close(fd);
    	}
    	else if (pid ==0){
    	printf("I'm child; I'm writing test.txt...
    ");
    	write(fd, "world", 5);
    	sleep(5);
    	write(fd, "lalala", 6);
    	close(fd);
    	}
    	else {
    		perror("fork");
    		return 1;
    	}
    	return 0;
    }
    
    

    7、分别在主函数中使用

    execvp 启动 ls 命令以及使用 fork 函数产生子进程调用 execvp 启动 ls 。

    程序调用execvp,实现一个程序运行另一个程序
    
    (1)使用execvp启动ls命令
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main(){
    	char* argv[] = {"ls","-l",NULL};
    	if (execvp("ls",argv) == -1){
    	perror("exec");
    	return 1;
    	}
    	return 0;
    }
    

    (2)使用fork函数产生子进程调用execvp启动ls
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main(){
    	char* argv[] = {"ls","-l",NULL};
    	pid_t pid = fork();
    	if (pid > 0){
    		printf("I'm father
    ");
    	}
    	else if (pid == 0) {
    		printf("I'm child
    ");
    		if (execvp("ls",argv) == -1){
    			perror ("exec");
    			return 1;
    		}
    	}
    	else {
    		perror("fork");
    		return 1;
    	}
    	return 0;
    }
    

    8、创建5个僵尸进程,并在终端通过 ps axf 命令查看僵尸进程信息。

    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    int main() {
    	printf("before fork
    ");
    	pid_t pid, n = 5;
    	while(n--) {
    		pid = fork();
    		if (pid == 0)
    			break;
    		else if (pid < 0){
    			perror("fork");
    			return 1;
    		}
    	}
    	if (pid == 0) {
    	printf("hello, I'm child %d; my father is %d
    ", getpid(),getppid());
    	//getpid()  获取当前进程的pid
    	//getppid() 获取当前进程的父进程的pid
    	return 0;
    	}
    	while(1) {
    		sleep(3);
    		printf("hello, I'm father %d
    ", getpid());
    	}
    	return 0;
    }
    

    ps axf		显示进程见关联的树状结构图
    另开一终端输入	ps axf	查看僵尸进程,显示如下:
    

    9、 通过 wait 来清理僵尸进程。

    wait(等待子进程中断或结束)
    (1)表头文件:
    #include<sys/types.h>
    #include<sys/wait.h>
    (2)定义函数: pid_t wait (int * status);
    (3)函数说明:
    wait()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用wait()时子进程已经结束,则wait()会立即返回子进程结束状态值。子进程的结束状态值会由参数status 返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则参数status可以设成NULL。
    (4)返回值:
    如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1,失败原因存于errno 中。
    
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    int main() {
    	printf("before fork
    ");
    	pid_t pid, n = 5;
    	while(n--) {
    		pid = fork();
    		if (pid == 0)
    			break;
    		else if (pid < 0) {
    			perror("fork");
    			return 1;
    		}
    	}
    	if (pid == 0) {
    		printf("hello, I'm child %d;my father is %d
    ",getpid(),getppid());
    		return 0;
    	}
    	while(1) {
    		sleep(3);
    		pid = wait(NULL);
    		if (pid == -1) {
    			perror("wait");
    			sleep(10);
    			printf("I'm father %d;I have wiped out all zombies
    ",getpid());
    			return 1;
    		}
    		printf("Hello, I'm father %d; child %d exit
    ",getpid(),pid);
    	}
    	return 0;
    }
    

    10、 父进程通过 waitpid 函数等待特定子进程结束,若该子进程不结束,父进程一直阻塞。

    waitpid
    (1)函数功能:用来等待某个特定进程的结束
    (2)函数原型:
        pid_t waitpid(pid_t pid, int *status, int options);
    (3)参数:
        status如果不为空,会把状态信息写到它指向的位置
        options允许改变waitpid的行为,最有用的一个选项是WNOHANG,它的作用是防止waitpid把调用者的执行挂起.
    (4)返回值:成功返回等待子进程的pid,失败返回-1
    
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    void handler(int sig)
    {
    	pid_t pid;
    	while ((pid = waitpid(-1,NULL,WNOHANG)) > 0)
    	{
    		printf("wait child sucess : %d
    ",pid);
    		
    	}
    }
    int main()
    {
    	signal(SIGCHLD,handler);
    	pid_t pid = fork();
    	if (pid == 0)
    	{
    		printf("child1 pid : %d
    ",getpid());
    		sleep(3);
    		exit(1);
    	}
    	pid_t pid2 = fork();
    	if (pid2 == 0)
    	{
    		printf("child2 pid2 : %d
    ",getpid());
    		sleep(5);
    		exit(2);
    	}
    	pid_t pid3 = fork();
    	if (pid3 == 0)
    	{
    		printf("child3 pid3 : %d
    ",getpid());
    		sleep(7);
    		exit(3);
    	}
    	printf("father pid : %d
    ",getpid());
    	while (1)
    	{
    		printf("father do self
    ");
    		sleep(1);
    	}
    	return 0;
    }
    

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    腾讯社招 —— 腾讯游戏后端工程师-电话面试
    乐刷科技-Java工程师社招面试
    平安人寿保险-深圳Java开发工程师社招面试
    字节跳动-后端工程师社招视频一面
    Markdown的常用使用语法
    oracle 表空间大小
    mysql、sqlserver、oracle获取最后一条数据
    mycat 学习
    oracle sql中特殊字符 & 的处理
    map循环
  • 原文地址:https://www.cnblogs.com/crystalqj/p/12823436.html
Copyright © 2011-2022 走看看