杂记 (2) —— linux and C

本文记录了Linux操作中的一些常见问题,包括SSH远程登录、Vi编辑器的使用,以及C语言编程的相关知识点。介绍了如何解决SSH无法连接、查看网络配置、Linux服务配置、进程管理等问题,并探讨了C程序的编译过程。此外,还分享了在Linux环境下进行C编程的技巧,如删除文件、管理进程和理解预编译、编译、汇编、链接步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

linux操作, ssh, vi

  • 自动获取IP的网卡设置怎嘛查看IPv4?
    将/etc/sysconfig/network-scripts/ifcfg-eth0中的BOOTPROTO的值更改成dhcp. 然后ifdown 网卡名, ifup 网卡名.

  • linux某一项service没有找到?
    将可执行文件的路径加入PATH中。相关文件(ubuntu): /etc/environment

  • 能Ping通linux,但是不能ssh远程登录。
    查看ssh服务是否开启,ps -e |grep ssh
    没有ssh存在就说明没有ssh服务。

$ ps -e |grep ssh
 4682 ?        00:00:00 sshd

则是存在的.
解决ssh服务问题: 在/etc/init.d中没有ssh则没有安装ssh。如果存在直接重启。否则安装,建议命令行+网络方式。
如果存在,那么查看22端口是否开放
ssh localhost
出现连接拒绝的信息就说明端口有问题。
解决端口问题: 编辑/etc/ssh/ssh_config
将port 22和protocol 2,1注释掉。重启即可。

  • Ubuntu的root密码是多少?
    Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码。可以使用sudo passwd root来解决这个问题。

  • 假设安装软件后有图标,但是不知道可执行文件的路径怎么办?
    可以将软件运行起来,然后ps -e |grep processname
    得到进程号,接着输入ll /proc/pid。查看,存在类似exe -> /usr/bin/的信息,它即是绝对路径。

  • ssh远程登录运行Qt,出现错误:QXcbConnection: Could not connect to display
    首先,确定远端允许X11转发,将/etc/ssh/sshd_config中X11Forwarding 置成yes
    然后,在本地安装xshell + xmanager。
    设置xshell: 文件——属性——SSH——隧道——转发x11到——xmanager——确定
    xshell远程登录后即可正常使用软件。

  • 在vi(vim)中怎样删除某一列,或者某几列,某几行?
    使用visual模式,找到一个角点,按下ctrl+v,进入visual block模式,选中对角线的另一个角点,选中的部分按下x(或者 d)就能删除。

  • root也杀不死进程?

[root@localhost test]# ps aux |grep udp1
weiyang  16351  0.0  0.0   4164   348 pts/4    T    15:45   0:00 ./udp1
root     18017  0.0  0.0 112640   960 pts/4    S+   16:03   0:00 grep --color=auto udp1
[root@localhost test]# kill 16351
[root@localhost test]# ps aux |grep udp1
weiyang  16351  0.0  0.0   4164   348 pts/4    T    15:45   0:00 ./udp1
root     18020  0.0  0.0 112640   960 pts/4    S+   16:03   0:00 grep --color=auto udp1

可以查看进程树,找一找进程间的关系:

pstree > read
vi read
/udp1

然后找到父进程杀死:

[root@localhost test]# ps aux |grep ssh
root      1840  0.0  0.1 137904  5324 ?        Ss   11:41   0:00 sshd: weiyang [priv]
weiyang   1842  0.0  0.0 137904  2344 ?        S    11:41   0:00 sshd: weiyang@pts/3
root      2014  0.0  0.1 137904  5320 ?        Ss   11:44   0:00 sshd: weiyang [priv]
weiyang   2016  0.0  0.0 137904  2344 ?        S    11:44   0:01 sshd: weiyang@pts/4
huzheng+  2790  0.0  0.0  52816   572 ?        Ss   Sep02   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
root      3517  0.0  0.0  82488  3592 ?        Ss   Sep02   0:00 /usr/sbin/sshd -D
root     18229  0.0  0.0 112644   956 pts/4    S+   16:15   0:00 grep --color=auto ssh
root     47223  0.0  0.1 137904  5320 ?        Ss   Sep02   0:00 sshd: huzhengquan [priv]
huzheng+ 47225  0.0  0.0 137904  2328 ?        S    Sep02   0:00 sshd: huzhengquan@pts/1
[root@localhost test]# ps aux |grep ssh |grep pts/4
weiyang   2016  0.0  0.0 137904  2344 ?        S    11:44   0:01 sshd: weiyang@pts/4
root     18345  0.0  0.0 112644   960 pts/4    S+   16:19   0:00 grep --color=auto ssh
[root@localhost test]# kill 2016

