Critical Sections

X

Xarky

Hi,
I am trying to learn Critical Sections. I have written a small
program. Source code problem below.

What the program is doing is disabling the CRTL-C signal in the
critical section. My problem is that when in the Critical Section I
press the CTRL-C signal more than once. As supposed nothing happens,
but when critical section ends, should it give me all the CTRL-C
signals pressed or just one (its giving me one)?

How should the exact implementation be?

I hope some one out there understands my problem.

Thanks in advance


/* Source Code */

#include <stdio.h>
#include <errno.h>
#include <signal.h>

int cnt;

void CTRL_C_Handler (int sig)
{
cnt++;
printf ("%d. Now we are in CTRL_C_Handler.\n", cnt);
(void) signal (SIGINT, CTRL_C_Handler);
} // end method CRTL_C_Handler

main()
{
system ("clear");

(void) signal (SIGINT, CTRL_C_Handler);
sigset_t masking;

cnt=0;
sigemptyset (&masking);
sigaddset (&masking, SIGINT);

if (sigprocmask (SIG_BLOCK, &masking, NULL) == -1)
printf ("An error encountered: %s.\n", strerror(errno));

printf ("Enterend in Critical Section. Last for 5 seconds.\n");
printf ("Pressing CTRL-C will be useless.\n");
sleep(5);

printf ("Critical Section is ending now.\n");
if (sigprocmask (SIG_UNBLOCK, &masking, NULL) == -1)
printf ("An error encountered: %s.\n", strerror(errno));

while(1); // infinite loop
} // end main
 
T

Thomas Matthews

Xarky said:
Hi,
I am trying to learn Critical Sections. I have written a small
program. Source code problem below.
[code snipped]

Sorry, but the _standard_ C language has no facilities or support
for Critical Sections so it is off-topic for this newsgroup.

However, a better newsgroup to discuss this is as it is an advanced programming concept. Just remember that not
all computer systems in the world support Critical Sections. And
when you post to a newsgroup that isn't platform dependent, like
and you are posting issues
that apply to all computer systems in the world (or most of them).

Followups set.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

Christopher Benson-Manica

(xpost and followups ignored, since this is metatopical)
However, a better newsgroup to discuss this is news:comp.programming,

I would also suggest comp.unix.programmer, given the code that OP
posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top