473,583 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's going on here?

I'm trying to write a very simple program that uses a signal-based
synchronization between two processes. Here's it:
-----------------------------------------------
/* The world's simplest syncronization example */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void handler(int sig);
void wait_signal();

int main() {
sigset_t curr_mask;
struct sigaction act;
pid_t pid;

act.sa_handler = handler;
sigaction(SIGUS R1, &act, NULL);

sigprocmask(0, NULL, &curr_mask);
sigaddset(&curr _mask, SIGUSR1);
sigprocmask(SIG _SETMASK, &curr_mask, NULL);

pid = fork();

/* Strict alternation */
if (pid > 0) {
while(1) {
printf("Parent\ n");
/* Tell the child we're done */
kill(pid, SIGUSR1);
wait_signal();
}
}
else {
pid_t parent_pid = getppid();

while(1) {
wait_signal();
printf("Child\n ");
/* Tell the parent we're done */
kill(parent_pid , SIGUSR1);
}
}
}

void handler(int sig) {
/* do nothing, just used for sync */
}

void wait_signal() {
sigset_t mask;
sigemptyset(&ma sk);
sigsuspend(&mas k);
}
-----------------------------------------------

With a 2.6 linux kernel, this hangs miserably with this output:

Parent
Child
Parent

Doing an strace yelds the following result:
-------------------
execve("./myprog", ["./myprog"], [/* 56 vars */]) = 0
uname({sys="Lin ux", node="scooter", ...}) = 0
brk(0) = 0x501000
mmap(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
0) = 0x2a9556b000
open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or
directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFRE G|0644, st_size=62108, ...}) = 0
mmap(NULL, 62108, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2a9556c000
close(3) = 0
open("/lib/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\ 0\0\0\0\0\0\0\0 \0\3\0>\0\1\0\0 \0`\315\1\0"... ,
640) = 640
lseek(3, 624, SEEK_SET) = 624
read(3, "\4\0\0\0\20\0\ 0\0\1\0\0\0GNU\ 0\0\0\0\0\2\0\0 \0\6\0\0\0"...,
32) = 32
fstat(3, {st_mode=S_IFRE G|0755, st_size=1232856 , ...}) = 0
mmap(NULL, 2215816, PROT_READ|PROT_ EXEC, MAP_PRIVATE, 3, 0) =
0x2a9566b000
mprotect(0, 1085320, PROT_NONE) = -1 ENOMEM (Cannot allocate
memory)
mmap(0x2a9586b0 00, 102400, PROT_READ|PROT_ WRITE,
MAP_PRIVATE|MAP _FIXED, 3, 0x100000) = 0x2a9586b000
mmap(0x2a958840 00, 16264, PROT_READ|PROT_ WRITE,
MAP_PRIVATE|MAP _FIXED|MAP_ANON YMOUS, -1, 0) = 0x2a95884000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
0) = 0x2a95888000
arch_prctl(0x10 02, 0x2a95888640) = 0
munmap(0x2a9556 c000, 62108) = 0
open("/dev/urandom", O_RDONLY) = 3
read(3, "\304fd\200\34\ 2727f\333\316\3 35\370\334\251\ 332Pi\235-"...,
64) = 64
close(3) = 0
rt_sigaction(SI GUSR1, {0x400819, [HUP],
SA_RESTART|SA_O NESHOT|SA_NOCLD STOP|0x555fa10} , NULL, 8) = 0
rt_sigprocmask( SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask( SIG_SETMASK, [USR1], NULL, 8) = 0
clone(child_sta ck=0,
flags=CLONE_CHI LD_CLEARTID|CLO NE_CHILD_SETTID |SIGCHLD,
child_tidptr=0x 2a958886d0) = 16791
fstat(1, {st_mode=S_IFCH R|0600, st_rdev=makedev (136, 2), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
0) = 0x2a9556c000
write(1, "Parent\n", 7Parent
) = 7
kill(16791, SIGUSR1) = 0
rt_sigsuspend([]Child
<unfinished ...>
--- SIGUSR1 (User defined signal 1) @ 0 (0) ---
<... rt_sigsuspend resumed> ) = -1 EINTR (Interrupted system
call)
rt_sigreturn(0x a) = -1 EINTR (Interrupted system
call)
write(1, "Parent\n", 7Parent
) = 7
kill(16791, SIGUSR1) = 0
rt_sigsuspend([] <unfinished ...>
--- SIGCHLD (Child exited) @ 0 (0) ---
----------------------------------------
And that's why it hangs, of course. The call to rt_sigaction shows
that it's called with the flag (among the others) SA_ONESHOT.

The funny part comes now: if I modify line 11 from

sigset_t curr_mask;

to, say

sigset_t curr_mask, mask1, mask2; /* add two unused variables */

it runs just fine!!

Here's the strace output (only the beginning, of course)
-------------------------------------------------
execve("./myprog", ["./myprog"], [/* 56 vars */]) = 0
uname({sys="Lin ux", node="scooter", ...}) = 0
brk(0) = 0x501000
mmap(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
0) = 0x2a9556b000
open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or
directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFRE G|0644, st_size=62108, ...}) = 0
mmap(NULL, 62108, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2a9556c000
close(3) = 0
open("/lib/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\ 0\0\0\0\0\0\0\0 \0\3\0>\0\1\0\0 \0`\315\1\0"... ,
640) = 640
lseek(3, 624, SEEK_SET) = 624
read(3, "\4\0\0\0\20\0\ 0\0\1\0\0\0GNU\ 0\0\0\0\0\2\0\0 \0\6\0\0\0"...,
32) = 32
fstat(3, {st_mode=S_IFRE G|0755, st_size=1232856 , ...}) = 0
mmap(NULL, 2215816, PROT_READ|PROT_ EXEC, MAP_PRIVATE, 3, 0) =
0x2a9566b000
mprotect(0, 1085320, PROT_NONE) = -1 ENOMEM (Cannot allocate
memory)
mmap(0x2a9586b0 00, 102400, PROT_READ|PROT_ WRITE,
MAP_PRIVATE|MAP _FIXED, 3, 0x100000) = 0x2a9586b000
mmap(0x2a958840 00, 16264, PROT_READ|PROT_ WRITE,
MAP_PRIVATE|MAP _FIXED|MAP_ANON YMOUS, -1, 0) = 0x2a95884000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
0) = 0x2a95888000
arch_prctl(0x10 02, 0x2a95888640) = 0
munmap(0x2a9556 c000, 62108) = 0
open("/dev/urandom", O_RDONLY) = 3
read(3, "\235!\fl\347\3 70Y\20}$\4\232N 97\263W\201\317 \273\4\230"...,
64) = 64
close(3) = 0
rt_sigaction(SI GUSR1, {0x400819, [ILL TRAP ABRT FPE USR1 SEGV USR2
PIPE ALRM STKFLT CONT STOP TTIN URG XFSZ PROF IO RTMIN RT_2 RT_4
RT_6], SA_NOCLDSTOP|0x 4000000}, NULL, 8) = 0
rt_sigprocmask( SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask( SIG_SETMASK, [USR1], NULL, 8) = 0
clone(child_sta ck=0,
flags=CLONE_CHI LD_CLEARTID|CLO NE_CHILD_SETTID |SIGCHLD,
child_tidptr=0x 2a958886d0) = 16892
fstat(1, {st_mode=S_IFCH R|0600, st_rdev=makedev (136, 2), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_ WRITE, MAP_PRIVATE|MAP _ANONYMOUS, -1,
0) = 0x2a9556c000
--------------------------------------------------------
This shows that rt_sigaction is called with different arguments this
time (without SA_ONESHOT)!

What's going on here? This is driving me mad. The compiler is
gcc-3.3.3.

Thanks for any help.
Nov 14 '05 #1
3 2192
su****@katamail .com (subnet) writes:
I'm trying to write a very simple program that uses a signal-based
synchronization between two processes. Here's it:
-----------------------------------------------
/* The world's simplest syncronization example */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
So far, so good.
#include <unistd.h>


Sorry, you've just exceeded the bounds of comp.lang.c. The <unistd.h>
header is system-specific; it's not defined by the C standard. You
can probably get help in comp.unix.progr ammer.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #2
Keith Thompson <ks***@mib.or g> wrote in message news:<ln******* *****@nuthaus.m ib.org>...
Sorry, you've just exceeded the bounds of comp.lang.c. The <unistd.h>
header is system-specific; it's not defined by the C standard. You
can probably get help in comp.unix.progr ammer.


Ok, I apologize with all the readers of this newsgroup for wasting
your time. Also, thanks for your kind and polite answer (when you post
to the wrong newsgroup, you usually get much harsher answers).
Sorry.
Nov 14 '05 #3
subnet wrote:
Keith Thompson <ks***@mib.or g> wrote:
Sorry, you've just exceeded the bounds of comp.lang.c. The
<unistd.h> header is system-specific; it's not defined by the C
standard. You can probably get help in comp.unix.progr ammer.


Ok, I apologize with all the readers of this newsgroup for
wasting your time. Also, thanks for your kind and polite answer
(when you post to the wrong newsgroup, you usually get much
harsher answers). Sorry.


A breath of fresh air. He gets it, learns quickly, and will
doubtless be back with pertinent problems etc. without long
arguments about topicality.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
6389
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a single article which can be viewed at http://www.tonymarston.net/php-mysql/good-bad-oop.html I fully expect this to be the start of another flame...
125
14635
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
9
1649
by: andrew | last post by:
Hi, I posted this message recently, but received no response, so I figured I'd try again. I can't find anybody who can tell me what's going on here. I'm having a problem with fieldsets in IE moving around on me as I hover from one table to another. The problem only appears in IE (I'm using v6). If I hover over the tables row, the bottom...
13
5029
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
8
3136
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that basically entitled to us to just about every .Net development tool you can imagine. I cant even begin to mention them. To begin with, my...
63
4721
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never be used. I'm wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?
18
3118
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is how global variables and function returns work... For example, I have a global array "char datestring;" which is defined in the function speakdate....
669
25766
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
6
1749
by: LurfysMa | last post by:
I am working on an electronic flashcard application. I have one version up and running using Visual Basic (6.0) and Access 2000. My long-range plans are to put it up on a website and sell subscriptions. But that is probably 2-3 releases away. In the meantime, I would like to put it up on the website for testing. I have been reading here...
10
2107
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine* pengine; Than later:
0
7888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.