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
open(2) 							System Calls Manual							   open(2)

NAME
open, creat - Opens a file for reading or writing SYNOPSIS
#include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> int open ( const char *path, int oflag [ , mode_t mode ] ); int creat ( const char *path, mode_t mode ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: creat(): POSIX.1, XPG4, XPG4-UNIX open(): POSIX.1, XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Specifies the file to be opened or created. If the path parameter refers to a symbolic link, the open() function opens the file pointed to by the symbolic link. Specifies the type of access, special open processing, the type of update, and the initial state of the open file. The parameter value is constructed by logically OR-ing special open processing flags. These flags are defined in the fcntl.h header file and are described below. Specifies the read, write, and execute permissions of the file to be created (requested by the O_CREAT flag in the open() interface). If the file already exists, this parameter is ignored. This parameter is constructed by logically OR-ing values described in the sys/mode.h header file. DESCRIPTION
The following two function calls are equivalent: creat(path, mode); open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); The open() and creat() functions establish a connection between the file named by the path parameter and a file descriptor. Subsequent I/O functions, such as read() and write(), use the open file descriptor to access the file. The file descriptor returned for a file is the lowest file descriptor not previously opened for that process. In most cases, a process cannot have more than OPEN_MAX file descriptors open simultaneously. The per-process soft descriptor limit is configurable. The minimum value is 64. See getrlimit(2) and setrlimit(2). The open() and creat() functions suspend the calling process until the calling thread is suspended. When an open() or creat() function is called, the file offset, which marks the current position within the file, is set to the beginning of the file. The new file descriptor is set to remain open across exec functions (see fcntl(2)). The file status flags and file access flags are designated by the oflag parameter. The oflag parameter is constructed by bitwise-inclusive OR-ing exactly one of the file access flags with one or more of the file status flags. File Access Flags The file access flags, which specify read and write access are as follows: The file is open only for reading. The file is open only for writing. The file is open for reading and writing. Only one file access value (O_RDONLY, O_WRONLY, or O_RDWR) can be specified. If exactly no value is set, the default O_RDONLY is used. File Status Flags The file status flags, which specify file open processing, are as follows: If the file exists, this flag has no effect, except as described under the O_EXCL flag. If the file does not exist, a regular file is created with the following characteristics: The owner ID of the file is set to the effective user ID of the process. The group ID of the file is set to the group ID of its parent directory. However, when the vfs subsystem attribute sys_v_mode is set to 1, the group ID of the file is set either to the group ID of the process or, if the S_ISGID bit of the parent directory is set, to the group ID of the parent directory. If the group ID of the new file does not match the process's effective group or one of its supplementary group IDs, the S_ISGID bit of the new file is cleared. The access permission bits of the file mode are set to the value of mode as follows: The corresponding bits are AND-ed with the complement of the file mode creation mask for the process. The file permission and attribute bits are set to the value of the mode parameter, which is modified as follows: All bits in the file mode whose corresponding bits in the file mode creation mask are set are cleared. The set-user ID attribute (S_ISUID bit) is cleared. The set-group ID attribute (S_ISGID bit) is cleared. The access control list of the new file is set to WILDCARD (discretionary access to the file according to traditional UNIX rules). To create a new file, the calling process must have permission to write to the file's parent directory with respect to all access control policies. If the file exists and O_EXCL and O_CREAT are set, the open will fail. If the path parameter identifies a termi- nal device, this flag assures that the terminal device does not become the controlling terminal for the process. System V Compatibility In the Tru64 UNIX operating system, O_NOCTTY is set by default and cannot be unset. In the System V habitat, however, O_NOCTTY is not set by default, and a terminal device can become the controlling terminal for the process if both of the following conditions are met: The process does not already have a controlling terminal. The terminal device pointed to by path is not already a control- ling terminal. If the file does not exist, this flag has no effect. If the file exists, is a regular file, and is successfully opened with O_WRONLY or O_RDWR*, the following occurs: The length of the file is truncated to 0 (zero). The owner and group of the file are unchanged. The set-user ID attribute of the file mode is cleared, unless the vfs subsystem variable sys_v_mode is set to 1. In this case, the file length is truncated to 0 and the mode, owner, and group are not changed. The set-user ID attribute of the file is cleared. The open fails if either of the following conditions is true: The file supports enforced record locks and another process has locked a portion of the file. The file does not allow write access. If the oflag parameter also specifies O_SYNC, O_DSYNC, or O_RSYNC, the truncation becomes a synchronous update. A program can request some control over when updates should be made permanent for a regular file that is opened for write access. The calling process must have write access to the file with respect to all access policies. File status flags that specify special processing for subsequent reads and writes of the open file are as follows: If set, and if the vfs subsystem variable strict_posix_osync is set to 1 (enabled), updates and writes to regular files and block devices will synchronously update data and file attribute information. When a function that performs an O_SYNC synchronous update (a write() system call when O_SYNC is set) returns, the calling process is assured that all data and file attribute information has been written to permanent storage, even if a file is also open for deferred (asynchronous) update. If the strict_posix_osync variable is set to 0, updates and writes to regular files and block devices synchronously update only data (see the O_DSYNC flag). If set, updates and writes to regular files and block devices will synchronously update only the data and file attributes that are required to retrieve data. For example, this occurs when a file is extended. When a function returns that performs an O_DSYNC synchronous update (a write() system call when O_DSYNC is set), the calling process is assured that all data has been written to permanent storage. However, using O_DSYNC does not guarantee that file-con- trol information, such as owner and modification time, is updated to permanent storage. If set and if O_DSYNC is set, enables synchronized I/O data integrity for read operations. The calling process is assured that all pending file data writes are written to permanent storage prior to a read. If set, and if O_SYNC is set, enables synchronized I/O file integrity for read operations. The calling process is assured that all data and file attribute information is written to permanent storage prior to a read. If O_RSYNC stoppp is used alone, it has no effect. If set, the file pointer is set to the end of the file prior to each write. If set, the call to open() will not block, and subsequent read() or write() operations on the file will be nonblocking. AdvFS-only File Flags The following file access and status flags are used only with AdvFS, and are ignored by all other file systems. Enables direct I/O on a file. Also enables direct I/O for all processes that have opened the file. Note that you cannot enable direct I/O for a data logging file or for a file that is mmapped. In addition, you cannot mmap a file that has direct I/O enabled. General Notes on oflag Parameter Flag Values The effect of O_CREAT is immediate. When opening a FIFO with O_RDONLY: If O_NDELAY or O_NONBLOCK is not set, the open() function is blocked until another process opens the file for writing. If the file is already open for writing (even by the calling process), the open() function returns immediately. If O_NDELAY or O_NONBLOCK is set, the open() function returns immediately. When opening a FIFO with O_WRONLY: If O_NDELAY or O_NONBLOCK is not set, the open() function is blocked until another process opens the file for reading. If the file is already open for reading (even by the calling process), the open() function returns without delay. If O_NDELAY or O_NONBLOCK is set, the open() function returns an error if no process currently has the file open for reading. When opening a block special or character special file that supports nonblocking opens (such as from a terminal device): If O_NDELAY or O_NONBLOCK is not set, the open() function is blocked until the device is ready or available. If O_NDELAY or O_NONBLOCK is set, the open() function returns without waiting for the device to be ready or available. Subsequent behavior of the device is device-specific. When opening a STREAMS file, oflag may be constructed from O_NDELAY or O_NONBLOCK OR-ed with either O_RDONLY, O_WRONLY or O_RDWR. Other flag values are not applicable to STREAMS devices and have no effect on them. The value of O_NDELAY or NON_BLOCK affects the operation of STREAMS drivers and certain system calls. See read(2), getmsg(2), putmsg(2), and write(2). For drivers, the implementation of O_NDELAY or NON_BLOCK is device-specific. Different STREAMS device drivers may treat this option differently. RESTRICTIONS
Since a file newly created by creat() is write_only, an fdopen() call using the r+ parameter fails if it follows a creat() call. A solu- tion to this problem is to create the file using a call that adheres to the following format: open(path, O_RDWR | O_CREAT, 0666); RETURN VALUES
On successful completion, the open() and creat() functions return the file descriptor, which is a nonnegative integer. If the open fails, a value of -1 is returned and errno is set to indicate the error. ERRORS
If the open() or creat() function fails, errno may be set to one of the following values: One of the following occurred: Search permission is denied on a component of the path prefix, the type of access specified by the oflag parameter is denied for the named file, the file does not exist and write permission is denied for the parent directory, or O_TRUNC is specified and write permission is denied. The O_TRUNC flag is set, the named file exists with enforced record locking enabled, and record locks are put on the file. The named file is a block device file and the block device is in use by a mounted file system. The directory in which the entry for the new link is being placed cannot be extended, because the quota of disk blocks (or inodes that are defined for the user on the file system containing the directory) has been exhausted. The O_CREAT and O_EXCL flags are set and the named file exists. The path parameter is an invalid address. A signal was caught by the open() function. The owner or group ID is not a value supported by this implementation, or the O_DIRECTIO flag was specified and the file is already opened for atomic write data logging or is mmapped. [Tru64 UNIX] O_CREAT was requested and an I/O error occurred when updating the directory. The named file is a directory and write access was requested. In a System V habitat, the named file is a directory and the oflag permission is write or read/write. Too many symbolic links were encountered in translating path. The system limit for open file descriptors per process has already reached the OPEN_MAX limit, or the per-process soft descriptor limit has already been reached. The length of the path string exceeds PATH_MAX or a pathname component is longer than NAME_MAX. The path parameter points to a remote machine and the link to that machine is no longer active. The system file table is full. One of the following applies: The O_CREAT flag is not set and the named file does not exist, the O_CREAT is set and the path prefix does not exist, or the path parameter points to an empty string. The system was unable to allocate kernel memory for more file descrip- tors. The directory that would contain the new file cannot be extended, the file does not exist, and O_CREAT is requested. Unable to allocate a stream. A component of the path prefix is not a directory. One of the following applies: The named file is a character special or block special file and the device associated with this special file does not exist. The named file is a multiplexed special file and the channel number is outside of the valid range, or no more channels are avail- able. The O_NONBLOCK flag is set, the named file is a FIFO, O_WRONLY is set, and no process has the file open for reading. A STREAMS module or driver open routine failed. The named file is a socket bound to the file system (a UNIX domain socket) and can- not be opened. The named file resides on a read-only file system and write access is required. For NFS file access, if the open() or creat() function fails, errno may be set to one of the following values: A server attempted to handle an NFS request by generating a request to another NFS server, which is not allowed. A stale NFS file handle exists. One of the following occurred: An open file was deleted by the server or another client, the server unmounted or unexported the remote directory, or the server unmounted or unexported the directory that contains an open file. A connection time-out occurred. For files that are mounted with the soft option, the server is down or there is a network problem. RELATED INFORMATION
Functions: chmod(2), close(2), fcntl(2), getmsg(2), lseek(2), putmsg(2), read(2), stat(2), truncate(2), umask(2), write(2), lockf(3) Standards: standards(5) delim off open(2)
All times are GMT -4. The time now is 07:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy