Linux学习笔记1
Init进程,是系统初始化进程(引导进程)
ps aux //查看系统进程
获取进程id和进程父id
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
printf("pid=%d\n",getpid());
printf("ppid=%d\n",getppid());
return 0;
}

获取用户登录信息
利用man分别查看getlogin和getpwnam
//1.getlogin
#include <unistd.h> //所需头文件
char *getlogin(void); //调用参数和返回类型
//2. getpwnam
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
//2.1 查看passwd结构
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
//3. 具体实现代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
int main(int argc,char *argv[])
{
char *login =getlogin();
struct passwd *ps = getpwnam(login);
printf("name=%s\n",ps->pw_name);
printf("dir=%s\n",ps->pw_dir);
return 0;
}
system
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
system("nano"); //调用记事本
return 0;
}
秋风
2016-11-21