Sponsored Content
Full Discussion: Signalsafe data structures
Top Forums Programming Signalsafe data structures Post 302582285 by DreamWarrior on Thursday 15th of December 2011 11:05:17 AM
Old 12-15-2011
As others said, you should be very wary of doing work in a signal handler. There is a short list of async-signal safe functions. If you're calling anything outside that list, there is already potential for breakage even disregarding that caused by potential data structure corruption.

I would do as recommended, restructure your code to capture the signal and relay it outside the signal handler to be handled by the main thread of execution. You can use, for example, a pipe to put a byte on signaling something has been caught; since write is safe you can call it.

Even the pthread_mutex_lock is async-signal unsafe. Outside of rolling your own async-signal safe locks using atomic variables, I can't suggest anything else. Not to mention that even if you properly protect the data structure, you're still limited in what you can do to it by the lack of safe functions you can call.
 

3 More Discussions You Might Find Interesting

1. Programming

Recommendations For Generic C Data Structures & Algorithms

Hi All, Rather than re-invent the wheel, I am trying to find a mature C library that provides generic support for lists, trees, etc. I understand C doesn't formally support "generics", but am aware of a few solutions like GLib and SGLib. Can anyone kindly recommend what they think is best?... (1 Reply)
Discussion started by: tristan12
1 Replies

2. Programming

shared memory - userdefined data structures

Hello, I wonder if I can write my userdefined data structures(ex: a list) to a shared memory segment? I know, the shm functions get (void*) parameter so I should be able to read and write a list into the shared memory. may someone inform and clarify me about that, please? (1 Reply)
Discussion started by: xyzt
1 Replies

3. Shell Programming and Scripting

Perl Data Structures

Here is what i need to do. @data #has all column wise data so say info for col 1 location for all rows would be in this array $array = \@data But i need to create a file which should contain these information in a format for all columns even if i have got no values from some of the index... (0 Replies)
Discussion started by: dinjo_jo
0 Replies
Struct(3)						User Contributed Perl Documentation						 Struct(3)

NAME
Inline::Struct -- Manipulate C structures directly from Perl. SYNOPSIS
use Inline C => Config => Structs => ['Foo']; my $obj = Inline::Struct::Foo->new; $obj->num(10); $obj->str("Hello"); myfunc($obj); __END__ __C__ struct Foo { int num; char *str; }; void myfunc(Foo *f) { printf("myfunc: num=%i, str='%s' ", f->num, f->str); } This complete program prints: myfunc: num=10, str='Hello' DESCRIPTION
Inline::Struct is not a new language. It's a language extension designed to be used by Inline::C. It parses struct definitions and creates typemaps and XS code which bind each struct into a Perl class. This code is passed to Inline::C, which compiles it in the normal way. NOTE: Inline::Struct parses only C-style structs. It doesn't know about any C++ extensions to structs like scopes, constructors or methods. If you want such functionality you should use Inline::CPP to parse your structs. Using Inline::Struct Inline::Struct has a Parse::RecDescent grammar to parse C structs. If a struct is recognized, it can be bound to Perl. If the struct's definition is not recognized (usually because it has a member with no typemap), it will not be bound to Perl, but will be available from other functions in C or C++. The following example shows how a simple struct might look to a Perl programmer. Example 1: use Inline C => <<'END', ENABLE => 'STRUCTS'; struct Fraction { long numer; long denom; }; END my $o = Inline::Struct::Fraction->new(4, 3); print $o->numer, $o->denom, " "; $o->numer(4)->denom(7); After the code above has been compiled, Perl's namespace looks a lot like the following: package Inline::Struct::Fraction; sub new { } sub DESTROY { } sub _KEYS { } sub _VALUES { } sub _HASH { } sub numer { } sub denom { } Note that these are actually XS subs written in C, not Perl subs. But that's what it looks like. The Struct Interface The following sections define the interface of each subroutine. Note: this interface is likely to change in future versions of Inline::Struct. Please don't rely on Inline::Struct in production code quite yet. When a struct is bound by Inline::Struct, a new namespace is created underneath Inline::Struct. So if you have a struct named 'Foo', the package of the Perl class will be 'Inline::Struct::Foo'. new If no arguments are provided, all fields are zeroed out. If you provide values, they should be appropriate for the field type, and in the same order as they are defined in the struct. DESTROY The destructor. Should never be called by the programmer -- this is called automatically when the Perl variable holding the struct is destroyed. Frees the memory associated with the struct. If the struct holds pointers to malloc'd memory, they will not be freed. If you run into such a situation, consider using C++ and Inline::CPP instead. _KEYS A read-only method, this returns a reference to an array containing the names of the fields in the struct. The fields are in the order they appear in the C source code. _VALUES A read-only method, this returns a reference to an array containing the values of the fields in the struct. The values are returned in the same order as the fields. _HASH A read-only method, this returns a reference to a hash, mapping field names to field values. Accessors For each field in the struct, an accessor sub will be created which lets you get or set the value in the struct. If no arguments are provided, the sub returns the value of that field. If any arguments are provided, the field is set to the first argument, and the modified structure is returned. This makes setting multiple fields easy: $o->field1(something)->field2(somethingelse); C and C++ Configuration Options Inline::Struct has no configuration options of its own, but it does provide a new configuration option for C or C++. STRUCTS Specifies that structs are to be bound to Perl. There are several meanings to this option, so I'll explain with an example: use Inline C => Config => STRUCTS => 'Foo'; Adds 'Foo' to the list of structs to bind to Perl. use Inline C => Config => STRUCTS => ['Foo', 'Bar']; Adds 'Foo' and 'Bar' to the list of structs to bind to Perl. use Inline C => Config => STRUCTS => undef; Clears the list of structs to bind to Perl. use Inline C => Config => ENABLE => 'STRUCTS'; or use Inline C => Config => STRUCTS => 1; Enable binding structs to Perl, without specifying any structs to search for. As shown, this would bind all structs to Perl. use Inline C => Config => DISABLE => 'STRUCTS'; or use Inline C => Config => STRUCTS => 0; Disable binding structs to Perl. SEE ALSO
For more information about using C from Perl, see Inline::C. For more information about using C++ from Perl, see Inline::CPP. AUTHOR
Neil Watkiss (NEILW@cpan.org) COPYRIGHT
Copyright (C) 2001, Neil Watkiss. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. (see http://www.perl.com/perl/misc/Artistic.html) perl v5.18.2 2001-05-13 Struct(3)
All times are GMT -4. The time now is 03:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy