473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(SIGUSR1, &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(&mask);
sigsuspend(&mask);
}
-----------------------------------------------

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="Linux", 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_IFREG|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`\3 15\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_IFREG|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(0x2a9586b000, 102400, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED, 3, 0x100000) = 0x2a9586b000
mmap(0x2a95884000, 16264, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x2a95884000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x2a95888000
arch_prctl(0x1002, 0x2a95888640) = 0
munmap(0x2a9556c000, 62108) = 0
open("/dev/urandom", O_RDONLY) = 3
read(3, "\304fd\200\34\2727f\333\316\335\370\334\251\332Pi \235-"...,
64) = 64
close(3) = 0
rt_sigaction(SIGUSR1, {0x400819, [HUP],
SA_RESTART|SA_ONESHOT|SA_NOCLDSTOP|0x555fa10}, NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [USR1], NULL, 8) = 0
clone(child_stack=0,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGC HLD,
child_tidptr=0x2a958886d0) = 16791
fstat(1, {st_mode=S_IFCHR|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(0xa) = -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="Linux", 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_IFREG|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`\3 15\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_IFREG|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(0x2a9586b000, 102400, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED, 3, 0x100000) = 0x2a9586b000
mmap(0x2a95884000, 16264, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x2a95884000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x2a95888000
arch_prctl(0x1002, 0x2a95888640) = 0
munmap(0x2a9556c000, 62108) = 0
open("/dev/urandom", O_RDONLY) = 3
read(3, "\235!\fl\347\370Y\20}$\4\232N97\263W\201\317\273\ 4\230"...,
64) = 64
close(3) = 0
rt_sigaction(SIGUSR1, {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|0x4000000}, NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [USR1], NULL, 8) = 0
clone(child_stack=0,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGC HLD,
child_tidptr=0x2a958886d0) = 16892
fstat(1, {st_mode=S_IFCHR|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 2176
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.programmer.

--
Keith Thompson (The_Other_Keith) 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.org> wrote in message news:<ln************@nuthaus.mib.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.programmer.


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.org> 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.programmer.


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
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...
125
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...
9
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...
13
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
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...
63
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...
18
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...
669
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...
6
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...
10
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*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.