Open function of sys/stat.h


 
Thread Tools Search this Thread
Top Forums Programming Open function of sys/stat.h
# 1  
Old 04-10-2010
Open function of sys/stat.h

If a process already has the entire file locked for read and write using newstruct.l_type = F_WRLCK; what would happen if another process would try to open it in read only mode using open(filename, O_RDONLY); ?

I want to check if the file exists and I want it to work even if another process has it locked. (I'm not sure what open would return if the file is already locked by another process...)
# 2  
Old 04-10-2010
What happens when you run the ls command on a directory you have permissions to see?

You see all of the files. You see files that are locked, too. This is because ls is looking at file metadata, not the data in the file itself. So it does not open the file, you can think of it as opening the directory instead.

stat() works the same way, it works against file metadata, so it does not matter if another process has the file locked, stat() will work anyway. When you stat a file you are checking it's existence.

More to the point:access() is the system call (unistd.h> that you should use to check file existence and your permissions to it.

Is this what you are asking?
# 3  
Old 04-10-2010
Quote:
Originally Posted by jim mcnamara
Is this what you are asking?
Yes, thank you, it is good to know that you can get file attributes even if the file is locked. I was not aware of access().
# 4  
Old 04-10-2010
I hope you are aware that file locking doesn't prevent any process from doing anything - apart from setting a conflicting lock of course - if you didn't explicitly request mandatory locking.

The default is advisory locking, which expects processes to check for and honor locks voluntarily.

Mandatory locking is frowned upon on systems that have it (System V derivatives) and difficult (Linux) or impossible (BSD derivatives) to enable on others.
# 5  
Old 04-11-2010
Also, I would like to know if fopen, fread & fwrite are thread-safe ? Can multiple users fopen, fread & fwrite files at exactly the same time with no problems or is that the whole point why fcntl.h locking was written ?

Last edited by cyler; 04-11-2010 at 10:03 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?

(Apologies for any typos.) OSX 10.12.3 AND Windows 10. This is for the serious Python experts on at least 3.5.x and above... In script format sys.stdout.write() AND sys.stderr.write() seems to work correctly. Have I found a serious bug in the interactive sys.stdout.write() AND... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Open file function

Hello all, just a quick little part of code i'm writing to check if the file i'm writing too in my automatic process is not being written too manually. #!/bin/bash FUSER=$(/sbin/fuser -s /toto.tmp >/dev/null 2>&1) LSOF=$(/usr/sbin/lsof | grep -q "toto.tmp") PGREP=$(pgrep -f "toto.tmp" >... (6 Replies)
Discussion started by: maverick72
6 Replies

3. Programming

Function open() sets errno

I am opening a text file using open() system call in O_RDONLY mode. open() returns me a valid handler but also sets errno to 13 i.e. EACCES(Permission denied). Question is when open() is returning a valid handler then why does it sets the errno? Should not errno be set only in case of error... (10 Replies)
Discussion started by: rupeshkp728
10 Replies

4. Shell Programming and Scripting

Stat value changes

Die to what all operations, the "Modify" and "Change" values of stat output changes for a file. I found, during editing a file, Change and Modify alters. When chmod'ing Change alters, while Modify doesnot alters. Is there more situations where these changes? (1 Reply)
Discussion started by: anil510
1 Replies

5. Shell Programming and Scripting

Help !! perl open function

Help Please perl Gurus, I am trying to add ungrouped passengers in a group and I creating a script however it fails on first step only I tried all the options it returns following error. syntax error at junki line 4, near "open " Execution of junki aborted due to compilation errors. ... (2 Replies)
Discussion started by: dynamax
2 Replies

6. Programming

could not open source file "sys/elf_386.h"

Hello All, i am porting my application from SunSolaris to Linux (RHEL4). When i compile my c/c++ code i am getting the following errors. 1. catastrophic error: could not open source file "sys/elf_386.h" #include <sys/elf_386.h> 2. catastrophic error: could not open source file... (2 Replies)
Discussion started by: tsaravanan
2 Replies

7. UNIX and Linux Applications

Sybase help: Open client, bcp function

To begin: I use Linux The Problem: I need bcp functionality for scripts. Perl modules, such as Sybase:xfer, require ctlib which comes with Sybase Open Client. Talking with Sybase sales reps is an exercise in futility and hate. They know absolutely nothing about their own products and will... (0 Replies)
Discussion started by: Bubnoff
0 Replies

8. Programming

Q with stat()

From reading various articles on the net, I know stat() is used on files to get things like permissions, sizes etc... As a folder is a special type of file in Unix, I assumed that stat() could work on it as well as any general file. However, from running my program, perror() reported that the... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Programming

Hi errno in sys/stat.h

How should I use errno in a c program and what info does it have . I am working with directories and files. So can any one tell me How to access errno?I am using the stat() function on \etc directory and I am alble to access only the half of the directories.I am not able to access other half and... (6 Replies)
Discussion started by: vijlak
6 Replies

10. Shell Programming and Scripting

function to test if file is open

I need to write a function that will work in sh/ksh shell that will test to see if a file has already been opened for writting by another user has anyone written something like this? (3 Replies)
Discussion started by: johnsonbryce
3 Replies
Login or Register to Ask a Question