By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

Critical Sections

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Xarky wrote:
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 news:comp.programming,
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
news:comp.lang.c and news:comp.programming, 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

Nov 14 '05 #2

P: n/a
In comp.lang.c Thomas Matthews <Th****************************@sbcglobal.net> wrote:

(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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.