
正文
c语言重启命令linux linux c语言执行命令
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在Linux系统中,如何运行一个C语言程序?
1、打开kali linuxc语言重启命令linux的终端。创建一个文件并命名为test.c。在终端输入:touch test.c。
2、可以看到已经生成c语言重启命令linux了一个后缀为test.cc语言重启命令linux的源文件。然后用vim工具打开这个文件并编写代码。在终端中输入c语言重启命令linux:vim test.c或者gvim test.c打开这个文件并编写代码。
3、编写完了这个代码。现在开始编译源文件。在终端中输入:gcc test.cgcc是linux自带的c语言编译器。如果是windows则要用ide工具来编译。linux系统一般写C语言用gcc +vim+gdb三个自带的工具就可以了。
4、打完gcc test.c编译完C源文件。然后就可以看见a.out的文件。一般linux系统就默认为a.out为编译完的文件。现在运行a.out文件。在a.out文件的目录下打开终端并输入./a.out就是运行文件了。
5、如果想要编译完的文件名不要用a.out文件。就可以在编译时打入.gcc test.c -o test.out然后就可以看见有一个test.out.文件 了。-o后面跟着的编译生成的文件名。
6、再运行test.out在终端中输入./test.out结果如图。这样在linux系统下编译并运行C语言就完成了。
相关问答
Q1: 怎么用C语言实现linux的命令
命令是查询当前登录的每个用户,它的输出包括用户名、终端类型、登录日期及远程主机,在Linux系统中输入who命令输出如下:
我们先man一下who,在帮助文档里可以看到,who命令是读取/var/run/utmp文件来得到以上信息的。
我们再man一下utmp,知道utmp这个文件,是二进制文件,里面保存的是结构体数组,这些数组是struct utmp结构体的。
struct utmp {
short ut_type;
pid_t ut_pid;
char ut_line[UT_LINESIZE];
char ut_id[4];
char ut_user[UT_NAMESIZE];
char ut_host[UT_HOSTSIZE];
struct {
int32_t tv_sec;
int32_t tv_usec;
} ut_tv;
/***等等***/
};
要实现who只需要把utmp文件的所有结构体扫描过一遍,把需要的信息显示出来就可以了,我们需要的信息有ut_user、ut_line、ut_tv、ut_host。
老师给的初始代码:who1.c运行结果如下:
需要注意的是utmp中所保存的时间是以秒和微妙来计算的,所以我们需要把这个时间转换为我们能看懂的时间,利用命令man -k time | grep 3搜索C语言中和时间相关的函数:
经过搜索发现了一个ctime()函数,似乎可以满足我们的需求,于是对代码中关于时间的printf进行修改:
printf("%s",ctime(utbufp-ut_time));
编译运行发现出来的结果虽然已经转换成了我们能看懂的时间格式,但是很明显这个时间是错的:
搜索一下ut_time这个宏,发现它被定义为int32_t类型:
但是ctime()函数中要求参数的类型是time_t类型,所以重新定义一下类型,编译运行之后,发现时间已经改成了正确的,但是发现()中的内容被换行了,猜想ctime()函数的返回值可能自动在最后补了一个字符\n:
一开始想通过\r\b来实现“退行”,但实践后发现并不可取,最后考虑到直接修改字符串中最后一个字符为\0,让其字符串结束,使输出达到与系统who命令一样的效果,即在输出语句前添加如下代码:
cp[strlen(cp)-1] = '\0'
最后编译执行效果,发现解决了该问题:
虽然能看出基本上和who指令的执行结果一致,但是并非完全一样,主要在两点,第一是时间格式不一样,第二个是比who执行的结果多了几条,需要注意的是utmp中保存的用户,不仅仅是已经登陆的用户,还有系统的其他服务所需要的“用户”,所以在显出所有登陆用户的时候,应该过滤掉其他用户,只保留登陆用户。我们可以通过ut_type来区别,登陆用户的ut_type是USER_PROCESS。
先用if语句对执行结果进行过滤,效果如下:
接着解决时间格式问题,利用man命令收到了两个非常有用的函数:localtime()和strftime(),localtime()是把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,strftime()则是用来定义时间格式的,如:年-月-日,利用这两个函数对时间进行修改后,结果显示终于和系统中who命令一模一样:
最终完整的代码如下:
#include stdio.h
#include stdlib.h
#include utmp.h
#include fcntl.h
#include unistd.h
#include time.h
#define SHOWHOST
void show_time(long timeval){
char format_time[40];
struct tm *cp;
cp = localtime(timeval);
strftime(format_time,40,"%F %R",cp);
printf("%s",format_time);
}
int show_info( struct utmp *utbufp )
{
if(utbufp-ut_type == USER_PROCESS){
printf("%-8.8s", utbufp-ut_name);
printf(" ");
printf("%-8.8s", utbufp-ut_line);
printf(" ");
show_time(utbufp-ut_time);
printf(" ");
#ifdef SHOWHOST
printf("(%s)", utbufp-ut_host);
#endif
printf("\n");
}
return 0;
}
int main()
{
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){
perror( UTMP_FILE );
exit(1);
}
while ( read(utmpfd, current_record, reclen) == reclen )
show_info(current_record);
close(utmpfd);
return 0;
}
Q2: c语言中 重启电脑的命令是什么
重启:shutdown -r
关机:shutdown -h
其他命令参数如下。
Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "c
omment"] [-d up:xx:yy]
No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c "comment" Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without war
ning
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer le
ss than 256)
yy is the minor reason code (positive integer le
ss than 65536)
Q3: linux C语言实现意外退出后自动重启程序
在脚本里 可以用
jobs | grep 进程号 || a.outc语言重启命令linux的绝对路径
Q4: 如何在C语言编程中调用linux系统终端下的命令
system(执行shell 命令)
相关函数 fork,execve,waitpid,popen
表头文件 #includestdlib.h
定义函数 int system(const char * string);
函数说明 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。
返回值 如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。
附加说明 在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。
范例 #includestdlib.h
main()
{
system(“ls -al /etc/passwd /etc/shadow”);
}
执行 -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
-r--------- 1 root root 572 Sep 2 15 :34 /etc/shadow
Q5: 写一个linux下写个关于c语言的双守护进程,就是监视一个进程,当其死掉,马上将其重启
可以分三步来做c语言重启命令linux:
做两个简单的守护进程c语言重启命令linux,并能正常运行
监控进程是否在运行
启动进程
综合起来就可以c语言重启命令linux了c语言重启命令linux,代码如下c语言重启命令linux:
被监控进程thisisatest.c(来自):
#includeunistd.h
#includesignal.h
#includestdio.h
#includestdlib.h
#includesys/param.h
#includesys/types.h
#includesys/stat.h
#includetime.h
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid0)
exit(1); //创建错误,退出
else if(pid0) //父进程退出
exit(0);
setsid(); //使子进程成为组长
pid=fork();
if(pid0)
exit(0); //再次退出,使进程不是组长,这样进程就不会打开控制终端
else if(pid0)
exit(1);
//关闭进程打开的文件句柄
for(i=0;iNOFILE;i++)
close(i);
chdir("/root/test"); //改变目录
umask(0);//重设文件创建的掩码
return;
}
void main()
{
FILE *fp;
time_t t;
init_daemon();
while(1)
{
sleep(60); //等待一分钟再写入
fp=fopen("testfork2.log","a");
if(fp=0)
{
time(t);
fprintf(fp,"current time is:%s\n",asctime(localtime(t))); //转换为本地时间输出
fclose(fp);
}
}
return;
}
监控进程monitor.c:
#includeunistd.h
#includesignal.h
#includestdio.h
#includestdlib.h
#includesys/param.h
#includesys/types.h
#includesys/stat.h
#includetime.h
#includesys/wait.h
#includefcntl.h
#includelimits.h
#define BUFSZ 150
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid0)
exit(1); //创建错误,退出
else if(pid0) //父进程退出
exit(0);
setsid(); //使子进程成为组长
pid=fork();
if(pid0)
exit(0); //再次退出,使进程不是组长,这样进程就不会打开控制终端
else if(pid0)
exit(1);
//关闭进程打开的文件句柄
for(i=0;iNOFILE;i++)
close(i);
chdir("/root/test"); //改变目录
umask(0);//重设文件创建的掩码
return;
}
void err_quit(char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
// 判断程序是否在运行
int does_service_work()
{
FILE* fp;
int count;
char buf[BUFSZ];
char command[150];
sprintf(command, "ps -ef | grep thisisatest | grep -v grep | wc -l" );
if((fp = popen(command,"r")) == NULL)
err_quit("popen");
if( (fgets(buf,BUFSZ,fp))!= NULL )
{
count = atoi(buf);
}
pclose(fp);
return count;
// exit(EXIT_SUCCESS);
}
void main()
{
FILE *fp;
time_t t;
int count;
init_daemon();
while(1)
{
sleep(10); //等待一分钟再写入
fp=fopen("testfork3.log","a");
if(fp=0)
{
count = does_service_work();
time(t);
if(count0)
fprintf(fp,"current time is:%s and the process exists, the count is %d\n",asctime(localtime(t)), count); //转换为本地时间输出
else
{
fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",asctime(localtime(t))); //转换为本地时间输出
system("/home/user/daemon/thisisatest"); //启动服务
}
fclose(fp);
}
}
return;
}
具体CMD命令:
cc thisisatest.c -o thisisatest
./thisisatest
cc monitor.c -o monitor
./monitor
tail -f testfork3.log -- 查看日志
c语言重启命令linux的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux c语言执行命令、c语言重启命令linux的信息别忘了在本站进行查找喔。