重新远程登录后,程序udp1消失了

  • 上面出现过的pts/4是什么东西?
    pts即虚拟终端. 最开始打开一个终端,他叫pts/0。
    与之相关的还有tty, 有一个tty7是表示图形界面,tty1-tty6表示文字界面

  • cp: omitting directory 省略目录错误?
    自己copy的是目录,应该用递归的方式来拷贝。

  • error: …file is busy. source insight: it is not currently avalible for write access
    多半是自己仅仅给文件夹赋予了响应的权限,没有给其子目录赋予权限。

记录

linux网络, C 指针

  • 在dos cmd中进行exe安装
    假设文件在H:/install 中
cd /d H:\install
filename.exe
  • linux查看磁盘的信息
    fdisk -l

  • 设置linux ubuntu和Windows联网:
    首先,插上网线。
    假设Windows的网络设置为:
    IP: 10.21.1.109
    掩码: 255.255.0.0
    网关: 10.21.0.254
    那么修改文件
    /etc/network/interfaces
    增添内容:

auto 网卡名
iface 网卡名 inet static
address 10.21.1.110
netmask 255.255.0.0
gateway 10.21.0.254

再输入命令: sudo /etc/init.d/networking restart

  • ubuntu桌面上的软件有一部分是在/opt中的。

  • 软连接的建立: ln -s 软件所在路径 创建快捷方式的路径

  • 查看所有的端口: netstat -an (windows or linux)

  • Linux C read()和scanf()的区别:
    ssize_t read(int fd, void *buf, size_t count);
    对于read(STDIN_FILENO,buff,size_len),如果我们在屏幕上输入hello, 再敲入enter,那么’\n’也是存进buff了的。
    也即对于:

int main(){
    char s[65535];
    ssize_t fid=read(STDIN_FILENO,s,65535);
    printf("%s%d\n",s,strlen(s));
    scanf("%s",s);
    printf("%s%d\n",s,strlen(s));
    return 0;
}

有这样的结果:

hello 
hello
6
hello 
hello5

recvfrom和read一样。

  • 编译错误:
error: variable-sized object may not be initialized
const int len=1<<7;
char str[len]={0};

将const int len=1<<7; 改成enum{ len=1<<7 };

  • 一次测试发现的奇怪现象:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int main(){
    int *num=NULL;
    int number[]={1,2,3,4,5};
    num=number;
    memset(num,0,sizeof(num));
    printf("%d\n",sizeof(num));
    int i;
    for(i=0;i<5;i++){
        printf("%d ",number[i]);
    }    
    puts("");
    return 0;
}

他的输出是这样的:

8
0 0 3 4 5

这是因为,sizeof(指针)得到的不是数组的长度而是指针的长度!
改之:

#include <unistd.h>
#include <string.h>
#include <stdio.h>
int main(){
        int *num=NULL;
    int number[]={1,2,3,4,5};
    num=number;
        memset(num,0,sizeof(int)*5);
    printf("%d\n",sizeof(num));
    int i;
    for(i=0;i<5;i++){
        printf("%d ",number[i]);
    }    
    puts("");
    return 0;
}

C程序编译过程探究

