标签 Linux 下的文章

Linux环境下查看服务器硬件信息

查看CPU型号
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
16  Intel(R) Xeon(R) CPU           X5560  @ 2.80GHz
查看物理CPU个数
cat /proc/cpuinfo | grep physical | uniq -c
1 physical id     : 1
1 physical id     : 0
查看CPU运行模式:32位或者64位,如果是32位的话并不代表不能运行在64位下
getconf LONG_BIT
64
查看是否CPU支持64位系统,如果值大于0,则支持64位运算,lm指long mode
cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -l
16
查看内存信息
cat /proc/meminfo
MemTotal:     74175052 kB
MemFree:       4242856 kB
Buffers:        675664 kB
Cached:       65423884 kB
SwapCached:         16 kB
Active:       46231212 kB
Inactive:     20719936 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     74175052 kB
LowFree:       4242856 kB
SwapTotal:    75778596 kB
SwapFree:     75778384 kB
Dirty:              60 kB
Writeback:           0 kB
AnonPages:      851416 kB
Mapped:       12422988 kB
Slab:          1495940 kB
PageTables:    1372180 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:  112866120 kB
Committed_AS: 21125340 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    299916 kB
VmallocChunk: 34359438111 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB
查看当前系统内核信息
uname -a
Linux HOSTNAME 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
查看当前操作系统内核信息
cat /etc/issue | grep Linux
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
查看服务器型号
dmidecode | grep "Product Name"
Product Name: PowerEdge R710
Product Name: 0VWN1R
查看网卡信息
dmesg | grep -i eth
Broadcom NetXtreme II Gigabit Ethernet Driver bnx2 v1.9.3 (March 17, 2009)
eth0: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem d6000000, IRQ 106, node addr a4badb28c33d
eth1: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem d8000000, IRQ 114, node addr a4badb28c33f
eth2: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem da000000, IRQ 122, node addr a4badb28c341
eth3: Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express found at mem dc000000, IRQ 130, node addr a4badb28c343
cnic: Added CNIC device: eth0
cnic: Added CNIC device: eth1
cnic: Added CNIC device: eth2
cnic: Added CNIC device: eth3
bnx2: eth0: using MSIX
ADDRCONF(NETDEV_UP): eth0: link is not ready
bnx2i: iSCSI not supported, dev=eth0
bnx2i: iSCSI not supported, dev=eth0
bnx2: eth0 NIC Copper Link is Up, 1000 Mbps full duplex
ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
eth0: no IPv6 routers present
process `sysctl' is using deprecated sysctl (syscall) net.ipv6.neigh.eth0.base_reachable_time; Usenet.ipv6.neigh.eth0.base_reachable_time_ms instead.

linux简单tcp通讯

server

#include <sys/socket.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>

#define portnum 3456

int main(int argc,char *argv[])
{
int s_fd,c_fd;
char buffer[1024];
struct sockaddr_in server_addr;//服务器ip地址
struct sockaddr_in client_addr;//客户端ip地址
int nbytes;
int cli_size;
/*
struct sockaddr_in
{
short int sin_family; //ip地址族
unsigned short int sin_port; //端口号
struct in_addr sin_addr; //ip地址
unsigned char sin_zero[8]; //填0
}
*/
/*
struct in_addr
{
unsigned long s_addr;
}
*/
s_fd=socket(AF_INET,SOCK_STREAM,0);
//地址描述,套接口类型,套接口所用的协议
if(s_fd==-1)
{
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
printf("socket() return value:%d\n",s_fd);

//服务器填充sockaddr结构
bzero(&server_addr,sizeof(struct sockaddr_in));//初始化,置0
server_addr.sin_family=AF_INET; //Internet
//(将本机器上的long数据转化为网络上的long数据)和任何主机通信
//INADDR_ANY表示额可以接收任意IP地址的数据
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnum);
bzero(&(server_addr.sin_zero),8);
int b;
b=bind(s_fd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr));
//未捆绑套接口的描述字,赋予套接口的地址,地址的长度
if(b==-1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("bind() return value:%d\n",b);

//设置允许连接的最大客户端数
int c;
c=listen(s_fd,5);//最大监听数是5
if(c==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
printf("listen() return value:%d\n",c);

//TCP循环服务器
while(1)
{
cli_size=sizeof(struct sockaddr_in); //获取ip地址结构的大小
if((c_fd=accept(s_fd,(struct sockaddr *)&client_addr,&cli_size))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
//套接口的描述字,客户端的ip地址,地址长度
printf("accept() return value:%d\n",c_fd);

//将网络地址转换成可读的ip地址
fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));

//接收从客户端发送来的数据,返回读取的字节数
//if(!fork())
//{
if((nbytes=read(c_fd,buffer,1024))==-1)
{
fprintf(stderr,"Read Error:%s\n",strerror(errno));
exit(1);
}
buffer[nbytes]='\0'; //给数据结束标志
//输出从客户端读取的数据
printf("Server received %s\n",buffer);
//process();
close(c_fd);
exit(0);
//}
}
// close(c_fd);//关闭客户端的文件描述符
// close(s_fd);
close(s_fd);
exit(0);
}

client

#include "stdio.h"
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#define portnum 3456

int main(int argc,char *argv[])
{
struct sockaddr_in server_addr;
int c_fd;
char buffer[1024];
struct hostent *host;

if(argc != 2)
{
fprintf(stderr,"Gethostname error\n");
exit(1);
}

if((host=gethostbyname(argv[1])) == NULL)
{
fprintf(stderr,"Gethostname error\n");
exit(1);
}

c_fd=socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1)
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
}
printf("socket() return value:%d\n",c_fd);

//客户端sockaddr结构
bzero(&server_addr,sizeof(struct sockaddr_in));//初始化,置0
server_addr.sin_family=AF_INET; //Internet
server_addr.sin_port=htons(portnum);
//server_addr.sin_addr=*((struct in_addr *)host.h_addr);//指向主机的第一个ip地址
int a;
a=connect(c_fd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr_in));
if(a==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
printf("connect() return value:%d\n",a);
//从标准输入获取buffer大小的数据放入buffer中
fgets(buffer,sizeof(buffer),stdin);
write(c_fd,buffer,sizeof(buffer));//向服务器写数据
close(c_fd);
return 0;
}

gcc -g选项:生成调试符号信息

-g Produce debugging information in the operating system's native
format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this
debugging information.

-g选项产生本地系统调试信息格式(比如:stabs,COFF,XCOFF,或者DWARF 2)。gdb调试的时候需要这些调试符号信息。

On most systems that use stabs format, -g enables use of extra
debugging information that only GDB can use; this extra information
makes debugging work better in GDB but will probably make other
debuggers crash or refuse to read the program. If you want to
control for certain whether to generate the extra information, use
-gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

大多数系统采用stabs格式,-g产生的额外调试符号只有gdb能够使用;额外调试符号能让gdb调试工作更好,但是也有可能让其他调试器崩溃或者拒绝读取程序。如果需要控制额外控制符号的生成,请使用-gstabs+, -gstabs, -gxcoff+, -gxcoff, 或 -gvms 选项(后面带+的表示产生,不加的表示不产生)。

GCC allows you to use -g with -O. The shortcuts taken by optimized
code may occasionally produce surprising results: some variables
you declared may not exist at all; flow of control may briefly move
where you did not expect it; some statements may not be executed
because they compute constant results or their values were already
at hand; some statements may execute in different places because
they were moved out of loops.

可以让-g和-O选项同时使用。不过优化后的代码可能存在各种各样的问题

Nevertheless it proves possible to debug optimized output. This
makes it reasonable to use the optimizer for programs that might
have bugs.

但事实证明调试优化后的程序有可能性的。所以调试优化后的程序也是合理的。