操作系统实验2:fork()系统调用
Posted 临风而眠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作系统实验2:fork()系统调用相关的知识,希望对你有一定的参考价值。
操作系统实验2:fork()系统调用
文章目录
Task1: fork的基本使用
代码
-
fork.c
代码#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main(void) pid_t pid; pid = fork(); if (pid < 0) fprintf(stderr, "Fork Failed"); exit(-1); else if (pid == 0) printf("I am a child\\n"); else wait(NULL); printf("I am a parent\\n"); exit(0);
-
test.sh
代码#!/bin/bash for((i=1;$i<=100;i++)); do echo $i ./fork done
作用是把fork.c编译链接后的可执行文件fork运行100次
运行结果
-
运行test.sh
先child 后 parent
-
去掉wait(NULL)之后再运行test.sh
不确定
解释
-
没有修改的
When you run the script
test.sh
, it will execute thefork
program 100 times in a loop. Each timefork
is called, it creates a new process by calling thefork
function. The child process will print “I am a child” and the parent process will wait for the child to finish before printing “I am a parent” and exiting.Therefore, you will see the output “I am a child” and “I am a parent” being printed 100 times, with the child process outputting first and the parent process outputting second each time. The parent process will wait for the child process to finish before outputting its message, so the messages will be printed in pairs.
It’s important to note that the child process is a copy of the parent process, so it will have its own separate memory space and run independently of the parent process. The
wait
function is used to synchronize the parent and child processes, allowing the parent to wait for the child to finish before continuing. -
Now I modify fork.c with the
wait(NULL)
line commented out , I run test.sh again. What will happenIf you comment out the
wait(NULL)
line infork.c
, the parent process will no longer wait for the child process to finish before continuing. This means that the parent process will output its message “I am a parent” immediately after the child process starts, rather than waiting for the child process to finish.As a result, when you run
test.sh
, you will see the output “I am a parent” and “I am a child” being printed 100 times, with the parent process outputting first and the child process outputting second each time. The messages will no longer be printed in pairs, because the parent process is no longer waiting for the child process to finish before continuing.It’s important to note that the child process will still run independently of the parent process, even if the parent process does not wait for it to finish. The
wait
function is used to synchronize the parent and child processes, but if it is not called, the parent process will continue without waiting for the child to finish.
If you run the modified program 100 times with the
wait(NULL)
line commented out, you may see different results each time you run the program. This is because thefork()
function creates a new process by duplicating the calling process, and the behavior of the child process is not deterministic.Here are some possible outcomes when running the program 100 times without the
wait(NULL)
line:
-
In some runs, you may see the child process print “I am a child” and the parent process print “I am a parent”, followed by the program exiting. This is the expected behavior when the
wait()
function is not called. In this case, the child process runs to completion and then the parent process exits. -
In other runs, you may see the child process print “I am a child” and then the parent process print “I am a parent”, but the program does not exit. This is because the parent process is not waiting for the child process to terminate before exiting, so the child process becomes a “zombie” process that is left running in the background.
-
In some runs, you may see the child process print “I am a child” and then the parent process print “I am a parent”, followed by the program hanging and not exiting. This is because the child process is not terminating, and the parent process is waiting indefinitely for the child process to finish.
Overall, the behavior of the program when the
wait()
function is not called is not deterministic and can vary depending on the specific implementation of thefork()
function and the scheduling of processes by the operating system.In general, it is good practice to use the
wait()
function to wait for child processes to terminate in order to avoid leaving zombie processes or hanging programs.
Task2: 深入理解fork创建的子进程与父进程的关系
代码
-
task2.c
#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> int main(void) int i; for (i = 0; i < 2; i++) fork(); printf("-"); wait(NULL); wait(NULL); return 0;
实验结果
突然感觉在CSDN上传博客也面临各种进程冲突哈哈哈,不然也不会那么多图床转换失败
一开始给的代码没有#include <sys/wait.h>,会报错,详见后文the troubles encountered in task2
现象解释
When you run this code, it will create a process that creates two child processes, and each of those child processes will create another two child processes. This will create a total of 2^3 = 8 processes.
Each process will print a hyphen character (“-”) to the console. The wait()
function will cause the parent process to wait for all of its child processes to terminate before continuing.
After all of the child processes have terminated, the parent process will return 0 to indicate that the program has completed successfully.
So, the output of the program will be a string of 8 hyphen characters, followed by a newline character.
遇到的问题
1.乌龙事件 vscode
- 缘起,在一个终端里面使用
code
命令出来这个
-
接着试遍了下面这些博客里的解决方案,依然没有用
-
然后我直接去点击vscode图标打开vscode,再关掉,这个时候再去另一个终端里面,用
code
和code .
都正常了…或者说就是我那个终端里面有什么权限冲突? 当时我换个终端就行???
code
和code .
的区别在于code .
是在当前所在路径下打开vscode
-
What’s the diff between code and code . when I wanna open vscode in terminal ?
The
code
command is used to open Visual Studio Code from the command line. When you typecode
followed by a space and then a file or directory name, it will open Visual Studio Code and open the specified file or directory in it.For example, if you have a file called
main.js
in the current directory and you want to open it in Visual Studio Code, you can typecode main.js
in the terminal. This will open Visual Studio Code and load themain.js
file in it.If you just type
code
followed by a space and then press Enter, it will open Visual Studio Code, but it will not open any specific file or directory. Instead, it will just open the default workspace for Visual Studio Code.So, the difference between
code
andcode .
is thatcode
will open Visual Studio Code without opening any specific file or directory, whilecode .
will open Visual Studio Code and open the current directory in it.
2.the troubles encountered in task1
如何跑.sh文件
-
一开始直接在终端里面输入
./test.sh
然后遇到这个:
bash: ./test.sh: Permission denied
-
然后我本能反应,加了个sudo :
sudo ./test.sh
还是不行 :
sudo: ./test.sh: command not found
-
于是去查了一下
To run a .sh file in the terminal, you will need to make the file executable first. You can do this by using the
chmod
command followed by the executable permission and the file name. For example:chmod +x filename.sh
This will give the current user executable permissions for the file.
Once the file is made executable, you can run it by typing
./filename.sh
in the terminal. -
果真chmod之后可以
编译链接遇到的问题
-
chmod加完可执行权限之后,运行
./test.sh
,有如下报错 -
反应过来,目录下当时只有fork.c,没有fork
-
于是进行编译链接
gcc -o fork fork.c
遇到如下问题
先解决那个newline的warning
-
The “warning: no newline at end of file” message is typically generated when you are trying to compile a source code file that does not end with a newline character. This can cause problems when the file is edited or processed by certain tools, as it can cause the last line of the file to be treated differently than the rest of the file.
To fix this warning, you can simply add a newline character at the end of the file. You can do this by opening the file in a text editor and pressing the “Enter” key or “Return” key at the end of the last line of the file.
It’s also a good idea to make sure that all of your source code files end with a newline character, as this can help prevent similar issues in the future.
-
于是我去fork.c新加了一行,那个warning没有了
-
但是下面的还是在
解决crt1.o,crti.o
心态是如何崩溃的
-
StackOverflow的这篇回答里面办法都快试遍了
-
sudo apt install gcc-multilib
之前装过了, update 再重装也无济于事,还是cannot find balabla -
create a symbolic link:
sudo ln -s /usr/lib/x86_64-linux-gnu /usr/lib64
这个试了,还是无济于事。
而且还不知道这个会不会对以后调别的东西产生影响
-
find指令 找出缺失的东西的位置, 然后再添加到LIBRARY_PATH里面
-
find之前记得sudo,不然一堆permission denied
![image-20221227075716405](https://img-blog.csdnimg.cn/img_convert/ca66490c1bde279586de748b2a262be1.png)
-
继续find
sudo find / -name crti.o sudo find / -name crt1.o
找出来发现在
/usr/lib32
,/usr/libx32
、/usr/lib/x86_64-linux-gnu
三个路径下都有
-
然后那个-lgcc_s,网上没搜到啥解决方案,我问了问chatgpt
-
/usr/bin/ld: cannot find -lgcc_s
-
If you are still getting the “cannot find -lgcc_s” error after specifying the
-L/usr/lib
flag, it is possible that thegcc_s
library is located in a different directory on your system.To find the location of the
gcc_s
library, you can use thefind
command to search for it. For example:find / -name libgcc_s.so
This will search for the
libgcc_s.so
library file on your system starting from the root directory (/
). Thefind
command will print the full path to the file if it finds it.Once you have found the location of the
libgcc_s.so
file, you can specify that directory using the-L
flag with thegcc
compiler. For example:gcc -L/path/to/gcc/library -o fork fork.c
Replace
/path/to/gcc/library
with the actual path to thelibgcc_s.so
file on your system.You may also need to specify the
-lc
flag to tellgcc
to link the C runtime library. For example:gcc -L/path/to/gcc/library -lc -o fork fork.c
-
-
又尝试StackOverflow里面的
ld -lgcc_s --verbose
-
我甚至想用之前的apt-file search
可是这太多了,也不知道安装哪个能fix呀
-
关于LIBRARY_PATH 和 LD_LBRARY_PATH
-
根据find 的结果,我加了几个路径到LIBRARY_PATH
此时症状疑似有所缓解
很奇怪,我在这个终端设置的LIBRARY_PATH,在另一个终端echo的时候,就输不出东西了!?…
这也是一个遗留问题
-
LD_LIBRARY_PATH是在这里看到的https://stackoverflow.com/a/32502403/18159372
- 看着感觉比LIBRARY_PATH更有道理,感觉燃起了希望,但是还是无济于事
-
以为自己要成功了!!!
-
StackOverflow上面这个让我燃起了希望
https://stackoverflow.com/a/12385495/18159372
-
试了试!
-
-
那么此时,我想,只需要加上-lgcc_s的路径就可以了吧!!!!!!!
-
赶紧去问问怎么加路径
How can I use -B to tell several paths to the terminal When I gcc
To specify multiple paths to include header files when using the
gcc
compiler, you can use the-I
option followed by the path to the directory containing the header files. For example:gcc -I/path/to/headers -I/another/path/to/headers main.c
This will tell the compiler to search for header files in the directories
/path/to/headers
and/another/path/to/headers
when compiling themain.c
file.You can also use the
-L
option to specify directories to search for libraries when linking, and the-l
option to specify the names of libraries to link with. For example:gcc -L/path/to/libraries -lmylibrary main.c
This will tell the compiler to search for the library
mylibrary
in the directory/path/to/libraries
when linking themain.c
file.The
-B
option, on the other hand, is used to specify the directory in which to look for thegcc
compiler and other programs needed during the compilation process. It is not used to specify paths to header files or libraries.I wanna use -B option to specify two directories
The
-B
option is used to specify the directory in which thegcc
compiler and other programs needed during the compilation process are located. You can specify multiple directories by separating them with a colon (:
). For example:gcc -B/path/to/compiler:/another/path/to/compiler main.c
This will tell the compiler to search for the
gcc
compiler and other necessary programs in the directories/path/to/compiler
and/another/path/to/compiler
.Note that the
-B
option is usually not necessary unless you have installed the compiler in a non-standard location or you are using a cross-compiler. In most cases, thegcc
compiler will be located in a standard location that is already in your system’s search path, so you do not need to use the-B
option.Show me an example, I don’t know what another should be repalced
Here is an example of using the
-B
option to specify multiple directories to search for thegcc
compiler and other necessary programs:gcc -B/usr/local/gcc-9.3.0/bin:/usr/local/gcc-9.3.0/libexec main.c
In this example, the
gcc
compiler and other necessary programs will be searched for in the directories/usr/local/gcc-9.3.0/bin
and/usr/local/gcc-9.3.0/libexec
. These directories might contain thegcc
compiler executable, as well as other programs needed during the compilation process, such as the preprocessor and linker.Note that this is just an example, and the directories specified in the
-B
option will depend on where you have installed thegcc
compiler on your system.
-
-
照着用冒号分隔,无济于事, 或者说需要找的是-lgcc而不是libgcc_s.so??? 我彻底麻了…
chatgpt给我的解决方案
这个gcc -L 目前我还没去官方文档查,也不知道是否正确,反正试下来是无效的
-
When I use gcc3.4 to complie and link a .c file , I meet the following problems : /usr/bin/ld: cannot find crt1.o: No such file or directory /usr/bin/ld: cannot find crti.o: No such file or directory
The “cannot find crt1.o” and “cannot find crti.o” errors are typically caused by a missing or incorrect library path when linking a C program using
gcc
.The
crt1.o
andcrti.o
files are object files that are part of the C runtime library and are required for linking C programs on many systems. These files are usually located in the/usr/lib
or/usr/lib64
directories.To fix these errors, you can try adding the appropriate library path to the
gcc
command using the-L
flag. For example:gcc -L/usr/lib -o program file1.o file2.o file3.o
This will tell
gcc
to search for libraries in the/usr/lib
directory, which should include thecrt1.o
andcrti.o
files.If the
crt1.o
andcrti.o
files are located in a different directory, you can specify that directory instead of/usr/lib
.You may also need to specify the
-lc
flag to tellgcc
to link the C runtime library. For example:gcc -L/usr/lib -lc -o program file1.o file2.o file3.o
-
试了
/usr/lib32
,/usr/libx32
这两个路径放在L后面,都没有用 -
chatgpt还告诉我这个
-
How to help the gcc find the LIBRARY_PATH when compile .c file
-
To tell the
gcc
compiler to search for libraries in the directories specified by theLIBRARY_PATH
environment variable, you can use the-Wl,-rpath
flag when compiling and linking your program.The
-Wl,-rpath
flag is a linker flag that tells the linker to add a specified directory to the list of directories that will be searched for shared libraries when running the program.For example, to tell
gcc
to search for libraries in the directories specified by theLIBRARY_PATH
variable, you can use the following command:gcc -Wl,-rpath=\\$ORIGIN -o program file.c
This will compile the
file.c
source code file and link it into an executable file calledprogram
. The-Wl,-rpath=\\$ORIGIN
flag tells the linker to add the directory where the program is located ($ORIGIN
) to the list of directories that will be searched for shared libraries when the program is run.The
gcc
compiler will also search the directories specified by the-L
flag and any default library directories when linking your program.I hope this helps! Let me know if you have any questions or if you need further assistance.
试了试,报错变成了 skipping incompatible??
这个终端里面我 修改了LIBRARY_PATH,所以没报找不到的错误??? ,换了一个终端,又直接not find了 啊啊啊哭
-
-
噢对,还有个忘记录了
分为两步,先gcc- c fork.c是不报错的, 到-o那一步才会报错
一个可能的原因
很多网上的帖子都是分为32bit和64bit处理的,所以我总感觉和这个有关系,就是安装的依赖是不是存在一些位数不匹配的冲突之类的
-
有个同学的gcc3.4是没问题的
他是看的Ubuntu18.04搭建linux0.11实验环境,里面的setup.sh文件安装的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vTwV203O-1672104829623)(null)]
不对,接着往下看…setup.sh
好像有个判断,会根据64位和32位自动选择
嘶…那问题在哪呢?
-
在安装gcc的时候少安装了i386的???
-
汇总一下在实验一里面我按照
-
我安装的时候是执行的如下(详细见实验一的记录)
wget http://old-releases.ubuntu.com/ubuntu/pool/main/g/gcc-3.4/cpp-3.4_3.4.6-6ubuntu2_amd64.deb wget http://old-releases.ubuntu.com/ubuntu/pool/main/g/gcc-3.4/gcc-3.4-base_3.4.6-6ubuntu2_amd64.deb wget http://old-releases.ubuntu.com/ubuntu/pool/main/g/gcc-3.4/gcc-3.4_3.4.6-6ubuntu2_amd64.deb sudo dpkg -i cpp-3.4_3.4.6-6ubuntu2_amd64.deb gcc-3.4-base_3.4.6-6ubuntu2_amd64.deb gcc-3.4_3.4.6-6ubuntu2_amd64.deb sudo apt install gcc-multilib sudo apt install bin86 sudo apt install lib32z1 libsm-dev:i386 libx11-6:i386 libxpm4:i386 libstdc++6:i386
-
-
除了换gcc版本之外,另一个可能的解决办法就是重新安装gcc😰
-
还有个可能的原因,但是感觉可能性不大,我的g++版本和gcc不匹配???
还有一些可能的解决方案
-
前面只问了google 、 StackOverflow 和 chatgpt,CSDN还没看
-
先留几个感觉有可能有用的链接,回头再来看看
摆啦☹,灵机一动, 把gcc版本调回7.5了, 没事啦😋
-
哈哈哈哈哈哈 开摆啦 ,主要是ddl不允许我深入钻研那么久了…
Vscode无法安装 C/C++ Complie run extension
-
- 里面还是安装一些依赖的解决方案
-
暂时懒得管更多的了…开摆
3.the troubles encountered in task2
The error message “implicit declaration of function ‘wait’; did you mean ‘main’?” indicates that the wait()
function is being used in the program, but it has not been properly declared.
To fix this error, you need to include the header file that declares the wait()
function in your program. The header file to include is sys/wait.h
.
To include this header file, add the following line at the top of your program:
#include <sys/wait.h>
This will declare the wait()
function and allow you to use it in your program.
You should also include the <sys/types.h>
header file, as the wait()
function requires the pid_t
type, which is defined in this header file.
After including the necessary header files, your program should compile and run without any issues.
加上头文件就不会warning了,不过加之前也是可以编译链接 运行的
Basic knowledge
chmod
-
chmod
is a command used in Unix and Unix-like operating systems to change the permissions of a file or directory. The permissions determine who is able to read, write, and execute a file.The
chmod
command is used to change the permissions of a file or directory by specifying the access rights for the owner, group, and others. The access rights can be specified using either symbolic notation or octal notation.Here is an example of using
chmod
with symbolic notation:$ chmod u=rwx,g=rx,o=r file.txt
This command sets the read, write, and execute permissions for the owner (
u
) torwx
(read, write, execute), the read and execute permissions for the group (g
) torx
(read and execute), and the read permission for others (o
) tor
(read only).Here is an example of using
chmod
with octal notation:$ chmod 754 file.txt
This command sets the read, write, and execute permissions for the owner to
rwx
(7 in octal notation), the read and execute permissions for the group torx
(5 in octal notation), and the read permission for others tor
(4 in octal notation). -
突然想起前面那个乌龙里面用了
chmod 777
-
What means chmod 777 ?
-
When using
chmod
, the777
permission set gives read, write, and execute permissions to all users (owner, group, and others).For example, the command
chmod 777 file.txt
would give read, write, and execute permissions to the owner, group, and others for the filefile.txt
.In symbolic notation, this would be equivalent to
chmod u=rwx,g=rwx,o=rwx file.txt
.It is generally not recommended to use the
777
permission set, as it gives full access to everyone and may pose a security risk. Instead, it is usually better to set more specific and limited permissions that are appropriate for the file or directory in question.
-
octal notation 和 symbolic notation
-
What’s the relation between symbolic notation and octal notation of chmod
-
In
chmod
, symbolic notation and octal notation are two different ways of specifying the access rights (permissions) for a file or directory.Symbolic notation uses letters and symbols to represent the permissions for the owner, group, and others. For example,
u=rwx
means that the owner has read, write, and execute permissions, whileg=rx
means that the group has read and execute permissions.Octal notation uses a combination of three digits to represent the same permissions. Each digit represents the permissions for the owner, group, and others, respectively. The digits are calculated by adding up the values for the permissions that are granted. For example,
7
means that the owner has read, write, and execute permissions (4 + 2 + 1),5
means that the group has read and execute permissions (4 + 1), and4
means that others have read permission only (4).Here is a table showing the correspondence between octal notation and symbolic notation:
Octal notation Symbolic notation 0 — 1 –x 2 -w- 3 -wx 4 r– 5 r-x 6 rw- 7 rwx
编译链接
-
how to complie and link .c files in ubuntu in one step or separate in two steps ?
In Ubuntu, you can compile and link a C program in one step or in two separate steps using the
gcc
compiler.-
一步直接
To compile and link a C program in one step, you can use the
gcc
command followed by the names of all the source files and any necessary libraries. For example, to compile and link a program calledprogram
with three source files,file1.c
,file2.c
, andfile3.c
, you can use the following command:gcc -o program file1.c file2.c file3.c -lm
This will compile and link the source code files into an executable file called
program
.解释一下这里的
-lm
In the context of C programming, the
-lm
flag is used to tell thegcc
compiler to link the math library when creating an executable file.The math library is a collection of functions that provide mathematical operations such as trigonometric functions(三角函数), exponential functions, and logarithmic(对数的) functions. These functions are declared in the
math.h
header file, and are usually implemented in a library file calledlibm.a
orlibm.so
.To use the math library in a C program, you will need to include the
math.h
header file and link the math library when compiling and linking your program. To link the math library, you can use the-lm
flag with thegcc
compiler.For example:
gcc -o program file.c -lm
This will compile the
file.c
source code file and link it with the math library to create an executable file calledprogram
. -
分为两步
Alternatively, you can compile the source code files into object files separately and then link them together in a separate step.
To do this, you can use the
gcc
command with the-c
flag to compile the source code files into object files, and then use thegcc
command again with the-o
flag to link the object files and create the executable file.For example:
gcc -c file1.c file2.c file3.c gcc -o program file1.o file2.o file3.o -lm
This will create object files for each of the source code files, and then link them together to create the
program
executable.
-
之前解决这三个心态要搞没了
crti.o 、 crt1.o
crti.o
and crt1.o
are object files that are part of the C runtime library. The C runtime library is a collection of functions and data structures that are used by C programs to perform various tasks, such as initializing variables, handling exceptions, and performing input/output operations.
crti.o
and crt1.o
are typically located in the /usr/lib
or /usr/lib64
directories on most systems. They are required for linking C programs on many systems.
libgcc_s.so
is a shared library file that is part of the GCC support library. The GCC support library is a collection of functions and data structures that are used by the GNU Compiler Collection (GCC) to perform various tasks, such as handling exceptions, implementing floating-point operations, and supporting language features.
libgcc_s.so
is typically located in the /usr/lib
or /usr/lib64
directories on most systems. It is required for linking programs compiled with GCC on many systems.
I hope this helps! Let me know if you have any questions or if you need further assistance.
libgcc_s.so
libgcc_s.so
is a shared library that contains object code for the GNU Compiler Collection (GCC) support functions. These functions include low-level operations such as exception handling, thread-local storage, and atomic operations.
libgcc_s.so
is typically used as a dependency of other shared libraries and executables that are compiled with GCC. It is often used to provide support for features that are not available in the standard system libraries, such as the C++ exception handling mechanism.
libgcc_s.so
is usually located in a system-specific library directory, such as /usr/lib
or /usr/local/lib
on Linux systems. It is typically installed as part of the GCC package, and is automatically linked to programs that are compiled with GCC.
If you receive an error message that refers to libgcc_s.so
, it may indicate that a shared library or executable that you are trying to run is missing a dependency or has a version mismatch. In this case, you may need to install or update the library or executable to resolve the issue.
ld
ld
is the GNU Linker. It is a command-line tool that is used to link object files and libraries to create executable programs or shared libraries.
When you compile a program written in a high-level language such as C or C++, the compiler produces object files that contain machine code for the individual functions and data definitions in your program. These object files do not contain all of the information needed to create a standalone executable, as they are missing references to functions and data defined in system libraries or other object files.
The linker combines the object files and libraries into a single executable or shared library, resolving the references between the different object files and linking them together into a single cohesive(结成一个整体的) unit. The linker also adds metadata to the executable, such as the program’s entry point and any required runtime libraries, and can perform other tasks such as optimizing the program’s memory layout.
You can use the ld
command to specify the object files and libraries that you want to link together, as well as any linker options that control the linking process. The linker produces an executable or shared library file as its output.
For example, to link a program written in C that consists of two object files main.o
and foo.o
, you might use a command like this:
ld main.o foo.o -o myprogram
This command would link the object files main.o
and foo.o
into an executable called myprogram
.
ld --verbose
The ld
command is the GNU Linker, a tool that is used to link object files and libraries into an executable program. The --verbose
flag is an option that tells ld
to print detailed information about the linking process.
When you use the ld
command with the --verbose
flag, it will print a list of the object files and libraries that are being linked, as well as the names of any undefined symbols (symbols that are referenced by the object files but not defined).
Here is an example of using the ld
command with the --verbose
flag:
ld --verbose -o program file1.o file2.o file3.o -lm
This will link the object files file1.o
, file2.o
, and file3.o
and the math library (-lm
) into an executable file called program
, and will print detailed information about the linking process.
The --verbose
flag can be useful for debugging linking issues or for understanding how ld
is linking your object files and libraries.
ldconfig -p
ldconfig
is a program that is used to configure the dynamic linker runtime bindings. It is typically used to create, update, and remove the necessary links and cache to the most recent shared libraries found in the directories specified in the /etc/ld.so.conf
file and the default directories, such as /lib
, /usr/lib
, etc.
The -p
option tells ldconfig
to display the current cache of shared libraries. When you run ldconfig -p
, it displays a list of shared libraries that are currently in the cache, along with their path, version, and dependencies.
Here is an example of the output of ldconfig -p
:
libc.so.6 (libc6,x86-64) => /lib/x86_64-linux-gnu/libc.so.6
libz.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libz.so.1
libm.so.6 (libc6,x86-64) => /lib/x86_64-linux-gnu/libm.so.6
libpthread.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libpthread.so.0
libgcc_s.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgcc_s.so.1
libc.so.6 (libc6) => /lib/i386-linux-gnu/libc.so.6
libz.so.1 (libc6) => /lib/i386-linux-gnu/libz.so.1
libm.so.6 (libc6) => /lib/i386-linux-gnu/libm.so.6
libpthread.so.0 (libc6) => /lib/i386-linux-gnu/libpthread.so.0
libgcc_s.so.1 (libc6) => /lib/i386-linux-gnu/libgcc_s.so.1
In this example, the output shows the shared libraries that are currently in the cache, along with their path, version, and dependencies. The libc6,x86-64
and libc6
entries indicate the version and dependencies of the shared library.
You can use ldconfig -p
to check which shared libraries are currently in the cache and troubleshoot issues with shared library dependencies.
以上是关于操作系统实验2:fork()系统调用的主要内容,如果未能解决你的问题,请参考以下文章