Signals in c++ language in hindi

SIGABRT , SIGFPE , SIGILL , SIGINT , SIGSEGV , SIGTERM , signals in c++ language in hindi :- इससे पहले के article मे , namespace ,exception handeling , template और class को discuss किया अब इस article मे , Signals को discuss करेगे |
Signals एक code है जो की किसी सॉफ्टवेर के execution को interrupts करता है | सॉफ्टवेर एक code होता है जो की प्रोसेस को किसी operating system तक पहुचता है | Signals को ओसके से डायरेक्ट ही generate किया जाता है | OS की तरह से Signals को तब ही generate किया जा सकता हगी जब OS मे कोई error condition आ जाती है | प्रोग्रामर की Ctrl+C का use करते है किसी सॉफ्टवेर को execution को interrupt करने के लिए के लिए | अतः इस article मे , हम c++ language के कुछ defualt Signals को discuss करेगे | और इनके उदाहरन को भी discuss करेगे |
c++ language मे कुछ Signals होते है जिसे प्रोग्राम मे कभी भी catch नहीं किया जा सकता है | इस article मे निन्म Signals को discuss किया गया है | इसके लिए csignal को header portion मे हमेशा  include किया गया है
1. SIGABRT :
इस Signal का use किसी प्रोग्राम को Abnormal terminate करने के लिए use किया जाता है | इसके लिए call और abort function को use किया जाता है |abort () function SIGABRT signal को generate करता है जिससे सॉफ्टवेर या प्रोग्राम का execution interrupt हो जाता है | इसके लिए निन्म उदाहरन है :-
/* abort example */
#include       /* fopen, fputs, fclose, stderr */
#include      /* abort, NULL */
int main ()
{
FILE * pFile;
pFile= fopen (“myfile.txt”,”r”);
if (pFile == NULL)
{
fputs (“error opening filen”,stderr);
abort();
}
/* regular process here */
fclose (pFile);
return 0;
}
इस प्रोग्राम मे , जब abort() call किया जाता है तब प्रोग्राम मे SIGABRT generate होता है |
2. SIGFPE :
इस signal का use तब किया जाता है जब erroneous arithmetic operation perform किया जाता है | जैसे किसी non zero value को zero से divide करने पर ये signal generate होता है जो की प्रोग्राम को terminate कर देता है |
Example :
#include
#include
int main() {
error(getpid(), SIGFPE);
cout
getch();
}
3. SIGILL :
इस signal का use किसी illegal instruction को detect करने के लिए किया जाता है | जब किसी प्रोग्राम मे ,  illegal instruction को occur होता है तब SIGILL generate होता है | जो की  प्रोग्राम को terminate कर देता है |
#include
#include
int main() {
int a , b ;
a=10;
b=20;
a-b;
getch();
}
इस code मे जब a-b; perform होता है तब इस प्रकार का signal generate होता है |

Also read : Signal Handling in hindi


SIGINT :
इस signal का use किसी interactive program interrupt signal को generate करने के लिए किया जाता है | इसके लिए निन्म code को उए किया जाता ही |
उदाहरन :
#include
#include
int main() {
int a , b ;
a=10;
b=20;
call SIGINT ;
getch();
}
SIGSEGV :
इस signal का use , किसी storage के invalid access को डिटेक्ट करने के लिए किया जाता है |
#include
#include
#include
#include
#include
#include
#define handle_error(msg)
do { perror(msg); exit(EXIT_FAILURE); } while (0)
char *buf;
int flag=0;
static void handler(int sig, siginfo_t *si, void *unused)
{
printf(“Got SIGSEGV at address: 0x%lxn”,(long) si->si_addr);
printf(“Implements the handler onlyn”);
flag=1;
//exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
char *p; char a;
int size;
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == -1)
handle_error(“sigaction”);
size=4096;
/* Allocate a buf aligned on a page boundary;
initial protection is PROT_READ | PROT_WRITE */
buf = memalign(size, 4 * size);
if (buf == NULL)
handle_error(“memalign”);
printf(“Start of region:        0x%lxn”, (long) buf);
printf(“Start of region:        0x%lxn”, (long) buf+size);
printf(“Start of region:        0x%lxn”, (long) buf+2*size);
printf(“Start of region:        0x%lxn”, (long) buf+3*size);
//if (mprotect(buf + size * 0, size,PROT_NONE) == -1)
if (mprotect(buf + size * 0, size,PROT_NONE) == -1)
handle_error(“mprotect”);
//for (p = buf ; ; )
if(flag==0)
{
p = buf+size/2;
printf(“It comes here before reading memoryn”);
a = *p; //trying to read the memory
printf(“It comes here after reading memoryn”);
}
else
{
if (mprotect(buf + size * 0, size,PROT_READ) == -1)
handle_error(“mprotect”);
a = *p;
printf(“Now i can read the memoryn”);
}
for (p = buf;p
{
//a = *(p);
*(p) = ‘a’;
printf(“Writing at address %pn”,p);
}
printf(“Loop completedn”);     /* Should never happen */
exit(EXIT_SUCCESS);
}
इस code को c language मे लिखा गया क्योकि c++ language मे signal को generate करने के लिए code की length काफी बाद जाती है |
SIGTERM :
इस signal का use किसी प्रोग्राम को terminate करने के लिए request को generate करता है | इसके लिए किसी signal को short टर्म के लिए generate कोइय जाता है जिसका use किसी प्रोग्राम को short period के लिए  terminate करने के लिए किया जाता है |SIGHUP: Hang-up (POSIX),
इस signal का use , किसी user’s terminal  को disconnect करने के लिए किया जाता है |इस signal का use किसी controlling process के terminate करने के लिए किया जाता है |

हम आशा करते है कि यह नोट्स आपकी स्टडी में उपयोगी साबित हुए होंगे | अगर आप लोगो को इससे रिलेटेड कोई भी किसी भी प्रकार का डॉउट हो तो कमेंट बॉक्स में कमेंट करके पूंछ सकते है |
आप इन्हे अपने Classmates & Friends के साथ शेयर कर सकते है |

Leave a Comment

Your email address will not be published. Required fields are marked *