LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-18-2010, 07:42 AM   #1
Khaj.pandey
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Rep: Reputation: 0
Signals


Code:
 1  static volatile sig_atomic_t sigreceived = 0;
 2
 3  sigset_t maskall, maskmost, maskold;
 4  int signum = SIGUSR1;
 5
 6  sigfillset(&maskall);
 7  sigfillset(&maskmost);
 8  sigdelset(&maskmost, signum);
 9  sigprocmask(SIG_SETMASK, &maskall, &maskold);
10  if (sigreceived == 0)
11     sigsuspend(&maskmost);
12  sigprocmask(SIG_SETMASK, &maskold, NULL);

Consider the above code. Text i have says that this is the correct way to wait for SIGUSR1 signal , but i am unable to wrap my head around it.


so maskall -> all signals , maskmost-> all except sigusr1

Can some one explain what does this do?

sigprocmask(SIG_SETMASK, &maskall, &maskold);


and why is this :

sigsuspend(&maskmost);

Shouldn't the mask of sigsuspend contain only signal SIGUSR1?

Cheers!
 
Old 06-18-2010, 08:20 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Code:
sigprocmask(SIG_SETMASK, &maskall, &maskold);
This will set the current mask to maskall, and store the old masks in maskold so they can be restored later.

Have you tried reading the man pages? You might want to try 'apt-get install manpages manpages-dev' on Debian/Ubuntu, not sure about other distros but then there will be something similar. Then 'man sigprocmask' should give you all the info you need on that particular function (and the others in your example.
 
Old 06-18-2010, 09:52 AM   #3
Khaj.pandey
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks jhon, that does clear up a couple of things. I got a little confused bw sigsuspend & sigwait.

I have another doubt though , if sigsuspend restores the mask, why is this required ?

sigprocmask(SIG_SETMASK, &maskold, NULL);
 
Old 06-18-2010, 01:10 PM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by Khaj.pandey View Post
I have another doubt though , if sigsuspend restores the mask, why is this required ?

sigprocmask(SIG_SETMASK, &maskold, NULL);
First of all, the signal mask holds al those signals which should be blocked by the application. Though, by using maskmost, you would block all signals except SIGUSR1. For more details, please check the man pages as suggested by John or the POSIX rationale/spec which contains very useful information about how signals are handled, when signals are blocked, when they are just suspended, etc.

Regarding your second question, why this second sigprocmask(2) is required: please verify your code that maskmost != maskall != maskold. Though the last statement just restores the maskold state while sigsuspend restores from maskmost to maskall.

Andi
 
Old 06-18-2010, 02:18 PM   #5
Khaj.pandey
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks forza , i think i got what is happening now... Early on all signals are blocked , then all signals except sigusr1 is blocked, on receipt the state is restored to the last configuration.

This is a way to prevent a signal being missed from the looks of it.

I am now going to hijack this in a new direction -> Sockets!

When speaking of file descriptors, as i understand it is nothing more than an integer. This is an excerpt from Beej's guide to n/w programming. Stripped for clarity.

Code:
select(maxfd+1, &readfd,NULL,NULL,NULL)

for(int i =0;;i<maxfd ; i++)
 if(FD_ISSET(i,&readfd))
first up , there is no ordering between the fd's returned by the OS if i understand correctly. So read fd could have fd's like 10,24,28,32. Does this make sense to iterate all they way from 0 to maxfd??


PS: My unix box is dead atm , hence i am asking some silly stuff which would be clearer to program .

Cheers!

Last edited by Khaj.pandey; 06-18-2010 at 02:19 PM.
 
Old 06-18-2010, 05:24 PM   #6
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
You didn't really ask any explicit question, though I'm just commenting some of your statements :-)

Quote:
Originally Posted by Khaj.pandey View Post
When speaking of file descriptors, as i understand it is nothing more than an integer. This is an excerpt from Beej's guide to n/w programming. Stripped for clarity.
Yes, file descriptors are integer values


Quote:
Originally Posted by Khaj.pandey View Post
first up , there is no ordering between the fd's returned by the OS if i understand correctly. So read fd could have fd's like 10,24,28,32. Does this make sense to iterate all they way from 0 to maxfd??
Well, it depends on what you understand by ordering. The file/socket descriptors returned by the OS are in a specific order. The OS returns the lowest available socket descriptor, starting at 3 (due to stdin, stdout, stderr) for a newly created program. For example, if you would close stdin before creating a new socket, you'll get a socket id of 0.

Andi
 
Old 06-18-2010, 08:40 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Q: So read fd could have fd's like 10,24,28,32. Does this make sense to iterate all they way from 0 to maxfd??
Code:
// WRONG
  select(maxfd+1, &readfd,NULL,NULL,NULL)

  for(int i =0;;i<maxfd ; i++)
    if(FD_ISSET(i,&readfd))
Code:
// CORRECT
int sock_ready(int sock) 
{ 
    int             res; 
    fd_set          sready; 
    struct timeval  nowait; 

    FD_ZERO(&sready); 
    FD_SET((unsigned int)sock,&sready); 
    /*bzero((char *)&nowait,sizeof(nowait));*/ 
    memset((char *)&nowait,0,sizeof(nowait)); 

    res = select(sock+1,&sready,NULL,NULL,&nowait); 
    if( FD_ISSET(sock,&sready) ) 
        res = 1; 
    else 
        res = 0; 

    return(res); 
}
So in answer to your question:
1. Yes, it can make sense to iterate 0 .. maxfd

2. It usually makes MORE sense to check a specific list of descriptors

3. *Whatever* you do, you should *always* declare and zero out "sready" each and every time you call "select()"

'Hope that helps .. PSM
 
Old 06-19-2010, 08:54 AM   #8
Khaj.pandey
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks guys!

As i understand, the complete data might not be sent in one go by the send command... i.e. which pretty much means, the complete data is not recovered at the other end.... Any mechanisms to handle this ? i.e. if one was to write a structure and send it (only half is sent), and only half is received then how is the same handled?

From the top of my head , i would say , allocating memory using malloc , copying as much as you can in it.... doing a memcpy of the address returned via malloc + data read and the new offset. Obviously all this with another send by te sender.

I theory this sounds nice, not sure if it actually works.

Last edited by Khaj.pandey; 06-19-2010 at 08:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signals sx6 Programming 2 09-12-2009 04:10 PM
Signals palisetty_suman Linux - Newbie 2 02-15-2009 10:57 PM
!!! about signals !!! b2na Programming 4 02-04-2005 01:34 AM
!! about signals !!! b2na General 1 01-03-2005 05:37 PM
Signals Speek Programming 2 12-24-2004 05:58 AM


All times are GMT -5. The time now is 10:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration