473,657 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatically restarting system calls?


I wrote a script(1) replacement in python (http://stromberg.dnsalias.org/
~dstromberg/pypty/), but I'm encountering a problem in it.

I think I know the solution to the problem, but I'd've thought python was
high level enough that this solution isn't required, so I wanted to
inquire about it here.

Specifically, the program has a signal handler for window size changes.
And if the window is resized during an os.write() (for example), I get a
python exception about needing to restart the system call.

In C, I know you're supposed to wrap your system calls with while loops
until you don't get an ERESTART, but does one really need to wrap all of
one's os.write()'s (for example) with such while loops in python?

Thanks!

Jun 27 '08 #1
4 2429
On Jun 13, 10:41 am, Dan Stromberg <dstrombergli.. .@gmail.comwrot e:
I wrote a script(1) replacement in python (http://stromberg.dnsalias.org/
~dstromberg/pypty/), but I'm encountering a problem in it.

I think I know the solution to the problem, but I'd've thought python was
high level enough that this solution isn't required, so I wanted to
inquire about it here.

Specifically, the program has a signal handler for window size changes.
And if the window is resized during an os.write() (for example), I get a
python exception about needing to restart the system call.

In C, I know you're supposed to wrap your system calls with while loops
until you don't get an ERESTART, but does one really need to wrap all of
one's os.write()'s (for example) with such while loops in python?
Unfortunately, signals are sometimes used to intentionally interrupt
system calls, so we can't always loop on ERESTART.

However, os.write() is a low level API. Maybe file.write() or
socket.send() would be a little more robust?
Jun 27 '08 #2
On Sat, 14 Jun 2008 10:04:15 -0700, Rhamphoryncus wrote:
On Jun 13, 10:41 am, Dan Stromberg <dstrombergli.. .@gmail.comwrot e:
>I wrote a script(1) replacement in python
(http://stromberg.dnsalias.org/ ~dstromberg/pypty/), but I'm
encountering a problem in it.

I think I know the solution to the problem, but I'd've thought python
was high level enough that this solution isn't required, so I wanted to
inquire about it here.

Specifically , the program has a signal handler for window size changes.
And if the window is resized during an os.write() (for example), I get
a python exception about needing to restart the system call.

In C, I know you're supposed to wrap your system calls with while loops
until you don't get an ERESTART, but does one really need to wrap all
of one's os.write()'s (for example) with such while loops in python?

Unfortunately, signals are sometimes used to intentionally interrupt
system calls, so we can't always loop on ERESTART.

However, os.write() is a low level API. Maybe file.write() or
socket.send() would be a little more robust?
Hmmm... How about an option with a default value of True or False, that
would control such looping?

BTW, it seems I'm getting EINTR, not ERESTART.

I perhaps could use file.write(), but I'm not confident that's the case,
because I'm using select.select() , and I'm getting EINTR's not just in
os.write() but also select.select() and tty.setraw(), so far.

The worst part is, I don't really know which python functions require
such loops and which don't, or even what those exceptions will look like
- that is, until I see a real world example. So as the errors come up in
real life, I'm wrapping my functions with:

def maybe_restarted _syscall(fn, loop_exc, loop_errnos):
while 1:
try:
result = fn()
except loop_exc, (errno, error_string):
if errno in loop_errnos:
continue
else:
sys.stderr.writ e('%s: %s\n' % (sys.argv[0], error_string))
raise
else:
break
return result

....and calling it like (for example) :

maybe_restarted _syscall(lambda : select.select([stdin, fd], [], []),
select.error, [ errno.EINTR ])

Is there some way of being a little more proactive with these errors?

Thanks!

Jun 27 '08 #3
On Jun 15, 1:06 pm, Dan Stromberg <dstrombergli.. .@gmail.comwrot e:
On Sat, 14 Jun 2008 10:04:15 -0700, Rhamphoryncus wrote:
On Jun 13, 10:41 am, Dan Stromberg <dstrombergli.. .@gmail.comwrot e:
I wrote a script(1) replacement in python
(http://stromberg.dnsalias.org/~dstromberg/pypty/), but I'm
encountering a problem in it.
I think I know the solution to the problem, but I'd've thought python
was high level enough that this solution isn't required, so I wanted to
inquire about it here.
Specifically, the program has a signal handler for window size changes.
And if the window is resized during an os.write() (for example), I get
a python exception about needing to restart the system call.
In C, I know you're supposed to wrap your system calls with while loops
until you don't get an ERESTART, but does one really need to wrap all
of one's os.write()'s (for example) with such while loops in python?
Unfortunately, signals are sometimes used to intentionally interrupt
system calls, so we can't always loop on ERESTART.
However, os.write() is a low level API. Maybe file.write() or
socket.send() would be a little more robust?

Hmmm... How about an option with a default value of True or False, that
would control such looping?

BTW, it seems I'm getting EINTR, not ERESTART.

I perhaps could use file.write(), but I'm not confident that's the case,
because I'm using select.select() , and I'm getting EINTR's not just in
os.write() but also select.select() and tty.setraw(), so far.

The worst part is, I don't really know which python functions require
such loops and which don't, or even what those exceptions will look like
- that is, until I see a real world example. So as the errors come up in
real life, I'm wrapping my functions with:

def maybe_restarted _syscall(fn, loop_exc, loop_errnos):
while 1:
try:
result = fn()
except loop_exc, (errno, error_string):
if errno in loop_errnos:
continue
else:
sys.stderr.writ e('%s: %s\n' % (sys.argv[0], error_string))
raise
else:
break
return result

...and calling it like (for example) :

maybe_restarted _syscall(lambda : select.select([stdin, fd], [], []),
select.error, [ errno.EINTR ])

Is there some way of being a little more proactive with these errors?
I can't think of anything. The best way to handle signals is to avoid
them whenever possible - which probably can't work in your case.

You might find a way to get SA_RESTART applied, but it won't work
right for you. Python splits signal handlers into a top half and a
bottom half - the bottom is in C, it's the real signal handler, and
top is in python. To keep things sane for the top half, it delays
until the interpreter is in a sane state (such as between bytecodes).
If you used SA_RESTART the bottom half would still work, but the top
would be delayed indefinitely until something else woke up the
interpreter. In other words, your window likely won't redraw until
the user types something or you print something.
Jun 27 '08 #4
Rhamphoryncus <rh****@gmail.c omwrites:
You might find a way to get SA_RESTART applied, but it won't work
right for you. Python splits signal handlers into a top half and a
bottom half - the bottom is in C, it's the real signal handler, and
top is in python. To keep things sane for the top half, it delays
until the interpreter is in a sane state (such as between
bytecodes). If you used SA_RESTART the bottom half would still
work, but the top would be delayed indefinitely until something else
woke up the interpreter. In other words, your window likely won't
redraw until the user types something or you print something.
One possible way to handle this is by implementing a small signal
handler in C. This signal handler can set the signal using
SA_RESTART, getting rid of EINTR, at least for popular syscalls. In
the signal handler it can simply write a byte into a pipe, the other
end of which is monitored by the select() event loop. Python,
presumably sleeping in select(), or about to do that, will immediately
notice the "signal" by the pipe becoming readable.
Jun 27 '08 #5

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

Similar topics

2
12703
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this script to a file (including that which is printed by the spawned programs) via the redirect (">") function on a Win2K Command Prompt. In the captured output however, the output from the os.system() calls ALWAYS comes before the output from the...
12
3423
by: Gil | last post by:
I am running a C++ process on Solaris that needs to find out how much diskspace is free and used on the system. Is 'system' in stdlib.h the only way to make OS calls from C++? In this case, I will probably be making unix calls like 'df' or 'statvfs' to get the diskspace information and redirecting it to a file, and then opening and reading
8
2252
by: Peter Olcott | last post by:
Is there any way to intercept system calls to windows such as TextOut and DrawText? what kinds of system calls can be intercepted, and what is the mechanism to accomplish this?
5
3532
by: markus | last post by:
Hi, I have a question that deals with the standard c library VS (Unix) system calls. The question is: which header files (and functions) are part of the C library and which header files (and function calls) are part of the (Unix) system calls. The cause of my confusion is that for example stdio.h is considered
22
2655
by: markus | last post by:
Hi, There are more than 1000 defined system calls in the Unix standard specification, however, a majority of them are optional and the availability of system calls are dependent on the OS implementation itself. The question I have is: How do you determine which system calls are available on any Unix/Linux machine?
11
2764
by: talk | last post by:
hi,guy i have a question. are the functions in <stdio.h> system calls provided by operation system? if so, i want to know how C implements that we can call system calls by using the functions in <stdio.h>. i need your help, thanks a lot.
21
2440
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here write() isn't called, if u * give \n here then two write()s will
0
8732
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
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
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.