使用linux时的问题总结

[TOC]

vim /etc 写入时 出现 E121:无法打开并写入文件

解决方案:

  1. 保存的时候用:w !sudo tee %
  2. 输入密码,即可

安装中文输入法

安装步骤

1.更新源文件,使用的是阿里云源

打开sources.list文件

leafpad /etc/apt/sources.list

复制源地址到文本中

deb https://mirrors.aliyun.com/kali kali-rolling main non-free contrib
deb-src https://mirrors.aliyun.com/kali kali-rolling main non-free contrib

更新安装源

apt-get update

2.下载IBus完后重启系统

apt-get install ibus
apt-get install ibus-pinyin
reboot //重启

3.IBus配置 鼠标右键选择设置(setting)>区域和语言(Region&Language),在输入源中添加汉语(PinYin),可通过搜索PinYin找到。

Linux 编程

gcc 多线程编译失败

image-20220403144624296

今天写一个线程的程序,已经在c文件中包含了线程的头文件<pthread.h>,可是编译的时候却报错“对pthread_create未定义的引用“,原来时因为 pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:gcc createThread.c -lpthread -o createThread. 加上这个以后编译成功!

image-20220403144755741

image-20220403152451357

{//线程创建相关函数

pthread_t tid;   //创建线程 ID

pthread_create(&tid, NULL,  threadFunc, NULL);
/*
    线程创建函数,参数列表:
    1.  线程 ID 地址
    2.  线程 属性
    3.  线程 函数地址
    4.  线程 传递给线程函数的参数
*/

pthread_join(tid, NULL);   //等待指定的线程结束

}
#include<time.h>
{//统计程序运行时间

	clock_t start,delta;
	double time_used;	//程序开头加上这两行代码

	/*
		程序变量在这里定义
	*/

	start = clock();

	/*
		程序执行代码放在这里
	*/

	delta = clock() - start;
	printf("The time taken in total : %lf seconds\n",(double)delta/CLOCKS_PER_SEC);	 //程序末尾输出运行时间

	return 0;
}

//统计程序时间,除了在代码中统计外,还能再运行程序前加上 [time ./程序名] ,让系统去统计

互斥锁

image-20220404145450778

信号量

image-20220404195520172

image-20220405143220745

image-20220405143628664

参考: 《操作系统原理及Linux实践》