Sponsored Content
Top Forums Programming Writing c code for who | sort | lp Post 302125219 by the_learner on Wednesday 4th of July 2007 01:18:46 PM
Old 07-04-2007
Quote:
Originally Posted by porter
Okay, what you need to understand is what a process is in UNIX. Basically a process is

1. a memory image

2. a set of file descriptors 0,1,2 etc.

3. a point of execution

The memory image exists outside the kernel, the file descriptors live inside the kernel. A single file descriptor points to a file. That file may be referenced by multiple file descriptors.

Fact 1. All close() does is closes a single file descriptor for the current process. That drops the usage count on the actual file, when the usage count on the file drops to zero then it is really closed.

Fact 2. When a process terminates, all it's file descriptors that are still open are closed.

Fact 3. All "dup" and "dup2" do are allocate another file descriptor to point to the same file and increase the usage count on the underlying file.

Fact 4. Fork() duplicates the entire memory image AND the file descriptor table. Both processes now continue executing at the same point, the only difference being the parent got the pid of the child, the child got nothing. Duplicating the file descriptor table is like dup() has been called on each open file descriptor.

Fact 5. Exec() replaces the entire memory image with the new program but the file descriptors do not change EXCEPT for the file descriptors marked as FD_CLOEXEC, which are closed.

Fact 6. Pipe() returns two file descriptors, one is readable, one is writeable. A pipe will remain readable until all writeable file descriptors have been closed. Remember if you create a pipe then call fork there are two descriptors that are writeable.

Hints:

Get into the habit of putting -1 in variables that hold file descriptors when they are not holding a value, when you close it, put -1 back in the file descriptor variable.


Hey thanks a lot Porter. This post has really cleared a lot of things for me. Anyway, I have left the working version of the code (you were right deadline was approaching.. Smilie But I really appreciate the time you took to explain things. Thanks again !
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

conditional writing of sql code

Hello again... I have a request from another department to list for them all the columns and tables we use in this certain database. I have spooled the oracle stored procedured into 1 file. I need a way to write out parts of that file. The criteria is to to start the block to be written when... (0 Replies)
Discussion started by: kburrows
0 Replies

2. Shell Programming and Scripting

writing code!!!

suppose by writing this code "$ /sbin/ifconfig eth0| grep 'inet addr:' " i've got this output "inet addr:192.168.2.1 Mask:255.255.255.0" now if i want to see the output like this "IP address: 192.168.2.1 Subnet Mask: 255.255.255.0" what is the code should be? can anyone... (3 Replies)
Discussion started by: shelly
3 Replies

3. Shell Programming and Scripting

Help in writing the code

hi i am studying unix with c language ,and completely new to this .I have question about how to pass the control from one program file to another.My first program has the main process and it should go into the second program where i can create six processes with also the usage of pipes to... (3 Replies)
Discussion started by: soumya_v7
3 Replies

4. Shell Programming and Scripting

script or piece of code where the data returned by a stored procedure you are writing

hi fndz. Can you please help me with the code if I call a stored procedure from my shell script and stored procedure returns a cursor, cursor output should be saved to a file (3 Replies)
Discussion started by: enigma_83
3 Replies

5. Homework & Coursework Questions

Writing Code

1. The problem statement, all variables and given/known data: Write a script that will perform the grep command on a user defined file and a user defined pattern 2. Relevant commands, code, scripts, algorithms: must use grep command (that is the only piece of information that was given) ... (2 Replies)
Discussion started by: akjim101
2 Replies

6. Programming

Vi question for writing source code

Hi guys, I'm modifying an old f77 code using vi. I'm modifying the code remotely from my windows machine using xming using vi. I'm using tabs to move past the first 6 columns of the code and to keep my loops and if statements neat, but when I hit the tab key, vi displays a big red block which is... (7 Replies)
Discussion started by: rks171
7 Replies

7. UNIX for Dummies Questions & Answers

sed remove two headers; writing more elegant code

Hi there, I have two questions. First, I was wondering how to use sed to remove two header lines or two tail lines. Here I just do the same operation twice...I'm sure there is a better way. Second, and more importantly, is there a better way to have these operations use files other than... (5 Replies)
Discussion started by: mikey11415
5 Replies

8. Shell Programming and Scripting

Sort error code 512

Hi Friends, I am back with small issue which i tried thinking but could not get much!!!! issue here is..i m using kind of extraction query using perl code in that i do sort with few number of csv files. each of them will be around 50 to 100 M size. sometime i get following error message... (1 Reply)
Discussion started by: Shahul
1 Replies

9. Shell Programming and Scripting

Writing a UNIX script from LOG to provide return code.

Folks - Firstly, I do apologize that my first post here is a question. I am quite familiar with UNIX since our application is running on it. We are trying to automate a few things on our end and I am challenged with a task in hand that requires UNIX scripting. I am totally a newbie in UNIX... (4 Replies)
Discussion started by: sk72
4 Replies

10. Shell Programming and Scripting

Help writing code

Hi, i need a string that take from a log file just two words (ngwan0) and his related ip address and then execute 2 commands in a shell. Any ideas where to start? Thanks (3 Replies)
Discussion started by: Board27
3 Replies
POSIX_SPAWN_FILE_ACTIONS_ADDOPEN(3)			   BSD Library Functions Manual 		       POSIX_SPAWN_FILE_ACTIONS_ADDOPEN(3)

NAME
posix_spawn_file_actions_addopen, posix_spawn_file_actions_adddup2, posix_spawn_file_actions_addclose -- add open, dup2 or close action to spawn file actions object LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <spawn.h> int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * file_actions, int fildes, const char *restrict path, int oflag, mode_t mode); int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t * file_actions, int fildes, int newfildes); int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t * file_actions, int fildes); DESCRIPTION
These functions add an open, dup2 or close action to a spawn file actions object. A spawn file actions object is of type posix_spawn_file_actions_t (defined in <spawn.h>) and is used to specify a series of actions to be performed by a posix_spawn() or posix_spawnp() operation in order to arrive at the set of open file descriptors for the child process given the set of open file descriptors of the parent. A spawn file actions object, when passed to posix_spawn() or posix_spawnp(), specify how the set of open file descriptors in the calling process is transformed into a set of potentially open file descriptors for the spawned process. This transformation is as if the specified sequence of actions was performed exactly once, in the context of the spawned process (prior to execution of the new process image), in the order in which the actions were added to the object; additionally, when the new process image is executed, any file descriptor (from this new set) which has its FD_CLOEXEC flag set is closed (see posix_spawn()). The posix_spawn_file_actions_addopen() function adds an open action to the object referenced by file_actions that causes the file named by path to be opened (as if open(path, oflag, mode) had been called, and the returned file descriptor, if not fildes, had been changed to fildes) when a new process is spawned using this file actions object. If fildes was already an open file descriptor, it is closed before the new file is opened. The string described by path is copied by the posix_spawn_file_actions_addopen() function. The posix_spawn_file_actions_adddup2() function adds a dup2 action to the object referenced by file_actions that causes the file descriptor fildes to be duplicated as newfildes (as if dup2(fildes, newfildes) had been called) when a new process is spawned using this file actions object, except that the FD_CLOEXEC flag for newfildes is cleared even if fildes is equal to newfildes. The difference from dup2() is useful for passing a particular file descriptor to a particular child process. The posix_spawn_file_actions_addclose() function adds a close action to the object referenced by file_actions that causes the file descriptor fildes to be closed (as if close(fildes) had been called) when a new process is spawned using this file actions object. RETURN VALUES
Upon successful completion, these functions return zero; otherwise, an error number is returned to indicate the error. ERRORS
These functions fail if: [EBADF] The value specified by fildes or newfildes is negative. [ENOMEM] Insufficient memory exists to add to the spawn file actions object. SEE ALSO
close(2), dup2(2), open(2), posix_spawn(3), posix_spawn_file_actions_destroy(3), posix_spawn_file_actions_init(3), posix_spawnp(3) STANDARDS
The posix_spawn_file_actions_addopen(), posix_spawn_file_actions_adddup2() and posix_spawn_file_actions_addclose() functions conform to IEEE Std 1003.1-2001 (``POSIX.1''), with the exception of the behavior of posix_spawn_file_actions_adddup2() if fildes is equal to newfildes (clearing FD_CLOEXEC). A future update of the Standard is expected to require this behavior. HISTORY
The posix_spawn_file_actions_addopen(), posix_spawn_file_actions_adddup2() and posix_spawn_file_actions_addclose() functions first appeared in FreeBSD 8.0. AUTHORS
Ed Schouten <ed@FreeBSD.org> BSD
May 9, 2013 BSD
All times are GMT -4. The time now is 08:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy