简体   繁体   English

使用SIGQUIT和SIGINT

[英]use SIGQUIT and SIGINT

I'm trying to solve a problem because I'm learning to use system calls in C. I used a Ubuntu 12.04 64bit. 我正在尝试解决问题,因为我正在学习在C语言中使用系统调用。我使用的是Ubuntu 12.04 64位。

The statement of the problem says that I need to implement a code that allows to execute a command (cmd2) after the correct end of other command (cmd1). 问题的陈述说,我需要实现一个代码,该代码允许在其他命令(cmd1)正确结束之后执行命令(cmd2)。 Also says that user can specify both commands and all of the arguments that user wants. 还说用户可以同时指定命令和用户所需的所有参数。

At this point I create this little program: 至此,我创建了这个小程序:

#include <syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>


int main(void) 
{
    int cmd1 = system("sleep 5");


    if((cmd1 = (-1)))
    {
        write(1,"error in command 1",18);
    }
    if(cmd1=(0))
    {
        write(1, "hello world", 11);
    }
    return EXIT_SUCCESS;
}

Next, statement says that if the first command doesn't finish correctly the second will not execute, also, user can abort the execution of the cmd1 using Ctrl+\\ or Ctrl+4 (SIGQUIT) and the second command (cmd2) using Ctrl+C (SIGINT). 接下来,语句表明,如果第一个命令未正确完成,第二个命令也将不执行,用户也可以使用Ctrl + \\或Ctrl + 4(SIGQUIT)中止cmd1的执行,而使用Ctrl +中止第二个命令(cmd2) C(SIGINT)。 If second command is canceled the first must be completed normaly. 如果取消了第二个命令,则第一个命令必须正常完成。

I have problems in this second part of the statement because I never used this kind of things in C and also I'm really noob in linux. 我在声明的第二部分中遇到问题,因为我从未在C中使用过这种东西,而且在Linux中我确实是菜鸟。 I tried to read something about SIGQUIT and SIGINT but I don't understand all that I've read probably because there are a lot of things of linux that I've not learned yet. 我试图阅读有关SIGQUIT和SIGINT的内容,但我不了解所读内容,可能是因为有很多我还没有学到的Linux知识。

Can anyone help me please? 谁能帮我吗?

Thanks! 谢谢!

I edit the code for this version using if functions. 我使用if函数编辑该版本的代码。 It doesn't work correctly, I'm finding how to check if the first command finishes correctly. 它不能正常工作,我正在寻找如何检查第一个命令是否正确完成。

Just to get you started. 只是为了让您入门。 Let me explain the question because I feel you haven't understood it. 让我解释一下这个问题,因为我觉得您还不了解。

The statement of the problem says that I need to implement a code that allows to execute a command (cmd2) after the correct end of other command (cmd1). 问题的陈述说,我需要实现一个代码,该代码允许在其他命令(cmd1)正确结束之后执行命令(cmd2)。

You will will be given two commands by the user cmd1 and cmd2 . 用户cmd1cmd2将给您两个命令。 cmd1 should be executed first. cmd1应该首先执行。

Next, statement says that if the first command doesn't finish correctly the second will not execute, 接下来,声明说,如果第一个命令未正确完成,第二个命令将不会执行,

ONLY IF cmd1 finished executing normally should cmd2 be executed. 仅当 cmd1完成正常执行后,才应执行cmd2

If second command is canceled the first must be completed normaly. 如果取消了第二个命令,则第一个命令必须正常完成。

Execution of cmd1 is not dependent on cmd2 . cmd1执行不依赖于cmd2

user can abort the execution of the cmd1 using Ctrl+\\ or Ctrl+4 (SIGQUIT) 用户可以使用Ctrl + \\或Ctrl + 4(SIGQUIT)中止cmd1的执行

You seem confused here. 您似乎在这里感到困惑。 Here, they mean to say, cmd1 can be abnormally terminated by passing SIGQUIT to it(Ctrl+\\ or Ctrl+4), in which case cmd2 should not be executed. 在这里,他们的意思是说,可以通过将SIGQUIT传递给cmd1 (Ctrl + \\或Ctrl + 4)来异常终止,在这种情况下,不应执行cmd2 You do NOT have to code the signals part. 不必到信号部件代码。 What you have to do is, check how cmd1 was terminated and then execute cmd2 if it was a normal termination else don't execute cmd2. 您要做的是,检查cmd1是如何终止的,然后执行cmd2如果这是正常终止),否则不要执行cmd2。

Note The code in the question was extremely different when the above part was posted. 注意发布上述部分时,问题中的代码有很大不同。


This was your code on 5:02 AM Wednesday, 20 May 2015 GMT.(Had to include this because you are changing the code too often and too much) 这是您的代码,格林尼治标准时间2015年5月20日,星期三,上​​午5:02。(由于您更改代码的次数过多且过多,因此必须包含此代码)

int main(void) 
{
    int cmd1 = system("sleep 5");
    if((cmd1 = (-1)))
    {
        write(1,"error in command 1",18);
    }
    if(cmd1=(0))
    {
        write(1, "hello world", 11);
    }
    return EXIT_SUCCESS;
}
  1. Here you are using = for comparing. 在这里,您使用=进行比较。 = is used for assignment and not equality compassion. =用于分配,而不用于平等同情。 == is used for equality compassion. ==用于平等同情。 So, if((cmd1 = (-1))) and if(cmd1=(0)) should have been if((cmd1 == (-1))) and if(cmd1 == (0)) 因此, if((cmd1 = (-1)))if(cmd1=(0))应该是if((cmd1 == (-1)))if(cmd1 == (0))

  2. You are checking if the returned value is -1 for failure. 您正在检查失败的返回值是否为-1。 This is incorrect. 这是不正确的。 Exit code for success is 0 for failure is any value other than 0. So, if((cmd1 = (-1))) should have been if(cmd1 != 0) . 成功的退出代码为0,失败的值为0以外的任何其他值。因此, if((cmd1 = (-1)))应该是if(cmd1 != 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM