Sponsored Content
Top Forums Programming Very simple Unix Prog in C, could use advice/help Post 17210 by AtleRamsli on Tuesday 12th of March 2002 07:55:31 AM
Old 03-12-2002
Excellent comment about the race condition!

One thing puzzles me, this code looks C++-ish?
Are // accepted as C comments, now?

Anyway, what I wanted to add, was that even though there is no _syntactical_ distinction between 'Procedure' and 'Function' in C, the _semantic_ one remains! So, unless a function returns void (the Function is not a "Function" but a "Procedure" Smilie ) its return value must always be checked.
This is why Perderabo tells you that 'it's ok to check first, but you must check after'

I have a 'standard' way for dealing with opening/creation.

/*
Try to open, if that fails, _try_to_ create.
If create was successful, close.
Now, we have done what we can to make sure
we can open the file, so now try to open
and return the result.
*/
myopen(...)
{
if (fd = open(...) == -1) {
if (fd = creat(...) != -1)
close(fd);
fd = open(...);
}
return(fd);
}

It all depends on what you define to be an 'error'.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Advice on UNIX/Linux

I have 6 extra hard drives and want to install them in my boxes and run UNIX, LINUX andyes M$ Winblows. How do I make it so i have the choice of which HD to boot from? Also, which UNIX distribution should i run to get aquainted with it, and wheres the best place to get it? Thank you very much if... (5 Replies)
Discussion started by: r1sk
5 Replies

2. UNIX Desktop Questions & Answers

Learning Unix , Advice?

I am a long-time Windows NT, 2000 user at home and work. I want to learn Unix to broaden myself. What flavor do you recommend and how do you recommend practicing with it at home? how to get a copy of it to practice with? (1 Reply)
Discussion started by: robmaxfli
1 Replies

3. UNIX for Dummies Questions & Answers

Advice to build a cheap Unix machine?

Hello! In my perpetual quest to learn Unix, I want to build a cheap Unix server for my personal use. Do you guys have suggestions of web sites, vendors etc...where I could get info on things such as step-by-step instruction, 2nd hand hardware etc... I would like to go preferably with a HP... (2 Replies)
Discussion started by: alan
2 Replies

4. UNIX for Dummies Questions & Answers

Re-learning Unix - need some advice.

I learned Unix about 6 years ago, I think it was the System V version back then. My course taught me, Unix commands, shell scripting, Vi editor, and probably more stuff. I wanted to stick with Unix but life didn't allow me to, my college back then used Windows 95 and then my job as a... (8 Replies)
Discussion started by: thoughts
8 Replies

5. UNIX for Dummies Questions & Answers

UNIX Help & Advice

I originally sent this as a PM to Neo, but I was ignorant towards the rule of PMing someone with technical information. Below is a copy / paste of the questions I asked. If anyone can help, I'd appreciate it tons. Thank you! :D The first OS I ever encountered was DOS and Windows 3.1. Ever since... (1 Reply)
Discussion started by: ZeeSquared
1 Replies

6. UNIX for Dummies Questions & Answers

UNIX advice needed desperately :/

Firstly I do need to state that I am turning here as a last resort. Im in my first year of a Computer Networking and technical support course, and as a first year I must complete a Introductory Unix/Linux class. I have been doing well in the course so far however I have been stumped on the final... (1 Reply)
Discussion started by: setaylor5
1 Replies

7. Solaris

Advice to become a unix guru?

Hello, I have recently completed my training for Solaris 10 and have a good understanding of the bases now. I am playing around with my system but can't do much since my knowledge is limited. I am looking for some projects or some threads where I can learn more and expand my knowledge step by... (16 Replies)
Discussion started by: saudsos
16 Replies

8. UNIX for Dummies Questions & Answers

Advice needed regarding Unix doc/book

I am a newbie Oracle DBA. All our oracle servers are hosted in AIX and Solaris Environment. I have no knowledge about this Unix O/S. So, it will be great if anyone here can suggest me any good book which will teach me the basics. Any inputs/suggestions will be great for me. (1 Reply)
Discussion started by: sandip250382
1 Replies

9. Shell Programming and Scripting

Need advice for project UNIX to Linux migration

I am working on UNIX AIX to Linux migration. Does anybody know the good site for doing this? Thanks for contribution (4 Replies)
Discussion started by: digioleg54
4 Replies
dup2(2) 							System Calls Manual							   dup2(2)

NAME
dup2 - duplicate an open file descriptor to a specific slot SYNOPSIS
DESCRIPTION
fildes is a file descriptor obtained from a or system call. fildes2 is a non-negative integer less than the maximum value allowed for file descriptors. causes fildes2 to refer to the same file as fildes. If fildes2 refers to an already open file, the open file is closed first. The file descriptor returned by has the following in common with fildes: o Same open file (or pipe). o Same file pointer (that is, both file descriptors share one file pointer.) o Same access mode (read, write or read/write). o Same file status flags (see fcntl(2), The new file descriptor is set to remain open across system calls. See fcntl(2). This routine is found in the C library. Programs using but not using other routines from the Berkeley importability library (such as the routines described in bsdproc(3C)) should not give the option to ld(1). RETURN VALUE
Upon successful completion, returns the new file descriptor as a non-negative integer, fildes2. Otherwise, it returns -1 and sets to indi- cate the error. ERRORS
fails if the following is true: fildes is not a valid open file descriptor or fildes2 is not in the range of legal file descriptors. An attempt to close fildes2 was interrupted by a signal. The file is still open. WARNINGS
A multithreaded application or an application with a signal handler can exhibit a race between on one thread and another kernel call that assigns a new file descriptor while running on a second thread or signal handler. If fildes2 is free before the call was made, the other thread or signal handler may win the race and acquire that descriptor (e.g., in an call). The thread calling can then close this file and reuse the descriptor. This results in multiple functions improperly referring to the same file. This race can be avoided either by ensuring that fildes2 references an open file before calling or by providing user-level synchronization (or signal disabling) which makes sure that the thread and another thread or signal handler don't make competing calls into the kernel at the same time. SEE ALSO
close(2), creat(2), dup(2), exec(2), fcntl(2), open(2), pipe(2), privileges(5). STANDARDS CONFORMANCE
dup2(2)
All times are GMT -4. The time now is 06:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy