problem with restarted I/O system calls


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users problem with restarted I/O system calls
# 1  
Old 01-27-2012
problem with restarted I/O system calls

Greetings.

Suppose I have a UNIX OS with automatic-restart (by default) of interrupted system calls,
or I use sigaction() with SA_RESTART flag.

If I am in the middle of a read(fd1, buf1, MAXSIZE) or a write(fd2, buf2, MAXSIZE),
and I am interrupted by a signal, then the system will automatically restart my system calls.

The problem is that if the read() succedded with reading n1 bytes, then the file-offset of fd1
will be updated by n1 bytes, but the "offset" in buf1 buffer will not be.

So, when the kernel restarts (automatically) the read() system call, then it is very probable
that my buf1 buffer will be overwritten.

What is the solution to this problem ? (To "save" the content of buf1, before the interrupted
system call is restarted.)

Thank you.
# 2  
Old 01-28-2012
You need a function that handles EINTR only. read() returns when a signal is sent.
You do not need a signal handler. write() behaves that same way.
Code:
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

ssize_t readfd(int fd, void *buffer, size_t bytes2read)
 {
     ssize_t readbytes = 0;
     ssize_t n=0;
 
     do {
         if ((n = read(fd, 
                       &((char *)buffer)[readbytes], 
                       bytes2read - readbytes)) == -1) 
         {
             if (errno == EINTR)
                 continue;
             else
             {
                 readbytes=-1;
                 break;
             }
         }
         if (!n) break;
             
         readbytes += n;
     } while (readbytes < bytes2read);
     return readbytes;
 }

Use something like readfd in place of a naked read() call.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

System got restarted unexpectedly

One of our Host server got restarted unexpectedly and all we got is this. Not able to guess the root cause behind this .Please help. Before restart Oct 17 19:56:06 SERVER1 kernel: suspend: event channel 75 Oct 17 19:56:10 SERVER1 smartd: smartd version 5.38 Copyright (C) 2002-8 Bruce... (3 Replies)
Discussion started by: pinga123
3 Replies

2. UNIX for Dummies Questions & Answers

system calls in C

Hello, how would i be able to call ps in C programming? thanks, ---------- Post updated at 01:39 AM ---------- Previous update was at 01:31 AM ---------- here's the complete system call, ps -o pid -p %d, getpit() (2 Replies)
Discussion started by: l flipboi l
2 Replies

3. UNIX for Dummies Questions & Answers

About system calls.

Hi all, I am new here . I want to know about system call in detail. As system calls are also function .How system identifies it.:) (2 Replies)
Discussion started by: vishwasrao
2 Replies

4. BSD

system calls

what is the functions and relationship between fork,exec,wait system calls as i am a beginer just want the fundamentals. (1 Reply)
Discussion started by: sangramdas
1 Replies

5. Programming

System calls

why user is not able to switch from user to kernel mode by writing the function whose code is identical to system call. (1 Reply)
Discussion started by: joshighanshyam
1 Replies

6. UNIX Desktop Questions & Answers

Using system calls

Hi, I'm new to UNIX system calls. Can someone share your knowledge as to how exactly system calls should be executed? Can they be typed like commands such as mkdir on the terminal itself? Also, are there any websites which will show me an example of the output to expect when a system call like... (1 Reply)
Discussion started by: ilavenil
1 Replies

7. Solaris

System calls ?

where can i find the differences in System calls between solaris and aix? also is it possible to find a comprehensive list of them? (1 Reply)
Discussion started by: TECHRAMESH
1 Replies

8. UNIX for Dummies Questions & Answers

System calls?

open, creat, read, write, lseek and close Are they all primitive? :confused: *Another Question: is there a different between a system call, and an i/o system call? (2 Replies)
Discussion started by: PlunderBunny
2 Replies

9. UNIX for Dummies Questions & Answers

System calls for cp and mv

Which system calls are made for operations cp and mv (2 Replies)
Discussion started by: gaurava99
2 Replies

10. UNIX for Dummies Questions & Answers

System Calls

What does the system call "dup" do? What is the difference between dup and dup2 I have a fair idea of what it does but I am confused when its coming down to the exact details... Please help me!:confused: (2 Replies)
Discussion started by: clickonline1
2 Replies
Login or Register to Ask a Question