简体   繁体   中英

How to exit the process in B program process, if input is -1 using signal in C

I would execute both process in A program and process in B program.

first of all, A program is executed.

and then, B program is executed.

A program and B program are sharing one data file.

A => input from user and then write a number to data file. B => read number from data file which is written by A

I want to do implementation that is process in A program input -1 and exit the A program

also, process in B program is finished using signal.

structure --

A sent sigusr1 to BB sent sigint to A

example

A : -1 (input) , print bye

B : print bye

How to send A program a sigusr1 to B program for this?

I'm thinking about this for a few days.

Thank you.

A program

do {
            scanf("%d",&num);
            if(num > 0)
            {
                    lseek(fd,0,SEEK_SET);
                    write(fd,&num,sizeof(num));
                    printf("writes ..... %d\n\n",num);
                    kill(bproc,SIGUSR1);
                    pause();
            }
            else
            {
                    kill(bproc,SIGUSR1);

            }
    } while ( num > 0 );
    printf("bye...\n");

B program

 while(rdnum > 0)
    {
            lseek(fd,0,SEEK_SET);
            read(fd,&rdnum,sizeof(int));
            printf("reads...  %d\n\n",rdnum);
            kill(Aproc,SIGINT);
            pause();
    }

    printf("bye...\n");
    exit(0);

Signals don't carry any data other than the signal number itself. You appear already to be using SIGUSR1 as a general attention signal from A to B, so you need to convey the information the B should terminate by other means, either in addition to or instead of the SIGUSR1 .

Among the more likely possibilities are

  • use a different signal to tell B to terminate, such as SIGUSR2 , or even SIGTERM .
  • record a special value or code in the file that A already uses to communicate data to B

But of course you do need B to handle the SIGUSR1 and any other signal you're sending. Since you're already handling signals in A, I presume that you can do similarly in B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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