Sponsored Content
Top Forums Programming how to prevent deadlock on this... Post 86175 by Perderabo on Tuesday 11th of October 2005 08:57:51 PM
Old 10-11-2005
You are setting the O_NDELAY flag on the open(). That is a bad idea for several reasons. The newer O_NONBLOCK flag corrects a problem with O_NDELAY. Use O_NONBLOCK for new code. Setting either flag means that the open() call itself will not hang. But subsequent operations on the fd are being affected. Posix says, about O_NONBLOCK, "When opening a block special or character special file that supports non-blocking opens: Subsequent behavior of the device is device-specific." The Linux man page says, "Neither the open nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait".

These flags are affecting read(). The read will return immediately if no characters are available. The old O_NDELAY will return a zero in this case. That is the same thing EOF would return... this is the problem with that flag. If O_NONBLOCK is set, -1 is returned with errno set to EAGAIN.

So my advice: use O_NONBLOCK for the open and then turn it off... something like this:
flags=fcntl(fd, F_GETFL, 0);
flags &= ~O_NONBLOCK;
fcnt(fd, F_SETFL,flags);

Next, you mention that you are setting termios paramters. Take a look at CMIN and CTIME. You can use them to control when the read returns. That means you can get rid of select().

You may still need to do more than one read. Use an overall loop...something like:
Code:
nbytes=16;
nreads=5;
ptr=buffer;
for (nreads=5; nbytes && nreads; nreads--)  {
        iret = read(fd, ptr, nbytes);
        if (iret > 0) {
                ptr += iret;
                nbytes -= iret;
        }
}

Of course, you will need to check the sys calls for failure. I'm just sketching in the basic flow, not writing real code.
 

10 More Discussions You Might Find Interesting

1. HP-UX

how to check whether a script is running actively or hanged/ in deadlock)

Hi I have a doubt regarding process states in HP unix system. Is there a way to check whether a process is hanged or still actively running? I have few scripts which run for a long time. but sometimes these are getting hanged. But i'm never sure whether they are running or waiting in kind of... (4 Replies)
Discussion started by: truth
4 Replies

2. Programming

How to prevent a class from inheretance?

