操作系统作业: 在UNIX环境下用C语言编写一段程序显示系统信息。这个他代码应该写在哪里啊?

2025-06-26 17:20:59
推荐回答(3个)
回答1:

这是我写的代码你拿去参考一下,在UNIX中主要是调用POSIX API,跟Linux中一样:

#include 
#include 
#include 

int main(int argc,char* argv[])
{
  int i;
  struct utsname name;

  printf("Logged User:\t\t%s\n",getlogin());
  printf("User ID:\t\t%lu\n",getuid());
  for(i=0;i<15;i++)putchar('-');
  putchar(0xa);
  printf("System Information Here:\n");
  printf("Memory page size:\t%lu\n",getpagesize());
  if(uname(&name)!=-1){
    printf("Name of the implementation of the operating system:%s\n",name.sysname);
    printf("Name of this node on the network:%s\n",name.nodename);
    printf("Current release level of this implementation:%s\n",name.release);
    printf("Current version level of this release:%s\n",name.version);
    printf("Name of the hardware type the system is running on:%s\n",name.machine);
    printf("Name of the domain of this node on the network:%s\n",name.__domainname);
  }

  return 0;
}

获取信息其实可以很多,我的这个只是一个小部分。

如果你要获取一些硬件信息,可以打开/proc目录下的一些映射文件,如:要得知CPU的信息,则只要打开/proc/cpuinfo文件,查看该文件即可。

用system,个人感觉很“鸡肋”。


这是我的运行效果截图:


我在Linux中调试就是gdb,在编译时加一个参数“-g”。

即为:gcc -o code code.c -g 

code.c是我的代码文件。


UNIX中有个cc编译器,应该跟gcc差不多。

回答2:

unix操作系统的系统信息都存在特定路径下的文件里
通过system或者popen调用系统命令cat,显示相应信息即可。

回答3:

通过system()调用
uname -a
指令