编译过程分为如下部分:
预处理:在预处理阶段,主要完成对源代码中的预编译语句(如宏定义#define等)和文件包含进行处理。需要完成的工作是对预编译指令进行替换,把包含文件放置到需要编译的文件中。完成这些工作后,会生成一个非常完整的C程序源文件。
编译:gcc对预处理以后的文件进行编译,生成以.s为后缀的汇编语言文件。该汇编语言文件是源代码编译得到的汇编语言代码,接下来交给汇编过程进行处理。汇编语言是一种比C语言更低级的语言,可以直接对硬件进行操作。程序需要编译成汇编指令以后再编译成机器代码。
汇编:汇编过程是处理汇编语言的阶段,主要调用汇编处理程序完成将汇编语言会变成二进制机器代码的过程。通常来说,汇编过程是将.s的汇编语言代码文件汇编为.o的目标文件的过程。所生成的目标文件作为下一步链接过程的输入文件。
链接:链接过程就是将多个汇编生成的目标文件及引用的库文件进行模块链接生成一个完整的可执行文件。在链接阶段,所有的目标文件被安排在可执行程序中的适当的位置。同时,该程序所调用到的库函数也从各自所在的函数库中链接到程序中。经过了这个过程以后,生成的文件就是可执行的程序。

写一个折半查找的程序:

#include <stdio.h>
#include <stdlib.h>
int midfind(int a[],int goal,int len){
    int l=0,r=len-1,m=0;
    int ans=-1;
    while(l<=r){
        m=(r+l)>>1;
        if(a[m]==goal) {
            ans=m;
            break;
        }
        else if(a[m]>goal) r=m-1;
        else l=m+1;
    }
    return ans;
}
int main(){
    int a[10]={2,4,7,9,12,15,18,19,20,35};
    int g=0;
    while(~scanf("%d",&g)){
        int ans=midfind(a,g,10);
        if(ans==-1) puts("not found");
        else printf("the location is %d\n",ans);
    }
    return 0;
}
  • 用gcc进行预处理过程。产生文件out1
    gcc -E -o out1 binary_search.c
    head -10 out1
$ head -10 out1 
# 1 "binary_search.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "binary_search.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 27 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 375 "/usr/include/features.h" 3 4
  • 生成汇编代码
    gcc -S -o out2 binary_search.c
    vi out2:
    .file   "binary_search.c"
    .text
    .globl  midfind
    .type   midfind, @function
midfind:
.LFB2:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movq    %rdi, -24(%rbp)
    movl    %esi, -28(%rbp)
    movl    %edx, -32(%rbp)
    movl    $0, -4(%rbp)
    movl    -32(%rbp), %eax
    subl    $1, %eax
    movl    %eax, -8(%rbp)
    movl    $0, -16(%rbp)
    movl    $-1, -12(%rbp)
    jmp .L2
.L6:
    movl    -4(%rbp), %eax
    movl    -8(%rbp), %edx
    addl    %edx, %eax
    sarl    %eax
    movl    %eax, -16(%rbp)
    movl    -16(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    cmpl    -28(%rbp), %eax
    jne .L3
    movl    -16(%rbp), %eax
    movl    %eax, -12(%rbp)
    jmp .L4
.L3:
    movl    -16(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    cmpl    -28(%rbp), %eax
    jle .L5
    movl    -16(%rbp), %eax
    subl    $1, %eax
    movl    %eax, -8(%rbp)
    jmp .L2
.L5:
    movl    -16(%rbp), %eax
    addl    $1, %eax
    movl    %eax, -4(%rbp)
.L2:
    movl    -4(%rbp), %eax
    cmpl    -8(%rbp), %eax
    jle .L6
.L4:
    movl    -12(%rbp), %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE2:
    .size   midfind, .-midfind
    .section    .rodata
.LC0:
    .string "not found"
.LC1:
    .string "the location is %d\n"
.LC2:
    .string "%d"
    .text
    .globl  main
    .type   main, @function
main:
.LFB3:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $64, %rsp
    movl    $2, -48(%rbp)
    movl    $4, -44(%rbp)
    movl    $7, -40(%rbp)
    movl    $9, -36(%rbp)
    movl    $12, -32(%rbp)
    movl    $15, -28(%rbp)
    movl    $18, -24(%rbp)
    movl    $19, -20(%rbp)
    movl    $20, -16(%rbp)
    movl    $35, -12(%rbp)
    movl    $0, -52(%rbp)
    jmp .L9
.L11:
    movl    -52(%rbp), %ecx
    leaq    -48(%rbp), %rax
    movl    $10, %edx
    movl    %ecx, %esi
    movq    %rax, %rdi
    call    midfind
    movl    %eax, -4(%rbp)
    cmpl    $-1, -4(%rbp)
    jne .L10
    movl    $.LC0, %edi
    call    puts
    jmp .L9
.L10:
    movl    -4(%rbp), %eax
    movl    %eax, %esi
    movl    $.LC1, %edi
    movl    $0, %eax
    call    printf
.L9:
    leaq    -52(%rbp), %rax
    movq    %rax, %rsi
    movl    $.LC2, %edi
    movl    $0, %eax
    call    __isoc99_scanf
    cmpl    $-1, %eax
    jne .L11
    movl    $0, %eax
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE3:
    .size   main, .-main
    .ident  "GCC: (GNU) 4.8.3 20140911 (Red Hat 4.8.3-9)"
    .section    .note.GNU-stack,"",@progbits
  • 生成目标代码
$ gcc -c -o out3 binary_search.c 
$ ls
binary_search.c  out1  out2  out3
$ file out3
out3: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

生成可重定位的目标代码文件out3
该文件还不能直接执行,可以尝试,不过他会输出这样的信息:
bash: ./out3: Permission denied
就算是用root也会得到这样的结果。

  • 链接生成可执行文件
gcc -o out4 out3
# ls
binary_search.c  out1  out2  out3  out4
# file out4
out4: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x36e8d6e06962c6cc0956d0a4838f46dfa94ce74b, not stripped
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值