Signal processing



  • Help me correct the program that releases the contents of the file on the screen by the N-string portions; each subsequent portion is issued after the key is pressed. Ctrl + C♪ Name of file and value N They're in the command line.
    Here's my code:

    #include <unistd.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    int N,f1;
    

    void a(int n){
    char c;
    while(N){
    read(f1,&c,1);
    if(c=='\n')
    N--;
    write(1,&c,1);
    }
    }

    int main (int argc,char **argv){
    signal(SIGINT,&a);
    N=atoi(argv[2]);
    f1=open(argv[1],O_RDONLY);
    return 0;
    }



  • After the opening, the program is running out and waiting for signals, I suggest we make a variable flag. FLAG=1 and after open to main while(FLAG) { sleep(100); } In the signal processor, when the data for withdrawal are no longer simply installed FLAG=0Sleep will stop the signal, so the cycle will end immediately.

    And after. while(N) I'd like N to make an equal argument again to read the next string at the next signal. And inside the cycle, read I'll give it back 0 when the file is over and then it's true. FLAG=0; break;


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2