实验六 进程基础
项目 |
内容 |
|
这个作业属于哪个课程 |
|
|
这个作业的要求在哪里 |
|
|
学号-姓名 |
17041417-李嘉麒 |
|
作业学习目标 |
1、掌握Linux系统环境C语言编程概念 2、学习Linux系统进程概念 |
实验内容
一、请举例说明静态链接库的创建与使用。
使用静态库是重用代码的一种绝佳方式。 你不必在要求功能的每个应用中重新实现同一例程,而只需将其写入静态数据库一次,然后从应用引用它们即可。
举例说明:
以计算加法、减法为例。先编写加法(add)、减法(sub)函数,并分别编译成一个静态库。
//add.c文件
int add(int a,int b) { return a+b; }
//sub.c文件
int sub(int a,int b) { return a-b; }
下图操作中,"gcc -c -static add.c -o add.o",是将add.c编译成一个静态库,sub.c同样。而"ar -r libbase.a add.o/sub.o",是将add.o、sub.o进行打包,可以理解为libbase.a只是.o的一个集合,方便使用。可以看到查看libbase.a包中内容为两个.o文件.

编写主函数(main),并在其中调用加法、减法函数。
//main.c文件
#include <stdio.h> int add(int a,int b); int sub(int a,int b); int main() { printf("3 + 1=%d\n",add(3,1)); printf("3 - 1=%d\n",sub(3,1)); return 0; }
下图中,"gcc main.o libbase.a -o app",链接main.o与libbas.a(即为add.o、sub.o)生成最终文件。其也可写为“gcc main.o -o app -L. -lbase”

二、请举例说明共享库的创建与使用。
也以计算加减法为例。创建目录结构如下,并补充相应代码文件

//common.h文件
#ifndef _common_
#define _common_
int add(int a,int b);
int sub(int a.int b);
#endif
//add文件
int add(int a,int b){
return a+b;
}
//sub文件
int sub(int a,int b){
return a-b;
}
//main文件
#include<stdio.h>
#include"common.h"
int main(){
printf("3+1=%d\n",add(3,1));
printf("3-1 =%d \n",sub(3,1));
return 0;
}
再创建共享库。

使用共享库,方式一:
gcc -c main.c -I../include #使用共享库 gcc main.o ../lib/libbase.so -o ../bin/app #需注意头文件不在当前文件夹也不在标准搜索路径时,需自己指定 ldd bin/app
#使用ldd查看共享库


方式二:
gcc main.o -L../lib -lbase -o ../bin/app

其中的libbase.so并没有找到,要添加一个环境变量
export LD_LIBRARY_PATH=../lib


三、编程实现一个简单文件复制命令。
//dircp.c文件
#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:\n mycp src dst\n");
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;
}
./dircp dircp.c dirtest
#将dircp.c文件复制b并命名为dirtest文件
diff dircp.c dirtest
#以逐行的方式比较文本的异同处

四、使用fork创建一个子进程,进程创建成功后父子进程分别输出不同的内容。
///fork1.c文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
pid_t pid;
printf("[%d]:Begin! \n",getpid());
fflush(NULL);
pid = fork();
if(pid<0)
{
perror("fork()");
exit(1);
}
else if(pid > 0)
{
printf("[%d]:Parent process if working!\n",getpid());
}
else
{
printf("[%d]:Child process if working!\n",getpid());
}
printf("[%d]:Finish!\n",getpid());
return 0;
}

全缓存:
填满标准I/O缓存区才进行实际的I/O操作。磁盘上的了件用标准I/O打开,默认都是全缓存的。当缓存区填满或者进行flush操作时候才会进行磁盘操作。
行缓存:
当输入输出遇到换行符时候就是行缓存了。标准输入和标准输出都是行缓存。
无缓存:
不对I/O操作进行缓存,对流的读写可以立即操作实际文件。典型例子就是标准出错。
(1)、删除fork1.c文件中 fflush(NULL); 这一行后运行结果为:

和之前相比,在非定向至文件输出是相同的,因为都是使用了行缓冲,检测到\n,执行I/O操作,并清理行缓存内容。将”fflush(NULL);”删除后,在重定向文件输出结果不同,是因为使用全缓冲,若无fflush()清除缓存区内容,就会出现缓存区内容的保留,如“Begin!”并未被清除,从而被二次写入。
(2)、继续删除fork1.c文件中 “ printf("[%d]:Begin! \n",getpid()); ” 这一句中的“\n”结果为

可以看到在将\n删除后,行缓存因为没有\n作为结束符,缓存区未清理,也出现了“Begin!”二次写入。
五、使用fork创建多个子进程。
int i;
pid_t pid;
for (i = 0; i < 3; i++)
pid = fork();
上面代码段会产生多少子进程?
会产生: 2^3-1=7个子进程。但实际上只有3个是父进程产生的,其余都为子进程 fork()出来的。父进程fork了3个子进程,第1个子进程执行完之后又fork了2个进程,第2个子进程fork了1个进程。
//fork2.c文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
int i;
pid_t pid;
printf("[%d] Begin! \n",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!\n",getpid());
}
else
{
printf("[%d] Child process %d is working!\n",getpid(),i);
}
return 0;
}

用sleep函数来控制进程输出顺序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
int i;
pid_t pid;
printf("[%d] Begin! \n",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!\n",getpid());
}
else
{
sleep(i);
printf("[%d] Child process %d is working!\n",getpid(),i);
}
return 0;
}

六、在 fork 之前以写的方式创建了一个文件 test.txt。然后 fork 出的子进程立即向文件中写入“world”,然后睡眠5秒。而父进程在 fork 后睡眠3秒后向 test.txt 写入 "hello",并关闭描述符。子进程恢复后,又向 test.txt 文件中写入 "lalala"后关闭描述符,结束。
//fork3.c文件
#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\n");
printf("before fork\n");
pid_t pid = fork();
if (pid > 0){
sleep(3);
printf("I'm father; I'm writing test.txt...\n");
write(fd, "hello", 5);
close(fd);
}
else if (pid ==0){
printf("I'm child; I'm writing test.txt...\n");
write(fd, "world", 5);
sleep(5);
write(fd, "lalala", 6);
close(fd);
}
else {
perror("fork");
return 1;
}
return 0;
}

七、分别在主函数中使用execvp启动ls命令以及使用fork函数产生子进程调用execvp启动ls。
(1)、使用execvp启动ls命令
//exetest.c文件
#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命令:
//exefork.c文件
#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\n");
}
else if (pid == 0) {
printf("I'm child\n");
if (execvp("ls",argv) == -1){
perror ("exec");
return 1;
}
}
else {
perror("fork");
return 1;
}
return 0;
}

八、创建5个僵尸进程,并在终端通过ps axf命令查看僵尸进程信息。
//fork4.c文件
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main() {
printf("before fork\n");
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\n", getpid(),getppid());
//getpid() 获取当前进程的pid
//getppid() 获取当前进程的父进程的pid
return 0;
}
while(1) {
sleep(3);
printf("hello, I'm father %d\n", getpid());
}
return 0;
}

在终端中新建一个窗口,输入:
ps axf //显示进程见关联的树状结构图

九、通过wait来清理僵尸进程。
//fork5.c文件
#include <unistd.h> #include <stdio.h> #include <string.h> #include <sys/wait.h> #include <sys/types.h> int main() { printf("before fork\n"); 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\n",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\n",getpid()); return 1; } printf("Hello, I'm father %d; child %d exit\n",getpid(),pid); } return 0; }

十、父进程通过waitpid函数等待特定子进程结束,若该子进程不结束,父进程一直阻塞。
//fork7.c文件
#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\n",pid);
}
}
int main()
{
signal(SIGCHLD,handler);
pid_t pid = fork();
if (pid == 0)
{
printf("child1 pid : %d\n",getpid());
sleep(3);
exit(1);
}
pid_t pid2 = fork();
if (pid2 == 0)
{
printf("child2 pid2 : %d\n",getpid());
sleep(5);
exit(2);
}
pid_t pid3 = fork();
if (pid3 == 0)
{
printf("child3 pid3 : %d\n",getpid());
sleep(7);
exit(3);
}
printf("father pid : %d\n",getpid());
while (1)
{
printf("father do self\n");
sleep(1);
}
return 0;
}

可以看到当该子进程不结束,父进程一直阻塞,进程不结束


浙公网安备 33010602011771号