📄 state.c
字号:
#include <unistd.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ipc.h>#include <fcntl.h>#include "job.h"/* * command syntax * stat */void usage(){ printf("Usage: state\n");}int main(int argc,char *argv[]){ struct jobcmd statcmd; int fd,fifo; char fifoname[100]; struct JCB info; char timebuf[BUFLEN]; if (argc != 1) { usage(); return 1; } statcmd.type = STAT; statcmd.defpri = 0; statcmd.owner = getuid(); statcmd.argnum = 0; strcpy(fifoname,"/tmp/stat"); if (mkfifo(fifoname,0666) < 0) error_sys("mkfifo failed"); if ((fd = open("/tmp/server",O_WRONLY)) < 0) error_sys("state open fifo failed"); if (write(fd,&statcmd,DATALEN)< 0) error_sys("state write failed"); close(fd); if ((fifo = open("/tmp/stat",O_RDONLY)) < 0) error_sys("open fifo failed"); /* Print Header */ printf("JOBID\tPID\tUID\tRUNTIME\tWAITTIME\tCREATTIME\t\tSTATE\n"); while (read(fifo,&info,sizeof(struct JCB))) { strcpy(timebuf,ctime(&(info.create_time))); timebuf[strlen(timebuf) - 1] = '\0'; printf("%d\t%d\t%d\t%d\t%d\t%s\t%s\n", info.jid, info.pid, info.ownerid, info.run_time, info.wait_time, timebuf, (info.state == READY)?"READY":"RUNNING"); } close(fifo); if (remove("/tmp/stat") < 0) error_sys("remove failed"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -