system()to execxx()和错误处理信号
我正在尝试根据旧考试论文制作一个小程序作为练习。该程序要做的是:
- 2≤n≤4个调用参数,每个参数是unix命令(即谁,日期)。
- 创建n子进程,每个tem打开执行给定的每个命令的xterm终端。终端必须保持打开状态,直到进入。
- 如果该过程正常关闭(使用ENTER),则
- 如果使用信号关闭过程/终端,请重新打开它,只需写消息,
我唯一的问题是第四点。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void catchsig(int sig){
;
}
int main(int argc, char **argv){
int pid, exit_status, proc_id, p_proc_id;
int proc[argc-1];
char buf[1024];
struct sigaction sa;
sa.sa_handler = &catchsig;
for (int i=0; i<argc-1; i++){
proc[i]=0;
}
/*Argument control*/
if ((argc-1)<2){
perror("Error");
exit(1);
}
else if ((argc-1)>4){
perror("Error");
exit(1);
}
for (int i=1; i<argc; i++){
snprintf(buf, sizeof buf, "xterm -e '%s;read'", argv[i]);
if ((pid=fork())<0){
perror("Error");
exit(1);
}
/*child*/
else if (pid==0 && proc[i-1]==0){
int f;
f = i-1;
close(piped[1]);
p_proc_id=getppid();
proc_id = getpid();
while (proc[f]==0){
printf("Process %s; PID %d \n",argv[i], proc_id);
exit_status = system(buf);
printf("Process %s; exit status = %d; if exited = %d; WEXITSTATUS = %d; WIFSIGNALED = %d; WTERMSIG=%d\n", argv[i], exit_status, WIFEXITED(exit_status), WEXITSTATUS(exit_status), WIFSIGNALED(exit_status), WTERMSIG(exit_status));
if (WIFSIGNALED(exit_status)){
proc[f]=1;
printf("Killed by signal");
}
}
}
/*parent*/
else{
;
}
}
exit(0);
}
我认为这是因为我使用了System()
,但是真诚的,我不知道如何用execxx()
替换它,从而可以执行同一件事。 提前致谢!
编辑: 我尝试使用execl()
和wait()
而不是sysytem()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void catchsig(int sig){
;
}
int main(int argc, char **argv){
int pid, proc_id, p_proc_id, status, stat;
int piped[2];
int proc[argc-1];
char buf[1024], key;
char buffe[argc];
struct sigaction sa;
sa.sa_handler = &catchsig;
if (pipe(piped)<0){
exit(1);
}
for (int i=0; i<argc-1; i++){
proc[i]=0;
}
status = 0;
/*Argument control*/
if ((argc-1)<2){
perror("Error");
exit(1);
}
else if ((argc-1)>4){
perror("Error");
exit(1);
}
while (status == 0){
for (int i=1; i<argc; i++){
int f;
f=i-1;
snprintf(buf, sizeof buf, "%s;read", argv[i]);
if ((pid=fork())<0){
perror("Errore fork");
exit(1);
}
/*child*/
else if (pid==0 && proc[f]==0){
proc_id = getpid();
printf("Process %s; PID %d \n",argv[i], proc_id);
execl("/usr/bin/xterm", "/usr/bin/xterm", "-e", buf, (void*)NULL);
}
/*parent*/
else{
wait(&stat);
printf("Process %s; exit status = %d; if exited = %d; WEXITSTATUS = %d; WIFSIGNALED = %d; WTERMSIG=%d\n", argv[i], stat, WIFEXITED(stat), WEXITSTATUS(stat), WIFSIGNALED(stat), WTERMSIG(stat));
if (WIFSIGNALED(stat)){
proc[f]=1;
printf("Process closed \n");}
for (int s=0; s<argc-1; s++){
if (proc[s]==1){
status=1;}
else if (proc[s]==0){
status=0;
break;}
}
}
printf("status=%d \n", status);
}
}
exit(0);
}
不幸的是,此代码的问题/限制是2:
- 它只能在时间开放1个Xterm终端。
- 我注意到,如果仅保留1个孩子的过程,并且我继续使用Enter关闭它,它将创建更多的父母流程,每个过程都执行该子过程。
I'm trying to make a small program as exercise based on old exam papers. What the program has to do is:
- 2≤N≤4 invocation arguments each of which are UNIX command (i.e. who, date).
- Create N child process each of tem opening an xterm terminal executing each of the command given. The terminal must stay open until enter is hit.
- If the process is closed normally (with enter) then re-open it
- If the process/terminal is closed using signal just write a message
My only problem is the 4th point.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void catchsig(int sig){
;
}
int main(int argc, char **argv){
int pid, exit_status, proc_id, p_proc_id;
int proc[argc-1];
char buf[1024];
struct sigaction sa;
sa.sa_handler = &catchsig;
for (int i=0; i<argc-1; i++){
proc[i]=0;
}
/*Argument control*/
if ((argc-1)<2){
perror("Error");
exit(1);
}
else if ((argc-1)>4){
perror("Error");
exit(1);
}
for (int i=1; i<argc; i++){
snprintf(buf, sizeof buf, "xterm -e '%s;read'", argv[i]);
if ((pid=fork())<0){
perror("Error");
exit(1);
}
/*child*/
else if (pid==0 && proc[i-1]==0){
int f;
f = i-1;
close(piped[1]);
p_proc_id=getppid();
proc_id = getpid();
while (proc[f]==0){
printf("Process %s; PID %d \n",argv[i], proc_id);
exit_status = system(buf);
printf("Process %s; exit status = %d; if exited = %d; WEXITSTATUS = %d; WIFSIGNALED = %d; WTERMSIG=%d\n", argv[i], exit_status, WIFEXITED(exit_status), WEXITSTATUS(exit_status), WIFSIGNALED(exit_status), WTERMSIG(exit_status));
if (WIFSIGNALED(exit_status)){
proc[f]=1;
printf("Killed by signal");
}
}
}
/*parent*/
else{
;
}
}
exit(0);
}
I think it's because I used system()
, but sincerely, I don't know how to replace it with an execXX()
which does the same thing.
Thanks in advance!
Edit:
I tried re-write the code using execl()
and wait()
instead of sysytem()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
void catchsig(int sig){
;
}
int main(int argc, char **argv){
int pid, proc_id, p_proc_id, status, stat;
int piped[2];
int proc[argc-1];
char buf[1024], key;
char buffe[argc];
struct sigaction sa;
sa.sa_handler = &catchsig;
if (pipe(piped)<0){
exit(1);
}
for (int i=0; i<argc-1; i++){
proc[i]=0;
}
status = 0;
/*Argument control*/
if ((argc-1)<2){
perror("Error");
exit(1);
}
else if ((argc-1)>4){
perror("Error");
exit(1);
}
while (status == 0){
for (int i=1; i<argc; i++){
int f;
f=i-1;
snprintf(buf, sizeof buf, "%s;read", argv[i]);
if ((pid=fork())<0){
perror("Errore fork");
exit(1);
}
/*child*/
else if (pid==0 && proc[f]==0){
proc_id = getpid();
printf("Process %s; PID %d \n",argv[i], proc_id);
execl("/usr/bin/xterm", "/usr/bin/xterm", "-e", buf, (void*)NULL);
}
/*parent*/
else{
wait(&stat);
printf("Process %s; exit status = %d; if exited = %d; WEXITSTATUS = %d; WIFSIGNALED = %d; WTERMSIG=%d\n", argv[i], stat, WIFEXITED(stat), WEXITSTATUS(stat), WIFSIGNALED(stat), WTERMSIG(stat));
if (WIFSIGNALED(stat)){
proc[f]=1;
printf("Process closed \n");}
for (int s=0; s<argc-1; s++){
if (proc[s]==1){
status=1;}
else if (proc[s]==0){
status=0;
break;}
}
}
printf("status=%d \n", status);
}
}
exit(0);
}
Unfortunally the problems/limitations of this code are 2:
- It can only open 1 xterm terminal at time.
- I noticed that if just 1 child process remain and I keep closing it with enter, it will create more parents processes, each of them executing that child process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论