:(Hi, There is a class in C++ called "CL". It should not participate in inheretance. If some body inherit it it should give errors.....:( (0 Replies)
Discussion started by: krishna_sicsr
0 Replies

3. UNIX for Dummies Questions & Answers

Please help about Unix Deadlock

hello guy, i really have a hard time complete one of my school paper. Does anyone how Unix deal with Deadlock situation. ><; Description or theory is good enough. ^^". Thank you for your kindness, Jaideej (1 Reply)
Discussion started by: jaideej
1 Replies

4. UNIX for Dummies Questions & Answers

Deadlock

Hi All, how to find which all processes cause deadlock into the system and how we can resolve in Unix platform. (1 Reply)
Discussion started by: ravi.sadani19
1 Replies

5. UNIX for Advanced & Expert Users

Parallel access - how to prevent

I have one shell script which is being accessed by many jobs at same time. I want to make the script such that , other job should wait for the script if script is being used by some other job. Is there any way to implement it in script level ? Gops (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

6. UNIX for Dummies Questions & Answers

How to prevent queues from disabling themselves

I understand that on my HP-UX 11.31 system when print queues can no longer communicate with remote printers, the queue disables itself. How can I configure it to stop disabling itself, or alternatively, to re-enable itself when the remote printer comes back online? I have users in warehouses who... (6 Replies)
Discussion started by: EatenByAGrue
6 Replies

7. Shell Programming and Scripting

how to prevent process from being killed

Hi,all.Well,I know someone has already asked this question before,however,It's too long before.So i post a new thread here. Here is the issue.I have a shell script that use awk to calculate something and the script takes about 15 mins,it will use 100% CPU,and the system automatically killed the... (2 Replies)
Discussion started by: homeboy
2 Replies

8. Shell Programming and Scripting

How to prevent command from deleted

Hi, I've been searching around for solution, hope that some gurus here can help. I'm using some commands in my shell script and I'd like to protect these command to be moved to another directory. For instance, cp currently in /bin/cp. If I move it to /bin/cpxxx, my script will not be able to... (3 Replies)
Discussion started by: gklntn
3 Replies

9. UNIX for Advanced & Expert Users

How to prevent Accidents 'rm -rf *'?

When invoking unix commands from other third party tools (IBM ETL), we run the rm / mv commands with the folder as argument been passed. Eg rm -rf {folder}/* when the parameter {folder} did not pass rightly or becomes blank, the command becomes dangerous to execute rm -rf /* How to prevent... (9 Replies)
Discussion started by: deepakwins
9 Replies

10. Emergency UNIX and Linux Support

How to prevent emails as spam?

If an email is sent from our application server(running on AIX) to an id that is outside of the organization like gmail etc, and if gmail should not treat the mail as spam, what has to be done from unix level? (7 Replies)
Discussion started by: ggayathri
7 Replies
read(2) 							System Calls Manual							   read(2)

Name
       read, readv - read from a file

Syntax
       cc = read(d, buf, nbytes)
       int cc, d;
       char *buf;
       int nbytes;

       #include <sys/types.h>
       #include <sys/uio.h>

       cc = readv(d, iov, iovcnt)
       int cc, d;
       struct iovec *iov;
       int iovcnt;

Arguments
       d	 File descriptor.

       buf	 Character pointer where information is stored.

       nbytes	 Integer that tells you how many bytes to read.

       iov	 Pointer to an iovec structure.

       iovcnt	 The number of iovec structures to be processed.

Description
       The system call attempts to read nbytes of data from the object referenced by the descriptor d into the buffer pointed to by buf.  The sys-
       tem call performs the same action, but scatters the input data into the iovcnt buffers specified by the	members  of  the  iovec  following
       array: iov[0], iov[1], ..., iov[iovcnt-1].

       For the iovec structure is defined as follows:

       struct iovec {
	    caddr_t   iov_base;
	    int  iov_len;
       };

       Each  iovec  entry  specifies  the base address and length of an area in memory where data should be placed.  The system call fills an area
       completely before proceeding to the next area.

       On objects that are capable of seeking, the starts at a position given by the pointer associated with d.  See for more  information.   Upon
       return from the pointer is incremented by the number of bytes actually read.

       Objects	that are not capable of seeking always read from the current position.	The value of the pointer associated with such an object is
       undefined.

       When attempting to read from an empty pipe (or FIFO):

       o    If no process has the pipe open for writing, returns zero to indicate end-of-file.

       o    If some process has the pipe open for writing and O_NDELAY or O_NONBLOCK is set, returns a -1, errno is  to  [EWOULDBLOCK].   If  some
	    process has the pipe open for writing and O_NDELAY and O_NONBLOCK are clear, blocks until data is written or the pipe is closed by all
	    processes that opened the pipe for writing.

       Upon successful completion, and return the number of bytes actually read and placed in the buffer.  The system reads the  number  of  bytes
       requested  if  the  descriptor  references  a  file  which  has	that many bytes left before the end-of-file; this is not true in any other
       instance.

       Unless the SV_INTERRUPT bit has been set for a signal, the system calls are automatically restarted when a process receives a signal  while
       waiting for input. See also

Return Values
       If the returned value is 0, then end-of-file has been reached.

       If the read is successful, the number of bytes actually read is returned.  Otherwise, a -1 is returned and the global variable errno is set
       to indicate the error.

Diagnostics
       The and system calls fail if one or more of the following are true:

       [EBADF]	      The d argument is not a valid file or socket descriptor open for reading.

       [EFAULT]       The buf points outside the allocated address space.

       [EINTR]	      A read from a slow device was interrupted before any data arrived by the delivery of a signal.

       [EIO]	      An I/O error occurred while reading from the file system.

       [ESTALE]       The file handle given in the argument is invalid. The file referred to by that file handle no  longer  exists  or  has  been
		      revoked.

       [EWOULDBLOCK]  The O_DELAY or O_NONBLOCK flag is set for the file descriptor and the process would be delayed in the read operation.

       In addition, may return one of the following errors:

       [EINVAL]       The iovcnt was less than or equal to 0, or greater than 16.

       [EINVAL]       One of the iov_len values in the iov array was negative.

       [EINVAL]       The sum of the iov_len values in the iov array overflowed a 32-bit integer.

       [EFAULT]       Part of the iov points outside the process's allocated address space.

       [ETIMEDOUT]    A  connect request or remote file operation failed because the connected party did not respond after a period of time deter-
		      mined by the communications protocol.

Environment
       SYSTEM_FIVE

       When you use the System V environment, note the following:

       o    If your program is compiled in this environment, a and system call returns 0 if the file has been set up for non-blocking I/O and  the
	    read would block.

       o    In this environment, the parameter nbytes is of type int instead of type unsigned.

       POSIX

       In the POSIX environment, [EAGAIN] is returned in errno instead of [EWOULDBLOCK].

See Also
       dup(2), open(2), pipe(2), sigvec(2), socket(2), socketpair(2)

																	   read(2)
All times are GMT -4. The time now is 04:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy