हिंदी माध्यम नोट्स
Categories: c++ language in hindi
signals in c++ language in hindi , SIGABRT , SIGFPE , SIGILL , SIGINT , SIGSEGV , SIGTERM
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 <stdio.h> /* fopen, fputs, fclose, stderr */
#include <stdlib.h> /* abort, NULL */
int main ()
{
FILE * pFile;
pFile= fopen (“myfile.txt”,”r”);
if (pFile == NULL)
{
fputs (“error opening file\n”,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 <stdio.h>
#include <signal.h>
int main() {
error(getpid(), SIGFPE);
cout<<“erroneous arithmetic operation occur”;
getch();
}
3. SIGILL :
इस signal का use किसी illegal instruction को detect करने के लिए किया जाता है | जब किसी प्रोग्राम मे , illegal instruction को occur होता है तब SIGILL generate होता है | जो की प्रोग्राम को terminate कर देता है |
#include <stdio.h>
#include <signal.h>
int main() {
int a , b ;
a=10;
b=20;
a-b;
getch();
}
इस code मे जब a-b; perform होता है तब इस प्रकार का signal generate होता है |
SIGINT :
इस signal का use किसी interactive program interrupt signal को generate करने के लिए किया जाता है | इसके लिए निन्म code को उए किया जाता ही |
उदाहरन :
#include <stdio.h>
#include <signal.h>
int main() {
int a , b ;
a=10;
b=20;
call SIGINT ;
getch();
}
SIGSEGV :
इस signal का use , किसी storage के invalid access को डिटेक्ट करने के लिए किया जाता है |
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#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%lx\n”,(long) si->si_addr);
printf(“Implements the handler only\n”);
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%lx\n”, (long) buf);
printf(“Start of region: 0x%lx\n”, (long) buf+size);
printf(“Start of region: 0x%lx\n”, (long) buf+2*size);
printf(“Start of region: 0x%lx\n”, (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 memory\n”);
a = *p; //trying to read the memory
printf(“It comes here after reading memory\n”);
}
else
{
if (mprotect(buf + size * 0, size,PROT_READ) == -1)
handle_error(“mprotect”);
a = *p;
printf(“Now i can read the memory\n”);
}
for (p = buf;p<=buf+4*size ;p++ )
{
//a = *(p);
*(p) = ‘a’;
printf(“Writing at address %p\n”,p);
}
printf(“Loop completed\n”); /* 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 करने के लिए किया जाता है |
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 <stdio.h> /* fopen, fputs, fclose, stderr */
#include <stdlib.h> /* abort, NULL */
int main ()
{
FILE * pFile;
pFile= fopen (“myfile.txt”,”r”);
if (pFile == NULL)
{
fputs (“error opening file\n”,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 <stdio.h>
#include <signal.h>
int main() {
error(getpid(), SIGFPE);
cout<<“erroneous arithmetic operation occur”;
getch();
}
3. SIGILL :
इस signal का use किसी illegal instruction को detect करने के लिए किया जाता है | जब किसी प्रोग्राम मे , illegal instruction को occur होता है तब SIGILL generate होता है | जो की प्रोग्राम को terminate कर देता है |
#include <stdio.h>
#include <signal.h>
int main() {
int a , b ;
a=10;
b=20;
a-b;
getch();
}
इस code मे जब a-b; perform होता है तब इस प्रकार का signal generate होता है |
SIGINT :
इस signal का use किसी interactive program interrupt signal को generate करने के लिए किया जाता है | इसके लिए निन्म code को उए किया जाता ही |
उदाहरन :
#include <stdio.h>
#include <signal.h>
int main() {
int a , b ;
a=10;
b=20;
call SIGINT ;
getch();
}
SIGSEGV :
इस signal का use , किसी storage के invalid access को डिटेक्ट करने के लिए किया जाता है |
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#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%lx\n”,(long) si->si_addr);
printf(“Implements the handler only\n”);
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%lx\n”, (long) buf);
printf(“Start of region: 0x%lx\n”, (long) buf+size);
printf(“Start of region: 0x%lx\n”, (long) buf+2*size);
printf(“Start of region: 0x%lx\n”, (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 memory\n”);
a = *p; //trying to read the memory
printf(“It comes here after reading memory\n”);
}
else
{
if (mprotect(buf + size * 0, size,PROT_READ) == -1)
handle_error(“mprotect”);
a = *p;
printf(“Now i can read the memory\n”);
}
for (p = buf;p<=buf+4*size ;p++ )
{
//a = *(p);
*(p) = ‘a’;
printf(“Writing at address %p\n”,p);
}
printf(“Loop completed\n”); /* 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 करने के लिए किया जाता है |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago